【问题标题】:Flipping SKSpriteNode texture after collision makes it looping碰撞后翻转 SKSpriteNode 纹理使其循环
【发布时间】:2017-08-27 14:03:02
【问题描述】:

我正在学习一些 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()
        }
    }

我知道只要方向没有改变并且我在点击时它就会一直发生碰撞。但是我应该如何以及应该如何将节点反弹回来以防止这种情况发生?

【问题讨论】:

  • “它将循环图像翻转”是什么意思?
  • @0x141E 它执行多次。例如,如果您在碰撞时添加分数,而不是添加 1,它会添加 3。只要碰撞停止,它就会添加分数。只要碰撞停止,就会翻转图像。
  • 也许您可以将direction(例如“Left”)传递给rotate 方法并翻转字符并仅在参数和当前方向不同且设置时才增加分数rotate 方法中的 direction 变量。
  • @0x141E 检查图像以了解我在说什么。我确信事情是有逻辑的,所以我正在尝试你的解决方案。

标签: swift sprite-kit


【解决方案1】:

试试这个...

func rotate(newDirection:String) {
    if newDirection != direction {
        node.xScale = node.xScale * -1
        updateScoreAndChangeBackgroundColor()
        direction = newDirection
    }
}

并调用旋转

rotate (newDirection: "Left")

或者这个

rotate (newDirection: "Right")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2015-10-06
    • 2014-04-14
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多