How do I Display sprites based on a random number

0 favourites
  • 7 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • 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

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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?

  • 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",...}

  • Ah! Thanks for that 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 )?

  • 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,

  • Thanks for the replies. I managed to use animation frames for each different image, and it's working nicely.

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