[英]Node not moving once Physics added to scene: swift, SpriteKit
every time I add the following code: 每次我添加以下代码:
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) self.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
the physics body for my "ball" item stops moving. 我的“球”项目的物理主体停止移动。 It worked before adding the previous line.
在添加上一行之前,它起作用了。
here is all my code: 这是我的所有代码:
class GameScene: SKScene {
// creaTE nODE/Sprite
let ball = SKSpriteNode(imageNamed: "ball.png")
override func didMoveToView(view: SKView) {
// set up scene here
self.backgroundColor = SKColor.whiteColor()
//add physics to scene
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//create cg piont for sprite
ball.position = CGPointMake(size.width / 2, size.height / 2)
//give it some real life physics!
ball.physicsBody = SKPhysicsBody(circleOfRadius: size.height / 2)
addChild(ball)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
This line is causing the problem: 这行引起问题:
ball.physicsBody = SKPhysicsBody(circleOfRadius: size.height / 2)
You need to use 您需要使用
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)
The physicsBody being created in the previous line was the size of the scene, hence it did not have space to move! 在上一行中创建的PhysicsBody就是场景的大小,因此它没有移动的空间!
You can check the physicsBody being created by setting the showsPhysics
property to YES
in the SKView
. 您可以通过在
showsPhysics
属性设置为YES
来检查正在创建的SKView
。 Check here for more details. 在此处查看更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.