我正在学习一些 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()
}
}
我知道只要方向没有改变,它就会一直发生碰撞,而我是在点击时发生碰撞的。但是我应该如何以及应该将节点弹回以防止这种情况发生?
最佳答案
试试这个...
func rotate(newDirection:String) {
if newDirection != direction {
node.xScale = node.xScale * -1
updateScoreAndChangeBackgroundColor()
direction = newDirection
}
}
调用轮换
rotate (newDirection: "Left")
或者这个
rotate (newDirection: "Right")
关于swift - 碰撞后翻转 SKSpriteNode 纹理使其循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172191/