2
我有點新Swift。我有這個問題,SpriteNode「球」沒有反應在touchesBegan方法中應用脈衝命令。整體物理運動完全正常,重力,恢復原狀等。但是當我使用applyImpulse時沒有任何事情發生。下面的代碼:適用脈衝SKSpriteNode觸摸開關不起作用
var ball = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0, -9.81)
// Add the ball
ball = SKSpriteNode(imageNamed: "Ball")
ball.setScale(2.0)
ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
ball.zPosition = 100.0
// Physics of the ball
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.height/2)
ball.physicsBody?.allowsRotation = true
ball.physicsBody?.dynamic = true
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody?.mass = 2.0
ball.physicsBody?.restitution = 0.8
ball.physicsBody?.velocity = CGVectorMake(0.0, 5.0)
self.addChild(ball)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
self.ball.physicsBody?.applyImpulse(CGVectorMake(0.0, 5.0))
}
請幫我:)
很顯然,我有一個地過,這只是相關的代碼。
我不明白這一點。我改進了我的代碼,但它仍然不起作用: – CodeCake
@CodeCake你可以發佈更多的代碼來定義'ball'對象嗎 –
我像你說的那樣編輯了代碼(上圖),但它仍然不起作用 – CodeCake