Random image frame from sprite...

0 favourites
From the Asset Store
Selection frame like in RTS games, works both on mobile and desktop devices.
  • I have a sprite (Sprite01) with 5 animation layers (0 - 4).

    I want to display a random frame from this sprite when an event is triggered.

    On the Events sheet I've got:

    Global number Sprite01 = 0

    System > On Start of Layout > Sprite01 > Set animation frame to 0

    What do I type to set animation frame to random, or is there a better way to do this?

    Thanks

  • Set animation frame to choose(0,1,2,3,4) or you can have floor(random(5)) which does the same thing.

  • Excellent!

    Thank you

  • Ah, hang on - I've spotted a problem.

    The animation frame is chosen and displayed after the score goes above 2 (score > 2) and then the layout resets.

    But when the layout restarts, the chosen frame is then replaced by another random chosen frame, rather than the frame that was previously chosen.

    How do I get it so whatever random frame that is chosen remains chosen the next time the layout is reset?

    Currently...

    Global number Sprite01 = 0

    System > On start of layout > Sprite01 > set animation frame to choose(0,1,2,3,4)

    Events happen that reset the layout...

    System > Score = 2 > Sprite01 > Set Visible

    ...

    I need to save the selected animation frame for the next layout, similar to how the score is added to at the end of one layout and recalled at the beginning of the next.

  • Don't choose a random animation on start of layout. Set the animation frame in the same event as you set the sprite visible if you don't see it until that point. So your bottom event looks like if score >= to 2 set animation frame to choose(0,1,2,3,4) and then set visible.

  • Don't choose a random animation on start of layout. Set the animation frame in the same event as you set the sprite visible if you don't see it until that point. So your bottom event looks like if score >= to 2 set animation frame to choose(0,1,2,3,4) and then set visible.

    Yep I've tried that:

    Global number Sprite01 = 0

    Events...

    System > Score = 2 > Sprite01 > set animation frame to choose(0,1,2,3,4)

    System > Score = 2 > Sprite01 > Set Visible

    ...but the sprite just displays and rapidly scrolls through all the frames.

  • Add a 'trigger once' condition to it. Yeah at the moment it's choosing a new animation while score is >= 2 which is constantly.

  • Again this makes it pick another random frame once the layout reloads.

    Wonder if I’m better off having one image per sprite.

  • That will be because your score is still 2 when the next layout loads. Should it be reset to 0? I don't know 100% how your game works but this is kinda simple gameplay logic which is totally possible to do.

  • Use a global variable.

    Set the random value (0 to 5) to that global variable.

    When your score is > 2 set the displayed frame to the value of the global variable.

    On start of layout, if score is >2 set the displayed frame to the value of the global variable.

    The global variable value won't change when the layout restarts.

  • Use a global variable.

    Set the random value (0 to 5) to that global variable.

    When your score is > 2 set the displayed frame to the value of the global variable.

    On start of layout, if score is >2 set the displayed frame to the value of the global variable.

    The global variable value won't change when the layout restarts.

    Can you expand on this?

    I've tried entering choose(0,1,2,3,4) into the associated global variable but it will only let me enter it as a string - will this resolve as a number?

  • Here is an example :

    | Global number Sprite01 = 0

    | Global number Score = 0

    + System: On start of layout

    ----+ System: Score > 2

    -----> Sprite01: Set animation frame to Sprite01

    -----> Sprite01: Set Visible

    + System: Score > 2

    + System: Trigger once

    -> System: Set Sprite01 to choose(?0?,?1?,?2?,?3?,?4?)

    -> Sprite01: Set animation frame to Sprite01

    -> Sprite01: Set Visible

    Perhaps you want to change the name of your global variable so it does not have the same name as your sprite (which may cause confusion).

    Your global variable "Sprite01" you mention is a supposedly a number.

    Make sure the it is well set to be such.

    At worst, set the value as int(choose(0,1,2,3,4)).

  • I've changed the names of the Sprites to Charms - so I've currently got:

    Global number Charms02global = 1000

    So if the score = 2 and the Global number of Charms02global = 1000 then choose a random sprite frame.

    Then the system should set the Global number of Charms02global to the random animation frame.

    Then when the score = 2 the Global number of Charms02global should be not equal to 1000 but be equal to the random frame number so display that frame.

    But it displays the frame for the first time Score = 2 - but after that the ? event displays just the first frame of the sprite.

    Any ideas?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Anyone have any ideas on how to set the Global Number for Charms02global to the animation frame number for Charms02?

  • Guys - really struggling on this. Can anyone help?

    I have the following:

    Global number Charms02Global = 0

    Global number Charms02GlobalCheck = 0

    This breaks down into:

    • Line 2 - If the Score is equal or above 2...
    • Line 3 - If Charms02Global is equal to 0 - then choose a random frame from Charms02 - set the value of Charms02Global to the chosen frame number for Charms02 - set the value of Charms02GlobalCheck to the new number of Charms02Global - make Charms02 and it's chosen frame visible
    • Line 4 - If Charms02Global is not equal to 0 (because it should have had the frame number written to it above) - then take the number from Charms02GlobalCheck and make it the animation from for Charms02 - make Charms02 and it's chosen frame visible

    Line 2 works fine. In line 3 a random frame is chosen and displays, however the frame number doesn't seem to be saving to Charms02Global or Charms02GlobalCheck as in the next round because the Charms02Global is no longer 0, it doesn't run line 3 and goes straight to line 4 - am I using the right action (.AnimationFrameCount) to do this? Or is there another way to write the animation frame to a global variable?

    Thanks.

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