如何快速更改 SKSpriteNode 的颜色? [英] In swift how do I change the color of a SKSpriteNode?

查看:29
本文介绍了如何快速更改 SKSpriteNode 的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有黑色 SKSpriteNode 的游戏,当用户触摸屏幕时,我希望 SKSpriteNode 变为白色.我已经搜索了所有可以搜索的内容,并尝试了许多不同的策略,但都没有成功.有谁知道怎么做?

I have created a game with an SKSpriteNode that is black and when the user touches the screen I would like for the SKSpriteNode to change to white. I have googled everything I can and attempted lots of different strategies with no luck. Does anyone know how to do this?

这是我的场景的代码:

var blackBird = SKSpriteNode()

override func didMoveToView(view: SKView) {
    //Black Bird
    var blackBirdTexture = SKTexture(imageNamed:"blackbird")
    blackBirdTexture.filteringMode = SKTextureFilteringMode.Nearest

    blackBird = SKSpriteNode(texture: blackBirdTexture)
    blackBird.setScale(0.5)
    blackBird.position = CGPoint(x: self.frame.size.width * 0.35, y:
        self.frame.size.height * 0.6)

    blackBird.physicsBody =
        SKPhysicsBody(circleOfRadius:blackBird.size.height/2.0)
    blackBird.physicsBody!.dynamic = true
    blackBird.physicsBody!.allowsRotation = false

    self.addChild(blackBird)
}

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

    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)

    blackBird.color = .whiteColor()
    blackBird.colorBlendFactor = 1.0
}

推荐答案

可以在SKSpriteNode上使用color属性,例如:

You can use the color property on SKSpriteNode, for example:

sprite.color = .whiteColor()

请记住,如果您的 SKSpriteNode 有纹理,您需要将 colorBlendFactor 设置为非零值才能看到您的颜色.来自 SKSpriteNode 文档 关于 colorBlendFactor:

Bear in mind, if your SKSpriteNode has a texture you'll need to set the colorBlendFactor to a non-zero value to see your color. From the SKSpriteNode documentation on colorBlendFactor:

该值必须是介于 0.0 和 1.0 之间的数字,包括 0.0 和 1.0.默认值 (0.0) 表示颜色属性被忽略并且纹理的值应该不加修改地使用.对于大于0.0,纹理在被绘制到场景之前与颜色混合.

The value must be a number between 0.0 and 1.0, inclusive. The default value (0.0) indicates the color property is ignored and that the texture’s values should be used unmodified. For values greater than 0.0, the texture is blended with the color before being drawn to the scene.

<小时>

如果您想为颜色变化设置动画,您可以使用 SKAction:

let colorize = SKAction.colorizeWithColor(.whiteColor(), colorBlendFactor: 1, duration: 5)
sprite.runAction(colorize)

来自 SKAction 文档 关于 colorizeWithColor:colorBlendFactor:duration:

此动作只能由 SKSpriteNode 对象执行.当...的时候动作执行,精灵的 color 和 colorBlendFactor 属性被赋予新的价值.

This action can only be executed by an SKSpriteNode object. When the action executes, the sprite’s color and colorBlendFactor properties are animated to their new values.

这篇关于如何快速更改 SKSpriteNode 的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆