我使用SpriteKit来玩一个有球和角色的游戏。角色正在使用SKAction在一条线上移动,用户可以通过触摸来移动球。当球和角色接触时,我希望球附着在角色上。
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == charMask && contact.bodyB.categoryBitMask == ballMask{
println("contact")
if contactMade {
let movingBall = contact.bodyB.node!.copy() as! SKSpriteNode
//this line of code doesn't affect anything no matter what coordinates i set it to
movingBall.position = contact.bodyA.node!.position
contact.bodyB.node!.removeFromParent()
contact.bodyA.node!.removeAllActions()
contact.bodyA.node!.addChild(movingBall)
}
}
}当前的问题是,当我执行contact.bodyA.node!.addChild(球)(角色节点)时,球会附加到角色的最左侧,而不是角色上,并且设置movingball.position似乎不会影响球。我也尝试过对场景进行addChild,虽然它将球添加到正确的位置,但如果没有另一个SKAction,它不会随着角色移动。我做错了什么/我能做些什么来修复它?
同样,不使用像那样的两个精灵,我是否应该使用单独的精灵,这样就有一个没有球的人物精灵,有一个球的人物精灵和球本身,然后当它们接触时只改变节点的纹理?
发布于 2016-04-29 21:35:18
因为movingBall是bodyA.node的子类,所以它的位置需要在bodyA.node的坐标空间中,所以如果你给它(0,0)的坐标,它应该在中间:
替换:
movingBall.position = contact.bodyA.node!.position使用
movingBall.position = CGPointZero看看这是什么样子。球应该直接定位在bodyA.node的顶部。
https://stackoverflow.com/questions/31616333
复制相似问题