I am new in spritekit and trying to develop a game with Acceleromotion restricting the SKSpritNode going outide the screen.It is working fine when I set the Player.physicsBody!.dynamic = true but same code is not working when setting this property to "false" . Please tell me where I am going wrong Below is the code:
import SpriteKit
import CoreMotion
class EndScene : SKScene,SKPhysicsContactDelegate {
let kShipName = "ship"
var Player = SKSpriteNode(imageNamed: "Airplane")
let motionManager: CMMotionManager = CMMotionManager()
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.frame.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
Player.physicsBody?.dynamic = false
Player.physicsBody!.mass = 0.02
self.addChild(Player)
Player.setScale(0.4)
Player.name = kShipName
motionManager.startAccelerometerUpdates()
}
func processUserMotionForUpdate(currentTime: CFTimeInterval) {
// 1
if let ship = childNodeWithName(kShipName) as? SKSpriteNode {
// 2
if let data = motionManager.accelerometerData {
// 3
if fabs(data.acceleration.x) > 0.2 {
// 4 How do you move the ship?
Player.physicsBody!.applyForce(CGVectorMake(30.0 * CGFloat(data.acceleration.x), 0))
}
}
}
}
override func update(currentTime: CFTimeInterval) {
processUserMotionForUpdate(currentTime)
}
}