首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可改变的图像

可改变的图像
EN

Stack Overflow用户
提问于 2016-09-24 10:48:40
回答 1查看 76关注 0票数 1
代码语言:javascript
复制
    Ghost = SKSpriteNode(imageNamed: "Ghost1")
    Ghost.size = CGSize(width: 50, height: 50)
    Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)

    Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
    Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
    Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
    Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
    Ghost.physicsBody?.affectedByGravity = false
    Ghost.physicsBody?.isDynamic = true

    Ghost.zPosition = 2


    self.addChild(Ghost)

在我的应用程序上,我有一个在屏幕上到处移动的对象,它的名字是“幽灵”。我不知道如何设置一个可以更改代码的按钮

代码语言:javascript
复制
Ghost = SKSpriteNode(imageNamed: "Ghost2")

而不是

代码语言:javascript
复制
Ghost = SKSpriteNode(imageNamed: "Ghost1")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-24 11:05:07

要更改SKSpriteNode的图像,请指定不同的纹理:

代码语言:javascript
复制
Ghost.texture = SKTexture(imageNamed:"Ghost2")

注意:变量名应该使用小写字母,以区别于类名。

一个包含Button和Ghost的示例实现如下图所示,右上角创建了一个红色按钮。注意按钮和重影的声明现在发生在didMoveToView之外,这样以后当用户点击屏幕时就可以引用这些变量。

代码语言:javascript
复制
class ButtonGhostScene: SKScene {
    var button: SKNode! = nil
    var ghost: SKSpriteNode! = nil

    override func didMove(to view: SKView) {
        button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
        button.position = CGPoint(x:self.size.width, y:self.size.height)

        ghost = SKSpriteNode(imageNamed: "Ghost1")
        ghost.size = CGSize(width: 50, height: 50)
        ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
        ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
        ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
        ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
        ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
        ghost.physicsBody?.affectedByGravity = false
        ghost.physicsBody?.isDynamic = true
        ghost.zPosition = 2

        self.addChild(ghost)
        self.addChild(button)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Loop over all the touches in this event
        for touch: AnyObject in touches {
            // Get the location of the touch in this scene
            let location = touch.location(in: self)
            // Check if the location of the touch is within the button's bounds
            if button.containsPoint(location) {
                ghost.texture = SKTexture(imageNamed:"Ghost2")
            }
        }
    }
}

当用户点击屏幕时,将执行touchesBegan,并进行检查以查看用户是否点击了按钮。

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

https://stackoverflow.com/questions/39671992

复制
相关文章

相似问题

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