我正在学习一些SpriteKit。我试图这样做:点击移动节点右 - >在右墙碰撞上翻转节点的纹理 - >改变方向,如果在左墙上则重复。
问题是碰撞后它会使图像翻转。什么可能导致这种情况?
我正在触摸SKSPriteNode:
moveAction = SKAction.moveBy(x: 100, y: 0, duration: 1)
node.run(moveAction, withKey: "Right")
node.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
node.physicsBody?.applyImpulse(CGVector(dx: 40, dy: 130))
这是我检测碰撞的方法:
if (firstBody.categoryBitMask & PhysicsCatagory.node == PhysicsCatagory.node &&
secondBody.categoryBitMask & PhysicsCatagory.RightWall == PhysicsCatagory.RightWall) {
direction = "Left"
rotate()
}else if (firstBody.categoryBitMask & PhysicsCatagory.RightWall == PhysicsCatagory.RightWall &&
secondBody.categoryBitMask & PhysicsCatagory.node == PhysicsCatagory.node) {
direction = "Left"
rotate()
}
就像这样我翻转节点:
func rotate(){
if direction == "Right"{
node.xScale = node.xScale * -1
updateScoreAndChangeBackgroundColor()
}else{
node.xScale = node.xScale * -1
updateScoreAndChangeBackgroundColor()
}
}
据我所知,只要方向改变而且我在点击时这样做,它就会一直发生碰撞。但是我应该如何以及应该将节点反弹以防止这种情况发生呢?
答案 0 :(得分:1)
试试这个......
func rotate(newDirection:String) {
if newDirection != direction {
node.xScale = node.xScale * -1
updateScoreAndChangeBackgroundColor()
direction = newDirection
}
}
并使用
调用旋转rotate (newDirection: "Left")
或者
rotate (newDirection: "Right")