James Ellsworth
James Ellsworth

Reputation: 27

Movement not working

I am making a plat form game, but I am having an issue where only one of the movement buttons work. Can someone please take some time to review my code and see what is wrong with it?

I do not get my errors, my guess is that the there is a problem down where I change the sod variable. When I run the app and click and hold the "left button, it moves a little but then slows to a stop. It is as if the script where I changed the variable was only run one time, even though I hold it down. Interestingly enough, before when I ran it it was just fine. Unfortunately, I do not remember what I changed.

Thanks, James

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
        }
    }
}

Upvotes: 0

Views: 43

Answers (1)

Laffen
Laffen

Reputation: 2771

When you start the game, the xspd variable starts off with a value of zero. When tapping the left node, you subtract 6 from the xspd giving you the result of -6 on the xspd. Further you have the updatePositions() function, which is called every frame I suppose and within that function you apply an impulse using a vector based on the xspd value. The if-condition after that is never fulfilled when tapping the left node first since you get a negative xspd value that breaks the condition (&& xspd > 0), hence you never get any animations with an initial velocity with a negative x.

To fix the animation, you could encapsulate the xspd in an abs() which will always return a positive number. && abs(xspd) > 0

The next issue is that your player stops moving because if you tap and hold on the left node, you won´t be able to maintain the movement speed without having to tap the left node repeatedly.

A suggestion from me, you could try the following:

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
}

I hope I've understood your problem correctly and that this is helpful

Upvotes: 2

Related Questions