2

I made a game on Xcode 7 BETA with an object that moves and has many different functions.

But the problem is that object always moves at the same speed and it won't speed up or slow down. What I want is for the object to speed up after every touch on screen.

I made a constant called "kDefaultXToMovePerSecond" (See below in code), so that with this constant the object always moves at the same speed.

func startMoving() {
    let moveLeft = SKAction.moveByX(-kDefaultXToMovePerSecond, y: 0, duration: 0.7)
    runAction(SKAction.repeatActionForever(moveLeft))
}

I put "-" before the constant because it needs to be negative to move. And I tried to make it speed up after every touch on screen. My code is here, in GameScene.swift,

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    if isGameOver {
        restart()
    } else if !isStarted {
        start()
    } else {
        Dot.flip()


        kDefaultXToMovePerSecond + 10

    }
}

Is something wrong with this part of my 2nd code sample?

"kDefaultXToMovePerSecond + 10"

paisanco
  • 4,098
  • 6
  • 27
  • 33
Emm
  • 1,963
  • 2
  • 20
  • 51

1 Answers1

1

There are two problems with your code. Firstly, to get kDefaultXToMovePerSecond change, the following syntax must be used: kDefaultXToMovePerSecond += 10, and not kDefaultXToMovePerSecond + 10. I am not sure why the second way of doing it, the one that you have in your code, is not caught by the compiler as it is not assigning this value to any variable. But I have tested this syntax in playground and the value of the variable does not change with this syntax. Secondly, though you create the action to move the sprite, it is never updated and therefore the sprites speed will never change. This is because by simply changing the kDefaultXToMovePerSecond variable in touchesBegan, you are merely changing the variable itself, but not the parameter value in the SKAction which is not connected to it. There are two options to create the behavior you are looking for.

  1. Add a new SKAction to the sprite every time the touchesBegan is called. This new action will override the previous one.
  2. Remove the previous action on the sprite and then add a new one every time touchesBegan is called. Though the effect of this is the same as option 1, it has the benefit of deallocating the SKActions that you have created. This prevents them from piling up, never to be used again, which takes up unnecessary memory.

In case you would like guidance on how to accomplish this, refer to the following code that I wrote and tested in the Xcode 7 Beta simulator.

class GameScene: SKScene {

let player = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 100, height: 50))
var kDefaultXToMovePerSecond: CGFloat = 0

override func didMoveToView(view: SKView) {
    player.position = CGPoint(x: 100, y: 200)
    addChild(player)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    //Make sure that it is the same kay as below
    player.removeActionForKey("anyKeyWillDo")

    kDefaultXToMovePerSecond += 10

    let moveLeft = SKAction.moveByX(kDefaultXToMovePerSecond, y: 0, duration: 0.7)
    player.runAction(SKAction.repeatActionForever(moveLeft), withKey: "anyKeyWillDo")
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

}

Note: In order to implement option 1, take out the player.removeActionForKey("anyKeyWillDo") line. For option 2, leave it in.

Acoop
  • 2,586
  • 2
  • 23
  • 39
  • kDefaultXToMovePerSecond += 10 won't work, It says "Cannot pass 'let' value 'kDefaultXToMovePerSecond' to mutating binary operator '+='" – Emm Jun 25 '15 at 12:23
  • The reason you are getting the "let" error is because you set up 'kDefaultXToMovePerSecond' with the "let" keyword. This creates a constant that cannot be changed like I suggested it should be. Instead, when instantiating the variable, use the keyword "var" – Acoop Jun 25 '15 at 12:31
  • Ya it get faster now. but overtime that I click on the screen it shows this, 2015-06-25 14:43:15.978 a Dot[5923:924684] SKScene: Animating the position of a SKScene has no effect. 2015-06-25 14:43:16.676 a Dot[5923:924684] SKScene: Animating the position of a SKScene has no effect. I can't notice that any animating is not working ! – Emm Jun 25 '15 at 12:44
  • Do you still have the startMoving function being called? With my code, it is unnecessary. It also does not animate your sprite, but animates the GameScene. This should it be happening. – Acoop Jun 25 '15 at 13:34
  • I Didn't delete startMovin function because when I delete it the game have many bugs and same things shows. – Emm Jun 25 '15 at 14:46
  • I cannot be of much help unless I get more information on what bugs you are experiencing and what functionality you are expecting. – Acoop Jun 25 '15 at 18:51
  • the objects that have to move with that bug they start to move and stop at the end of screen and they become a group of the objects, It hasn't to happen, objects need just to go and never to stop (until game over). – Emm Jun 26 '15 at 10:08