How do I Display sprites based on a random number

Get help using Construct 2

Post » Thu Jun 18, 2015 10:12 pm

Hi.

I am developing a game that will involve selecting objects that begin with a certain letter. I want to firstly display a sprite with a letter (say A - C), based on a random number, (i.e. if the random number=1, then display the 'A' sprite, if it = 2, then display the 'B' sprite, etc.).

Once that is done, then display some sprites. I had thought I would use a 2 dimensional array to store the sprites (or their names), where the X value would be the letter, and the Y values would be the object / sprites / sprite names. I would need to keep track of what was loaded to the layout, so I could ensure the loaded sprites were able to to be moved, etc.

Is this possible, or is there a completely different (better) approach.

Thanks - AC
B
10
S
2
Posts: 13
Reputation: 632

Post » Thu Jun 18, 2015 11:01 pm

Select the animation on on created ? Exactly how you do this depends partly on what it is you are trying to do, and how much flexibility you want and so on. It is more an issue of how you manage the name to other data mapping, not the actual issue of displaying the sprite.
My tutorial through development blog is at http://myconstruct2dev.blogspot.co.uk/
B
12
S
3
Posts: 221
Reputation: 1,328

Post » Thu Jun 18, 2015 11:09 pm

Hi. Do you mean set each animation frame as a completely different image and display the frame based on the random number? So, for example, have frame 1 as an apple, frame 2 as a banana, etc, then if the random number =2, then display the sprite with animation frame 2? I assume the loop property would be disabled?

How would I determine which object was selected in that case?
B
10
S
2
Posts: 13
Reputation: 632

Post » Thu Jun 18, 2015 11:38 pm

fruit.setFrame(random(fruit.AnimationFrameCount))

You can keep an array or list or dictionary linking the fruit animation frame to the fruit name like:

{0:"apple",1:"banana",2:"mango",...}
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,415
Reputation: 4,824

Post » Thu Jun 18, 2015 11:53 pm

Ah! Thanks for that @gumshoe2029. In your example, would I need 3 sprites, each with 3 animation frames, or a single 'container' sprite with 3 animation frames that I spawn programatically (rahther than just make visible )?
B
10
S
2
Posts: 13
Reputation: 632

Post » Fri Jun 19, 2015 7:40 am

It depends. I've just written a version of Pacman which does this pretty much - there's 6 animations (four different colours + flashing + eyes) and the four colours contain two frames for each direction (it is animated manually).

If your sprite basically always does the "same thing" - it's just the picture that differs, it can make things easier. You can put them in a family but families I think only work at a depth of one (e.g. you can't put a family in a family)

One thing I would add is that I wouldn't use the current animation frame to identify what it is, but keep that in an instance variable. The reason for that is it would make it easier to change how you map the ID to animation/frame later on - you would need to change the code only where you select the animation, not where you identify it. If you went from keeping each object in an animation to each object in a frame for some reason,
My tutorial through development blog is at http://myconstruct2dev.blogspot.co.uk/
B
12
S
3
Posts: 221
Reputation: 1,328

Post » Mon Jun 22, 2015 4:12 am

Thanks for the replies. I managed to use animation frames for each different image, and it's working nicely.
B
10
S
2
Posts: 13
Reputation: 632


Return to How do I....?

Who is online

Users browsing this forum: plinkie, Yahoo [Bot] and 19 guests