omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    sprite texture spritenode
    3
    5
    3724
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Sketo
      Sketo last edited by ccc

      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
      
      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        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`
        
        Sketo 1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          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
          
          Sketo 1 Reply Last reply Reply Quote 0
          • Sketo
            Sketo @omz last edited by

            @omz Thank you for responding! This is very helpful.

            1 Reply Last reply Reply Quote 0
            • Sketo
              Sketo @JonB last edited by

              @JonB Thank you for responding also. These are both very helpful options.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors