Indexed Sprites variables

This forum is currently in read-only mode.
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Howdy! I am putting together a little AI experiment to show the kids. The program creates multiple moving copies of a single sprite, (Indexed). Each sprite has its own variables. What I need help with is what code to use to grab a variable from the opposing sprite on a collision. Example:

    Sprite on Collision with Sprite ->Get the sprite(other sprite you collided with) private variable ('Life').

    I assume I would need to call the index number of the collided sprite then evaluate that in an expression like

    sprite(a global variable that was called).value('Life')[/code:1k7ga6tb].  But I don't know how to pull that global value from the opposing sprite on the collision.   
    
    PLEASE HELP!  And thanks.   P.S.  LOVE THIS Program TO PIECES!
     
    
    Zetar
  • Considering all the annoyances involved in colliding one instance of a sprite with another I'd suggest you include your sprite in a family and test for Family on collision with Sprite. This way you can refer easily to whichever colliding instance you want, eg. Family('Life') and Sprite('Life') will each refer to the different colliding instances.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • That might work. i will test it tomorrow night. Thanks!

  • If you're using instances of the same sprite then selecting a particular sprite that is being collided is as simple as picking it in the collision event:

    + On collision between aSprite and bSprite
       -> Set global('aFoo') to aSprite.Value('life')
       -> Set global('bFoo') to bSprite.Value('life')
    [/code:2svmipbv]
    
    The On Collision event places both instances of the sprites into Construct's Selected Object List (the SOL).
    
    Any actions will be performed only on the instances that are on the SOL.  If there are no instances on the SOL, (i.e., you have not picked them with conditions) then any actions will be performed on ALL the instances of the object.
    
    If you are pulling info from an instance in an expression (such as the 'life' value) and that instance is on the SOL, then the info will be pulled from that instance.  If there is no instance on the SOL, then the info will be pulled from the earliest instance that was created.
    
    I don't know what Daiz means by using families, unless I'm misunderstanding something.
  • [quote:6pkyd7k9]

    + On collision between aSprite and bSprite
       -> Set global('aFoo') to aSprite.Value('life')
       -> Set global('bFoo') to bSprite.Value('life')[/code:6pkyd7k9]
    
    In Zetar's case the code would be like this:
    
    [code:6pkyd7k9]+ On collision between Sprite and Sprite
       -> Set global('aFoo') to Sprite.Value('life')
       -> Set global('bFoo') to Sprite.Value('life')[/code:6pkyd7k9]
    
    The hell are you supposed to specify which one is which in this scenario? I said to use a family because if your object Sprite is the only member of family Blue, you can do it like this:
    
    [code:6pkyd7k9]+ On collision between Blue and Sprite
       -> Set global('aFoo') to Blue.Value('life')
       -> Set global('bFoo') to Sprite.Value('life')[/code:6pkyd7k9]
    
    And it's very easy to to do basically anything, like comparing their values with each other, or doing specific things on the other one, like this for example:
    
    [code:6pkyd7k9]+ On collision between Blue and Sprite
    + Blue('life') < Sprite('life')
       -> Destroy Blue
    Else
       -> Destroy Sprite[/code:6pkyd7k9]
    
    Without the family trick, the code would look like this
    
    [code:6pkyd7k9]+ On collision between Sprite and Sprite
    + Sprite('life') < Sprite('life')
       -> Destroy Sprite
    Else
       -> Destroy Sprite[/code:6pkyd7k9]
    
    ...which I'm fairly sure would not work as intended.
  • Okay then, I did misunderstand. I read over the OP three times and somehow still missed that he was colliding instances of the same object with each other.

    Yes, the family trick will work just fine for this.

  • I think the family idea will be the answer here! Thanks to both of you.

    On a side note, deadeye, just for my clarification, when you said aSprite and bSprite, did you think I had the same sprite as two seperate objects? If so, that was my mistake of clarification. I have a single sprite object placed randomly on the screen, multiple times using a Loop proceedure.

    Unfortunantly I don't understand the SOL, or indexed sprites very well, so I will want to do a little research on that tonight. Thanks again guys!

    Zetar

  • On a side note, deadeye, just for my clarification, when you said aSprite and bSprite, did you think I had the same sprite as two seperate objects? If so, that was my mistake of clarification. I have a single sprite object placed randomly on the screen, multiple times using a Loop proceedure.

    Nah, that wasn't your fault. It was my fault for reading it wrong.

    Happens a lot, I'm always answering the wrong questions

  • Thanks again Deadeye and Daiz! If you want to see what i am working on, I posted a link in the Creations Folder : http://www.scirra.com/forum/viewtopic.php?f=4&t=5612&p=44795#p44795

    Zetar

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)