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

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

问题描述

我用黑色的SKSpriteNode创建了一个游戏,当用户触摸屏幕时,我希望SKSpriteNode变为白色.我已尽一切可能在Google上进行了搜索,并尝试了很多不同的策略,但都没有碰到运气.有人知道该怎么做吗?

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和1.0).默认值 值(0.0)表示color属性被忽略,并且 纹理的值应未经修改地使用. 对于大于 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)

来自 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天全站免登陆