Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How to compare SpriteNode.textures
-
How do I retrieve just the texture of a SpriteNode so that I can compare it to another?
When I compare sprite1's texture to sprite2's texture it always returns False even though I know they are the same texture.
I am assuming that I am comparing something other then the textures of sprite1 and sprite2.
How do I compare the textures so that if both sprites are 'plc:Brown_Block' then it returns True?
from scene import * sprite1 = SpriteNode('plc:Brown_Block') sprite2 = SpriteNode('plc:Brown_Block') print(sprite1.texture == sprite2.texture) False
-
When you use an image name for initializing a
SpriteNode
, it creates a new texture object every time (even if you use the same image name twice).I would recommend creating the
Texture
object separately:from scene import * tex = Texture('plc:Brown_Block') sprite1 = SpriteNode(tex) sprite2 = SpriteNode(tex) print(sprite1.texture == sprite2.texture) # <- should be `True`
-
There is really no way to do it. Use another custom attribute instead, or subclass, or make a wrapper function to store the name
sprite1.type='backgound'
sprite2.type='enemy'
sprite3.type='enemy'... or
class BackgroundSprite(SpriteNode): def __init__(self,texturename): SpriteNode.__init__(self,texturename) self._texturename=texturename ... if type(sprite1)==BackgroundSprite: #do something else. or check texturename... etc
or
def make_sprite(tex): s=SpriteNode(tex) s._texturename=tex sprite1=make_sprite('plc:Brown_Block') sprite2=make_sprite('plc:Brown_Block') if sprite1._texturename==sprite2._texturename: #do something
-
@omz Thank you for responding! This is very helpful.
-
@JonB Thank you for responding also. These are both very helpful options.