Indexed Sprites variables

For questions about using Classic.

Post » Mon Jan 25, 2010 6:21 am

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[code:1k7ga6tb]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!
:P

Zetar
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Mon Jan 25, 2010 6:26 am

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.
B
16
S
8
G
4
Posts: 136
Reputation: 3,144

Post » Mon Jan 25, 2010 6:30 am

That might work. i will test it tomorrow night. Thanks! 8)
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Mon Jan 25, 2010 9:17 am

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:

[code:2svmipbv]
+ 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.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Mon Jan 25, 2010 9:53 am

[quote:6pkyd7k9][code:6pkyd7k9]+ On collision between aSprite and bSprite
-> Set global('aFoo') to aSprite.Value('life')
-> Set global('bFoo') to bSprite.Value('life')[/code:6pkyd7k9][/quote: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.
B
16
S
8
G
4
Posts: 136
Reputation: 3,144

Post » Mon Jan 25, 2010 10:22 am

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.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Mon Jan 25, 2010 6:55 pm

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 :D
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Mon Jan 25, 2010 9:49 pm

[quote="Zetar":274th7av]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. [/quote:274th7av]

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

Happens a lot, I'm always answering the wrong questions :P
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Jan 27, 2010 7:39 am

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

Zetar
B
192
S
6
G
7
Posts: 213
Reputation: 10,178


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests