简体   繁体   中英

Removing a sprite in an if statement with SpriteKit

I want to make a game where every time a user touches, it switches between one of two "states". In order to keep track of touches, I made a variable called userTouches, which changes from true to false each time a user touches. I want to make it so that if numberOfTouches is true, it updates the texture to state0; if it's false, it updates the texture to state1. Pretty much just toggling between state0 and state1 for each touch.

   var userTouches: Bool = true


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


    for touch in (touches as! Set<UITouch>) {
        userTouches = !userTouches

    }



    let centered = CGPoint(x: size.width/2, y: size.height/2)





    let state0 = SKTexture(imageNamed:"state0")


    let state1 = SKTexture(imageNamed:"state1")


    var activeState: SKSpriteNode = SKSpriteNode(texture: state0)

    //Add new state0 if it's odd, remove old state0 if it's not.
    if userTouches == true {
       activeState.texture = state0
    println("IT'S TRUE")
    } else {
        activeState.texture = state1
    println("IT'S FALSE")
    }

  self.addChild(activeState)
    activeState.name = "state0"
    activeState.xScale = 0.65
    activeState.yScale = 0.65
    activeState.position = centered

}

When I run the code, the new textures are added according to the conditions, but the old ones are still there. They are being added as new spritenodes in the scene. I do not want this. I was expecting it to simply switch between the textures ( state0 and state1 ) of the activeState spritenode depending on my boolean variable. How can I have my code toggle between textures each time a user taps, instead of piling new spritenodes on top of one another?

Each time you create a new object, change its texture (a new object`s texture, but not the texture of object which was set up last time) and add it to the scene. That's why new objects are added and nothing happens with the old objects. Do this and it will solve your problem:

  1. Create textures and SKSpriteNode object outside the touchesBegan function
  2. If you have no init at your scene, create SKSpriteNode object at didMoveToView function for example and add it to scene
  3. Then in touchesBegan function only set texture to SKSpriteNode object

it would look something like this:

class GameScene: SKScene {
...
    let state0 = SKTexture(imageNamed:"state0")
    let state1 = SKTexture(imageNamed:"state1")
    var activeState: SKSpriteNode?

...
    override func didMoveToView(view: SKView) {        
        let centered = CGPoint(x: size.width/2, y: size.height/2)
        activeState = SKSpriteNode(texture: state0)
        self.addChild(activeState!)
        activeState!.name = "state0"
        activeState!.xScale = 0.65
        activeState!.yScale = 0.65
        activeState!.position = centered
    }
...
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        if userTouches {
           activeState!.texture = state0
        } else {
           activeState!.texture = state1
        }
    }
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM