coding22
coding22

Reputation: 689

How would I switch between images in Swift?

I have this image of a car in the middle of my screen. So when I press on the screen I want it to change from the car to a plane image and when I let go of the screen I want it to go back to the car image. The code I have now keeps spawning the images over and over and they dont switch between the two images. Here is the code I have:

func addCar() {

    let redCar = SKSpriteNode(imageNamed: "redC")
    redCar.position = CGPointMake(self.size.width / 2, self.size.height / 7)
    redCar.zPosition = 42
    addChild(redCar)



    redCar.physicsBody = SKPhysicsBody(circleOfRadius: 20)
    redCar.physicsBody?.allowsRotation = false
    redCar.physicsBody?.affectedByGravity = false
    redCar.physicsBody?.contactTestBitMask = RedCategory | RedCarCategory
    redCar.physicsBody?.categoryBitMask = RedCarCategory
    redCar.physicsBody?.collisionBitMask = RedCarCategory
}



func addBluePlane() {

    let bluePlane = SKSpriteNode(imageNamed: "blueC")

    bluePlane.position = CGPointMake(self.size.width / 2, self.size.height / 7)
    bluePlane.zPosition = 43
    addChild(bluePlane)

    bluePlane.physicsBody = SKPhysicsBody(circleOfRadius: 20)
    bluePlane.physicsBody?.allowsRotation = false
    bluePlane.physicsBody?.affectedByGravity = false
    bluePlane.physicsBody?.contactTestBitMask = GreenCategory | BluePlaneCategory
    bluePlane.physicsBody?.categoryBitMask = BluePlaneCategory
    bluePlane.physicsBody?.collisionBitMask = BluePlaneCategory
}

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

    for touch in touches {
        if let touch = touches.first {
            let location = touch.locationInNode(self)
            let node = self.nodeAtPoint(location)

            addCar()
        }
    }
}


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

      addBluePlane()

}

Upvotes: 1

Views: 900

Answers (2)

coding22
coding22

Reputation: 689

Got it to work! This is how I did it:

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



        if let touch = touches.first {
            let location = touch.locationInNode(self)
            let node = self.nodeAtPoint(location)



            addCar()
            bluePlane.removeFromParent()




    }
}




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

    addBluePlane()
    redCar.removeFromParent()


}

Upvotes: -1

Max
Max

Reputation: 3439

I don't have any SpriteKit experience specifically but it seems like you are creating and adding the nodes again and again. You should declare your redCar as an instance variable instead of a local variable in the class. Same goes for bluePlane.

class YourClass {
    let redCar = SKSpriteNode(imageNamed: "redC")
    let bluePlane = SKSpriteNode(imageNamed: "blueC")

    func addCar() {
        bluePlane.removeFromParent()
        addChild(redCar)
        //rest goes here. dont create another redCar object
        //.......
    }

    func addBluePlane() {
        redCar.removeFromParent()
        addChild(bluePlane)
        //rest goes here. dont create another bluePlane object
        //.......
    }
}

Upvotes: 3

Related Questions