【问题标题】:How would I switch between images in Swift?如何在 Swift 中的图像之间切换?
【发布时间】:2016-01-04 00:39:22
【问题描述】:

我的屏幕中间有一张汽车的图像。因此,当我按下屏幕时,我希望它从汽车图像变为平面图像,当我松开屏幕时,我希望它返回汽车图像。我现在拥有的代码一遍又一遍地生成图像,并且它们不会在两个图像之间切换。这是我的代码:

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()

}

【问题讨论】:

  • 您可以使用一个 SKSpriteNode 并在汽车/飞机之间切换其纹理。或者你可以交换两个精灵节点。
  • 所以我会把 redCar.texture = SKTexture(imageNamed: "")?
  • 是的,这是一个选项(虽然你想要imageNamed: "blueC")。这取决于你。
  • 我不需要把func放在touchesBegan和touchesEnded中?
  • 我刚刚试了一下,当我按下屏幕或松开屏幕时没有任何反应。

标签: swift sprite-kit touchesbegan touchesended


【解决方案1】:

我没有任何 SpriteKit 经验,但您似乎在一次又一次地创建和添加节点。您应该将 redCar 声明为实例变量,而不是类中的局部变量。 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
        //.......
    }
}

【讨论】:

    【解决方案2】:

    让它工作!我就是这样做的:

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

    【讨论】:

    • 哇,不酷@mamnun 给了你正确的答案,而且他的方法也好多了。我会将他标记为已接受的答案
    • 我知道我也只是想分享我的做法。
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多