简体   繁体   English

从节点获取纹理名称-Swift / Spritekit

[英]Get texture name from a Node - Swift/Spritekit

I got an animation of blocks changing the colors, when the there's a click in the block, the color animation stops and I want to get the color that the block stopped. 我得到了一个动画,该动画包含改变颜色的块,当在块中单击时,颜色动画停止,我想获得块停止的颜色。

I noticed that a SKSpriteNode have a SKTexture attribute, but I can't reference it by calling: 我注意到一个SKSpriteNode具有SKTexture属性,但是我不能通过调用来引用它:

node.texture

I get an error that the node does not have a membed named 'texture', but in debugger the texture is there: 我收到一个错误,该节点没有名为“纹理”的膜,但是在调试器中存在纹理:

<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'orange@2x.png' (40 x 40)] position: {604.6412353515625, 205.83619689941406} size:{20, 20} rotation:0.0

I want to get the 'orange@2x.png' name of a Node, so I can compare it later. 我想获取节点的“ orange@2x.png”名称,以便稍后进行比较。

You probably only have the node as an SKNode , which doesn't have the texture property. 您可能只将节点作为SKNode ,而没有texture属性。 You should be able to use an optional binding to cast to SKSpriteNode , like this: 您应该能够使用可选绑定将其SKSpriteNode转换为SKSpriteNode ,如下所示:

if let spriteNode = node as? SKSpriteNode {
    println(spriteNode.texture)
}

Sprite-Kit Change Image of Node when screen gets touched 触摸屏幕时Sprite-Kit更改Node的图像

First answer may help, the way he did it seems easy and logical. 第一个答案可能会有所帮助,他的做法似乎很容易且合乎逻辑。

Make it SpriteNode instead of Node. 使它成为SpriteNode而不是Node。 Define the textures first as variables, then assign these variables to the .texture of the node. 首先将纹理定义为变量,然后将这些变量分配给节点的.texture。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM