How could I select random instances among a group

  • Hello, here is my problem,

    I have a couple of purple sprite in my level

    When I touch one of those purple sprite with my character their opacity become 50 (On collision-> set opacity to 50)

    I would like to know how I could select randomly a purple sprite among those I have already touched

    I know the event "Pick nth instance" but It pick a precise IID instance

    I know also know the event "Pick random instance" but it allow me to pick one purple sprite among all instances, and I want to pick one among the purple sprites that I have already touched -> Is there a simple way to do that ?

  • Hi,

    You have to add boolean variable (for example, named "collision") in instance variables of the sprite and edit event [ON COLLISION -> SET OPACITY TO 50 AND SET collision TO TRUE]

    Then use the system event, "PICK RANDOM INSTANCE" but add this event with this : "SPRITE IS collision" (don"t forget to invert it with right click -> Invert)...

  • Thank you Saad Swad for the help, I succeeded what I wanted to do thanks to you.

    I did as you asked and add some stuff for fun :

    Event : when character on collision with purple sprite =>Action : set opacity of purple sprite to 50, set "Collision" (instance variable of purple sprite) to 1.

    Event : Every 1.0 seconds,"Collision"(instance variable of purple sprite)=1, Pick a random purple sprite instance => Action : Apply Physics Force (0,500) at image point 0.

    Is there a way to join the project capx or images in the Forum, then this could help more other people ?

  • Upload your image on server or drive (like dropbox (recomanded), googledrive...) and use this code in your post :

    [img=URL_LINK][/img][/code:drbys96q]Or simply post url link to your .capx (updated on dropbox for example...)
  • Super, I will post the link soon, thank you Saad Swad

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • here is the link to access the construct2 (capx) file I was talking about :

    https://www.dropbox.com/s/vrqcehkejm5ix ... .capx?dl=0

    Please tell me if you can't access it, hope it will help you

  • Note : you can move the red sprite with the arrow keys

  • Perfect !

  • Super, Thanks !

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