Random image frame from sprite...

Get help using Construct 3!

Post » Wed Sep 27, 2017 12:58 pm

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

Post » Thu Sep 28, 2017 4:24 am

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

Post » Thu Sep 28, 2017 5:53 pm

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

Global number Charms02global = 1000
Image

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

Post » Fri Sep 29, 2017 1:05 pm

Anyone have any ideas on how to set the Global Number for Charms02global to the animation frame number for Charms02?
B
11
S
5
G
7
Posts: 137
Reputation: 4,851

Post » Wed Oct 04, 2017 3:08 pm

Guys - really struggling on this. Can anyone help?

I have the following:
Global number Charms02Global = 0
Global number Charms02GlobalCheck = 0
Image

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

Post » Wed Oct 04, 2017 6:51 pm

As explained in the manual (Sprite article) : https://www.construct.net/gb/make-games/manuals/construct-3/plugin-reference/sprite
The expression "AnimationFrameCount" is the total number of frames your sprite has.

So consider your animation has 5 frames, in your current code, Charms02Global will end up with a value of 5 after event 3.
Then Event 4 is executed, which puts Chams02GlobalCheck to a value of 5 as well.

To get the currently displayed animation frame, use the "AnimationFrame" expression (without Count).

Also, your animation speed must be set to 0 so that only a single frame is displayed and stays that way (otherwise the Sprite will continue displaying all the frames of the animation, in order, after the delay of the animation speed has passed).
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

Previous

Return to How do I...?

Who is online

Users browsing this forum: No registered users and 0 guests