How do I set animation for each instance?

Get help using Construct 2

Post » Mon Apr 17, 2017 12:58 pm

Message: ddabrahim can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
It seems like my original question has been deleted:
viewtopic.php?t=190634&f=147#p1116775

It doesn't exist.
I have explained what is my problem is and what actions I tried to solve the problem on my own.
Anyone able to explain it to me why is my topic has been deleted?

Fortunately I always copy the text before submit so here we go again, if there is any problem with it, please explain.

I was searching the forum but can not find the answer.
I'm creating 10 instances of the same object at the start of the layout and what I'm trying to do is set their animation to be a random animation for each instance but I can't get it to work.

I have 3 animations and I named them 0,1 and 2 to be able to set the animation randomly using the random() expression.
Basically, this is what it looks like:

Code: Select all
On start of layout:
sub->Repeat 10 times : Create object
                      : Set animation to str(random(2))


But it doesn't work. Each instance display the first animation like nothing happened.
I have even tried using an instance variable. First generate a random value, store it inside an instance variable and use the variable to change the animation but I get the same result. I have also tried using the For each condition after all the objects are created:

Code: Select all
On start of layout:
sub->Repeat 10 times : Create object
                      : Set animation to random(2)    //this is an instance variable of object
sub->For each object  : Set animation to str(object.animation) 


It didn't work either.
The only thing I managed to get to work is a counter variable. I increasing the value of the variable after each instance created and use that to set the animation.

Code: Select all
On start of layout:
sub-> Local number counter = 0
sub->Repeat 10 times : Create object
                      : Set animation to str(counter)
                      : Add 1 to counter
sub->-> counter = 3   : Set counter to 0


This is the only thing that works and set the animation of each instance to be different but it is not exactly what I want.

Anyone could help me please how to set random animation for each instance?

Thanks.
B
4
S
1
Posts: 28
Reputation: 406

Post » Mon Apr 17, 2017 1:14 pm

First you might want to use str(floor(random(3)) otherwise you won't be getting a whole number and you would never get animation "2". Alternatively, use choose("0","1","2")

Also, have you tired separating out the set animation into a Create object event?
e.g:

On start of layout
>repeat 10 times: create object X

On object X created
>set animation to str(floor(random (3))

Using the "On created" can get around a bunch of other problems that can arise when trying to set variables and states of created objects immediately after they've been created but before they've been drawn.
B
20
S
8
G
3
Posts: 458
Reputation: 4,055

Post » Mon Apr 17, 2017 1:39 pm

Thanks a lot it worked :)
The "On created" event and the floor() expression sort of worked but for some reason the floor(random()) expression always returned only 2 of 3 possible values like it was returning only 0 and 1 or only 1 and 2 but never 0,1 and 2... but the choose() expression just solved all the problems :)
B
4
S
1
Posts: 28
Reputation: 406

Post » Mon Apr 17, 2017 1:48 pm

Glad to have helped :)
B
20
S
8
G
3
Posts: 458
Reputation: 4,055


Return to How do I....?

Who is online

Users browsing this forum: Blendi1223 and 20 guests