Random image frame from sprite...

Get help using Construct 3!

Post » Tue Sep 26, 2017 3:57 pm

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 :)
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Tue Sep 26, 2017 4:05 pm

Set animation frame to choose(0,1,2,3,4) or you can have floor(random(5)) which does the same thing.
B
48
S
19
G
87
Posts: 2,316
Reputation: 51,516

Post » Tue Sep 26, 2017 4:10 pm

Excellent!

Thank you :)
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Tue Sep 26, 2017 8:03 pm

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.
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Tue Sep 26, 2017 8:19 pm

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.
B
48
S
19
G
87
Posts: 2,316
Reputation: 51,516

Post » Tue Sep 26, 2017 10:25 pm

plinkie wrote: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.
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Tue Sep 26, 2017 10:42 pm

Add a 'trigger once' condition to it. Yeah at the moment it's choosing a new animation while score is >= 2 which is constantly.
B
48
S
19
G
87
Posts: 2,316
Reputation: 51,516

Post » Wed Sep 27, 2017 12:54 am

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

Wonder if I’m better off having one image per sprite.
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Wed Sep 27, 2017 8:21 am

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.
B
48
S
19
G
87
Posts: 2,316
Reputation: 51,516

Post » Wed Sep 27, 2017 9:21 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
304
S
123
G
98
Posts: 7,357
Reputation: 72,958

Next

Return to How do I...?

Who is online

Users browsing this forum: No registered users and 1 guest