Asynchronous moving of 2 nodes sprite kit - ios

I have 2 nodes, that I want to grab with 2 fingers asynchronously on my iPad. That's code for moving:
var ninjaMoving = false
var monsterMoving = false
var loc = CGPoint(x: 0.0, y: 0.0)
var prevLoc = CGPoint(x: 0.0, y: 0.0)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if !self.nodesAtPoint(location).isEmpty && (self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 || self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3) {
if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 1 {
ninjaMoving = true
}
else if self.nodeAtPoint(location).physicsBody?.categoryBitMask == 3 {
monsterMoving = true
}
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
loc = touch.locationInNode(self)
prevLoc = touch.previousLocationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
let location = touch.locationInNode(self)
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 1 {
ninjaMoving = false
}
if self.nodeAtPoint(prevLoc).physicsBody?.categoryBitMask == 3 {
monsterMoving = false
}
}
}
override func update(currentTime: CFTimeInterval) {
if ninjaMoving {
var x = playerNinja.position.x + (loc.x - prevLoc.x)
var y = playerNinja.position.y + (loc.y - prevLoc.y)
x = max(x, playerNinja.size.width / 2)
x = min(x, size.width / 2 - playerNinja.size.width)
y = max(y, playerNinja.size.height / 2)
y = min(y, size.height - playerNinja.size.height / 2)
playerNinja.position = CGPointMake(x, y)
}
if monsterMoving {
var x = playerMonster.position.x + (loc.x - prevLoc.x)
var y = playerMonster.position.y + (loc.y - prevLoc.y)
x = max(x, self.size.width / 2 + playerMonster.size.width)
x = min(x, size.width - playerMonster.size.width / 2)
y = max(y, playerMonster.size.height / 2)
y = min(y, size.height - playerMonster.size.height / 2)
playerMonster.position = CGPointMake(x, y)
}
}
But I can't move one of sprites to the left, and another to the right at the same time. Is there any way to do that in sprite kit?

Please check the answer for this question: multitouch in spritekit
The idea is that you should track individual touches separately.

Related

SpriteKit: calculate angle of joystick and change sprite based on that

I am making a RPG Birds-eye style game with SpriteKit. I made a joystick because a D-Pad does not give the player enough control over his character.
I cannot wrap my brain around how I would calculate the neccessary data needed to change the Sprite of my character based on the angle of the joystick thumb Node.
Here is my code I am using
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if isTracking == false && base.contains(location) {
isTracking = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location: CGPoint = touch.location(in: self)
if isTracking == true {
let v = CGVector(dx: location.x - base.position.x, dy: location.y - DPad.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / Double.pi)
let Length:CGFloat = base.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * Length
let yDist: CGFloat = cos(angle - 1.57079633) * Length
print(xDist,yDist)
xJoystickDelta = location.x * base.position.x / CGFloat(Double.pi)
yJoystickDelta = location.y * base.position.y / CGFloat(Double.pi)
if base.contains(location) {
thumbNode.position = location
} else {
thumbNode.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
}
}
}
Update method
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if xJoystickDelta > 0 && yJoystickDelta < 0 {
print("forward")
}
}
The way I have set up right now tests the positive or negative state of the Joystick position in a cross method based on where the thumb Node is inside of the four marked sections below
I dont want it to do that
How can I set it up so that it changes the sprite based on where the thumb node is actually pointing inside my joysticks base like so.
I have been struggling with this for 3 days now so any help would be appreciated.
That looks far too complicated. Just compare the x and y components
of the difference vector v. Something like this:
if v.dx > abs(v.dy) {
// right
} else if v.dx < -abs(v.dy) {
// left
} else if v.dy < 0 {
// up
} else if v.dy > 0 {
// down
}

Character is not moving in desired scene in scenekit

i want to move my character in scene kit .i have a map as at github Fpscontroller and i want my character move in this map but when ever i touch to move character it jumps and don,t know where is goes here is my code.`
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if CGRectContainsPoint(gameView.virtualDPadBounds(), touch.locationInView(gameView)) {
if padTouch == nil {
padTouch = touch
controllerStoredDirection = float2(0.0)
}
} else if panningTouch == nil {
panningTouch = touches.first
}
if padTouch != nil && panningTouch != nil {
break
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = panningTouch {
let displacement = (float2(touch.locationInView(view)) - float2(touch.previousLocationInView(view)))
panCamera(displacement)
}
if let touch = padTouch {
let displacement = (float2(touch.locationInView(view)) - float2(touch.previousLocationInView(view)))
controllerStoredDirection = clamp(mix(controllerStoredDirection, displacement, t: ViewController.controllerAcceleration), min: -ViewController.controllerDirectionLimit, max: ViewController.controllerDirectionLimit)
}
}
func commonTouchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = panningTouch {
if touches.contains(touch) {
panningTouch = nil
}
}
if let touch = padTouch {
if touches.contains(touch) || event?.touchesForView(view)?.contains(touch) == false {
padTouch = nil
controllerStoredDirection = float2(0.0)
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
commonTouchesEnded(touches!, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
commonTouchesEnded(touches, withEvent: event)
}
i followed apple fox example for moving with touch and in my viewcontroller
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func walkGestureRecognized(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Ended || gesture.state == UIGestureRecognizerState.Cancelled {
gesture.setTranslation(CGPointZero, inView: self.view)
}
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
//get walk gesture translation
let translation = walkGesture.translationInView(self.view)
//create impulse vector for hero
let angle = heroNode.presentationNode.rotation.w * heroNode.presentationNode.rotation.y
var impulse = SCNVector3(x: max(-1, min(1, Float(translation.x) / 50)), y: 0, z: max(-1, min(1, Float(-translation.y) / 50)))
impulse = SCNVector3(
x: impulse.x * cos(angle) - impulse.z * sin(angle),
y: 0,
z: impulse.x * -sin(angle) - impulse.z * cos(angle)
)
heroNode.physicsBody?.applyForce(impulse, impulse: true)
let scene = gameView.scene!
let direction = characterDirection()
let groundNode = character.walkInDirection(direction, time: time, scene: scene, groundTypeFromMaterial:groundTypeFromMaterial)
setting character position and direction....
private func characterDirection() -> float3 {
let controllerDirection = self.controllerDirection()
var direction = float3(controllerDirection.x, 0.0, controllerDirection.y)
if let pov = gameView.pointOfView {
let p1 = pov.presentationNode.convertPosition(SCNVector3(direction), toNode: nil)
let p0 = pov.presentationNode.convertPosition(SCNVector3Zero, toNode: nil)
direction = float3(Float(p1.x - p0.x), 0.0, Float(p1.z - p0.z))
if direction.x != 0.0 || direction.z != 0.0 {
direction = normalize(direction)
}
}
return direction
}
there are actually two scenes called in view controller class one is empty scene called for mapNode as hereoNode and other is character sceneNode
let scene = SCNScene()
scene.rootNode.addChildNode(heroNode)
let personScene = SCNScene(named: "game.scnassets/person.scn")!
personNode = personScene.rootNode.childNodeWithName("person", recursively: true)
personNode?.position = SCNVector3(x:4.5,y:0,z:25)
node.addChildNode(personNode)
scene.rootNode.addChildNode(character.node)
i am updating my question with some more details as you can see in this screen shot image i m attatching here enter image description here i want to move this fox in map with touch but it is not working it just jump and go out from map when i touch .....plz someone tell me where is my fault in code

Move the object in a specific area

I have a circle called circle and a line called ground in the View.
I can drag & move the circle in the whole view, but when I try to limit it (in the code) to move only under the ground area - it works (when I try to drag it above the ground) and the circle moves only by my x location dragging, BUT when I try to drag it back under the ground - the circle only moves by the x location dragging and the y location dragging doesn't change (the y is the ground y).
How can I make it move back again, in a free way, under the ground and not only by the x location of the dragging?
Here is what I tried so far:
let ground = SKSpriteNode()
var circle = SKShapeNode(circleOfRadius: 40)
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// ground.physicsBody?.dynamic = false
ground.color = SKColor.redColor()
ground.size = CGSizeMake(self.frame.size.width, 5)
ground.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.size.height / 5)
self.addChild(ground)
circle.position = CGPointMake(CGRectGetMidX(self.frame) ,CGRectGetMinY(ground.frame) - (circle.frame.size.height / 2))
circle.strokeColor = SKColor.blackColor()
circle.glowWidth = 1.0
circle.fillColor = SKColor.orangeColor()
self.addChild(circle)
}
var lastTouch: CGPoint? = nil
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
if circle.position.y < ground.position.y - (circle.frame.size.height / 2){
circle.position = touchLocation
} else {
circle.position.x = CGFloat(touchLocation.x)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
if circle.position.y < ground.position.y - (circle.frame.size.height / 2) {
circle.position = touchLocation
} else {
circle.position.x = CGFloat(touchLocation.x)
}
}
I have solved it:
In touchesMoved func I wrote this code:
for touch in touches {
if touchLocation.y < ground.position.y {
circle.position = touchLocation
} else {
circle.position.x = touchLocation.x
}
}

SpriteKit friction not seeming to working properly

On a project in Xcode 7 I have a few SKSpriteNodes that move back and forth on the screen and another one, called user, that is meant to jump from sprite to sprite, progressively up the screen. However, when user lands on one of the moving sprites the moving sprite just slides right out from under it and user falls back down. I thought that this meant that I needed to increase the friction property on the nodes so that user would "stick" to the nodes, but this just makes it bounce on the other nodes. My problem is that the nodes moving back and forth seem to "slippery," and user just doesn't stay on them.
Here's my code:
My class for user:
class UserNode: SKSpriteNode
{
class func newNode(position position: CGPoint) -> UserNode
{
let position = position
let sprite = UserNode(imageNamed: "userImage")
sprite.position = position
sprite.size = CGSize(width: sprite.size.width * 2, height: sprite.size.height * 2)
sprite.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "userImage"), size: sprite.size)
sprite.physicsBody?.affectedByGravity = true
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.friction = 0.2
return sprite
}
}
and for moving user (the methods in my gamescene)
let scale: CGFloat = 2.0
let damping: CGFloat = 0.98
var point = CGPoint?()
func moveNodeToPoint(sprite: SKSpriteNode, point: CGPoint)
{
let dx = (point.x - sprite.position.x) * scale
let dy = (point.y - sprite.position.y) * scale
sprite.physicsBody?.velocity = CGVectorMake(dx, dy)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
/* Called when a touch begins */
for _: AnyObject in touches
{
if !welcomeNode.hidden
{
let fadeAway = SKAction.fadeOutWithDuration(0.3)
welcomeNode.runAction(fadeAway)
directionsNode.runAction(fadeAway)
touchStartNode.runAction(fadeAway)
welcomeNode.hidden = true
directionsNode.hidden = true
touchStartNode.hidden = true
}
//////////
point = CGPointMake(self.frame.midX, user.position.y + 300)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
point = nil
}
override func update(currentTime: CFTimeInterval)
{
/* Called before each frame is rendered */
if (point != nil)
{
moveNodeToPoint(user, point: point!)
}
else
{
let dx = user.physicsBody!.velocity.dx * damping
let dy = user.physicsBody!.velocity.dy * damping
user.physicsBody?.velocity = CGVectorMake(dx, dy)
}
}
and for moving the platforms:
let screenSize = UIScreen.mainScreen().bounds
let width = screenSize.size.width * 2
let firstAction = SKAction.moveBy(CGVector(dx: width, dy: 0), duration: 2)
let secondAction = SKAction.moveBy(CGVector(dx: -width, dy: 0), duration: 2)
let actions = [firstAction, secondAction]
let barAction = SKAction.sequence(actions)
let mainBarAction = SKAction.repeatActionForever(barAction)
platform.runAction(mainBarAction)

Removing SKSpriteNode with touch detection

I want to remove a node generated by this function by touching it.
func cuadrado(){
var cuadradoRojo = SKSpriteNode(imageNamed: "cuadradoRojo")
cuadradoRojo.physicsBody = SKPhysicsBody(circleOfRadius: cuadradoRojo.size.width)
cuadradoRojo.physicsBody?.dynamic = true
cuadradoRojo.physicsBody?.categoryBitMask = BodyType.cuadrado.rawValue
cuadradoRojo.physicsBody?.contactTestBitMask = BodyType.colorAzul.rawValue | BodyType.colorRojo.rawValue
cuadradoRojo.physicsBody?.collisionBitMask = 0
var actionArray3:NSMutableArray = NSMutableArray()
if gameOver == false{
let minX = circuloAzul.size.width/2
let maxX = self.frame.size.width - circuloAzul.size.width/2
let rangeX = maxX - minX
let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)
cuadradoRojo.position = CGPointMake(position, self.frame.size.height + cuadradoRojo.size.height)
addChild(cuadradoRojo)
let minDuration = 3
let duration = Int(minDuration)
func touchesBegan(touches: NSSet, withEvent event: UIEvent){
for touch: AnyObject in touches {
let location = (touch as UITouch).locationInNode(self)
if self.nodeAtPoint(location) == self.cuadradoRojo {
cuadradoRojo.removeFromParent()
}
}
}
actionArray3.addObject(SKAction.moveTo(CGPointMake(position, -cuadradoRojo.size.height), duration: NSTimeInterval(duration)))
cuadradoRojo.runAction(SKAction.sequence(actionArray3))
}
It runs perfectly but it doesn't detect the touch, if I put the touches function outside it detects the touch, but the game crashes.
Thanks for your help!

Resources