How do I set animation for each instance?

0 favourites
  • 4 posts
From the Asset Store
3 pixel charaters with over 8 animations each + animations with a gun
  • It seems like my original question has been deleted:

    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:

    On start of layout:
    sub->Repeat 10 times : Create object
                          : Set animation to str(random(2))[/code:8jiwq5dk]
    
    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:8jiwq5dk]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)  [/code:8jiwq5dk]
    
    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:8jiwq5dk]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[/code:8jiwq5dk]
    
    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.
  • 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.

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Glad to have helped

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