首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中的图像之间切换?

如何在Swift中的图像之间切换?
EN

Stack Overflow用户
提问于 2016-01-04 00:39:22
回答 2查看 816关注 0票数 1

我的屏幕中央有一辆车的图像。所以当我按下屏幕,我希望它从汽车变成平面图像,当我放开屏幕时,我希望它回到汽车图像。我现在的代码不断地重复生成图像,它们不会在这两个图像之间切换。下面是我的代码:

代码语言:javascript
复制
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()

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-04 00:44:57

我没有任何具体的SpriteKit经验,但似乎您正在一次又一次地创建和添加节点。应该将redCar声明为实例变量,而不是类中的局部变量。bluePlane也是如此。

代码语言:javascript
复制
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
        //.......
    }
}
票数 3
EN

Stack Overflow用户

发布于 2016-01-04 01:19:24

开始工作了!我就是这样做的:

代码语言:javascript
复制
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()


}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34583310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档