0

I'am using SwipeGesture to move the car Up & Down. The car is not moving when I added the below line(physicsbody):

playerCar.physicsBody = SKPhysicsBody(rectangleOfSize: playerCar.frame.size)

Note: project is running fine without that line

**I need physicsBody to complete the game

import SpriteKit

class GameScene: SKScene,SKPhysicsContactDelegate {
var playerCar = SKSpriteNode()
var swipeUp = UIGestureRecognizer()
var swipeDn = UIGestureRecognizer()
override func didMoveToView(view: SKView) {
    //Car
    let playerCarTexture = SKTexture(imageNamed: "playerCar.png")
    playerCar = SKSpriteNode(texture: playerCarTexture)
    playerCar.position = CGPoint(x:frame.size.width/2, y: frame.size.height/2)
    playerCar.physicsBody = SKPhysicsBody(rectangleOfSize: playerCar.frame.size)
    playerCar.physicsBody?.dynamic = true
    playerCar.physicsBody?.affectedByGravity = false
    playerCar.physicsBody?.allowsRotation = false
    self.addChild(playerCar)

    //Swipe
    let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipeUpFunc:"))
    swipeUp.direction = .Up
    view.addGestureRecognizer(swipeUp)
    let swipeDn:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipeDnFunc:"))
    swipeDn.direction = .Down
    view.addGestureRecognizer(swipeDn)


    }

func swipeUpFunc(sender:UISwipeGestureRecognizer){
    if playerCar.position.y == (frame.size.height/2)
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: (frame.size.height/2) + 30)
    }
    else if playerCar.position.y == (frame.size.height/2) + 30
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: (frame.size.height/2) + 60)
    }
    else if playerCar.position.y == (frame.size.height/2) + 60
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: (frame.size.height/2) + 90)
    }
}

func swipeDnFunc(sender:UISwipeGestureRecognizer){

    if playerCar.position.y == (frame.size.height/2) + 90
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: (frame.size.height/2) + 60)
    }
    else if playerCar.position.y == (frame.size.height/2) + 60
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: (frame.size.height/2) + 30)
    }
    else if playerCar.position.y == (frame.size.height/2) + 30
    {
        self.playerCar.position = CGPoint(x:frame.size.width/2, y: frame.size.height/2)
    }
  }

  }
Ali
  • 3
  • 2
  • Print playerCar.position in didMoveToView right after you set it, and print it in swipeUpFunc and you will see what is the problem (node's position will change after adding a body). One question...How do you plan to use physics body ? Just for handling contacts, or you are interested in collisions and realistic physics simulations? – Whirlwind Jan 30 '16 at 13:48
  • Whirlwind:thanks for your reply. the position change when I swipe Up or Down but it's not changing in didMoveToView. I'm interested in physics simulations. – Ali Jan 30 '16 at 14:08
  • If you are interested in physics simulations then you shouldn't change node's position manually, but rather using forces. If you are changing node's position manually, you are pulling the node out of physics simulation. There are plenty of posts about this on SO, so you can find more useful stuff about that. As for changed position...I can't remember exactly why is that happening (position change when physics body is added) but I remember that there are few posts about that as well. – Whirlwind Jan 30 '16 at 14:49
  • In your case, because you are comparing two floats, as you can see, you can easily get into trouble (for example when [rounding error occurs](http://stackoverflow.com/a/19100271/3402095)). – Whirlwind Jan 30 '16 at 14:51

0 Answers0