【问题标题】:Movement not working运动不工作
【发布时间】:2016-04-05 10:15:16
【问题描述】:

我正在制作一个平台游戏,但我遇到了一个问题,即只有一个移动按钮可以工作。有人可以花点时间查看我的代码,看看有什么问题吗?

我没有得到我的错误,我的猜测是我更改 sod 变量的地方有问题。当我运行应用程序并单击并按住“左键”时,它会移动一点,但随后会减速停止。就好像我更改变量的脚本只运行了一次,即使我按住它。有趣的是够了,在我运行之前它还不错。不幸的是,我不记得我改变了什么。

谢谢,詹姆斯

class GameScene: SKScene {

    var alien = SKSpriteNode()
    var left = SKSpriteNode()
    var right = SKSpriteNode()
    var jump = SKSpriteNode()
    var level = SKSpriteNode()
    var background = SKSpriteNode()
    var myLabel = SKLabelNode(fontNamed:"Chalkduster")

    var playerLight = SKLightNode()

    var xspd :Double = 0
    var touching :Bool = false
    var alienFrame :String = "stand"
    var textureFrames :Double = 0
    var buttonTouching :Bool = true
    var buttonCheck :Int = 0
    var ninety :Double = 0

    override func didMoveToView(view: SKView) {

        self.physicsWorld.gravity = CGVectorMake(0, -9.8)

        alien = SKSpriteNode(imageNamed: "alien_stand")
        alien.physicsBody = SKPhysicsBody(circleOfRadius: alien.frame.height / 2)
        alien.position = CGPoint(x: self.frame.width / 6, y: self.frame.height / 2)
        alien.xScale = 0.7
        alien.yScale = 0.7
        alien.physicsBody?.affectedByGravity = true
        alien.physicsBody?.dynamic = true
        alien.physicsBody?.allowsRotation = false
        alien.zPosition = 1
        alien.zRotation = 0
        self.addChild(alien)

        left = SKSpriteNode(imageNamed: "left")
        left.position = CGPoint(x: self.frame.width / 8, y: self.frame.height / 3.5)
        left.physicsBody?.pinned = true
        left.xScale = 2
        left.yScale = 2
        left.zPosition = 3
        left.alpha = 0.4
        self.addChild(left)

        right = SKSpriteNode(imageNamed: "right")
        right.position = CGPoint(x: self.frame.width / 3, y: self.frame.height / 3.5)
        right.physicsBody?.pinned = true
        right.xScale = 2
        right.yScale = 2
        right.zPosition = 4
        right.alpha = 0.4
        self.addChild(right)

        jump = SKSpriteNode(imageNamed: "up")
        jump.position = CGPoint(x: (self.frame.width / 8) * 7, y: self.frame.height / 3.5)
        jump.physicsBody?.pinned = true
        jump.xScale = 2
        jump.yScale = 2
        jump.zPosition = 5
        jump.alpha = 0.4
        self.addChild(jump)

        myLabel.text = "Hello, World!";
        myLabel.fontSize = 45;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touching = true
        if let touch = touches.first {
            if xspd > -40 && xspd < 40 {
                buttonCheck = 0
                if jump.containsPoint(touch.locationInNode(self)) {
                    alien.physicsBody?.applyImpulse(CGVectorMake(0, 250))
                    jump.alpha = 0.1
                    alien.texture = SKTexture(imageNamed: "alien_jump")
                    buttonCheck += 1
                }
                if left.containsPoint(touch.locationInNode(self)) {
                    xspd -= 6
                    left.alpha = 0.1
                    alien.xScale = -0.7
                    buttonCheck += 1
                }
                if right.containsPoint(touch.locationInNode(self)) {
                    xspd += 6
                    right.alpha = 0.1
                    alien.xScale = 0.7
                    buttonCheck += 1
                }
                if buttonCheck > 0 {
                    buttonTouching = true
                }
                else {
                    buttonTouching = false
                }
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touching = false
    }

    override func update(currentTime: CFTimeInterval) {
        updatePositions()
        playerLight.position = CGPoint(x: alien.position.x, y: alien.position.y)
        textureFrames += 1
    }

    func updatePositions() {

        myLabel.text = String(round(xspd))

        alien.physicsBody?.applyImpulse(CGVector(dx: CGFloat(xspd), dy: 0))

        if touching == true && buttonTouching == true && xspd > 0 {

            if xspd > 0 {
                ninety = 900
            }
            else {
                ninety = -900
            }

            if alienFrame == "stand" {
                alien.texture = SKTexture(imageNamed: "alien_walk1")
                alienFrame = "walk1"
            }
            else {
                if alienFrame == "walk1" {
                    if textureFrames > 9.9 / xspd {
                        alien.texture = SKTexture(imageNamed: "alien_walk2")
                        alienFrame = "walk2"
                        textureFrames = 0
                    }
                }
                else {
                    if alienFrame == "walk2" {
                        if textureFrames > 9.9 / xspd {
                            alien.texture = SKTexture(imageNamed: "alien_walk1")
                            alienFrame = "walk1"
                            textureFrames = 0
                        }
                    }
                }
            }
        }
        else {
            if xspd < 0.6 && xspd > -0.6 {
                alien.texture = SKTexture(imageNamed: "alien_stand")
            }
            else {
                if xspd != 0 {
                    xspd = xspd * 0.85
                }
            }
            right.alpha = 0.4
            left.alpha = 0.4
            jump.alpha = 0.4
        }
    }
}

【问题讨论】:

  • 当你说it moves a little but then slows to a stop; “它”是什么?
  • @Laffen 哎呀!抱歉不解释!我指的是播放器。
  • 玩家 == 外星人?
  • @Laffen 是的。那是播放器。

标签: iphone swift sprite-kit


【解决方案1】:

当您开始游戏时,xspd 变量以零值开始。点击左侧节点时,从xspd 中减去 6,得到xspd 上的 -6 结果。此外,您还有updatePositions() 函数,我想它在每一帧都被调用,并且在该函数中,您使用基于xspd 值的向量应用脉冲。之后的 if 条件在首先点击左节点时永远不会满足,因为你得到一个负的 xspd 值,它打破了条件 (&amp;&amp; xspd &gt; 0),因此你永远不会得到任何具有负 x 的初始速度的动画。

要修复动画,您可以将xspd 封装在abs() 中,该abs() 将始终返回一个正数。 &amp;&amp; abs(xspd) &gt; 0

下一个问题是您的玩家停止移动,因为如果您点击并按住左侧节点,您将无法保持移动速度而无需重复点击左侧节点。

我的建议,您可以尝试以下方法:

if xspd < 0.6 && xspd > -0.6 {

    alien.texture = SKTexture(imageNamed: "alien_stand")
} else if touching == false && xspd != 0 { // Never attempt to decrease movement speed if the player is in fact touching a movement button

    xspd = xspd * 0.85
}

我希望我已经正确理解了您的问题并且这对您有帮助

【讨论】:

  • 谢谢@Laffen!这非常有效。感谢您抽出宝贵时间来回顾一下,这是我决定进行的一个 9 年级项目,我将在下周一展示!!很有帮助,谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 2018-08-17
  • 2021-08-30
  • 2021-06-08
  • 1970-01-01
相关资源
最近更新 更多