• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Animation after key pressed and placed instance of another sprite....need help

xxpilotxx

Member
Here's the code:
GML:
if(firstTime && keyboard_check_pressed(ord("3"))){
  
    image_index = PlanterDudeAnimation;
  
    for(i = 0; i <= sprite_get_number(PlanterDudeAnimation); i += .1){
        if (image_index >= PlanterDudeAnimation.image_number){
image_speed = 0;
        }
    image_index +=i;
    timer += i;
                }
                firstTime = false;
                haveNotAnimd = false;
                timer++;
                instance_create_layer(x, y -15, "Instances", Leaf);
                keyboard_clear(ord("3"));
            }
    else if(keyboard_check_pressed(ord("3")) && timer > 240 && haveNotAnimd && firstTime = false){
    sprite_index = PlanterDudeAnimation;
for(i = 0; i <= sprite_get_number(PlanterDudeAnimation); i += .1){
    if (image_index + >=  PlanterDudeAnimation.image_number){
image_speed = 0;
        }
    image_index +=i;
                }
                haveNotAnimd = false;
                timer = 0;
                instance_create_layer(x, y -15, "Instances", Leaf);
                keyboard_clear(ord("3"));
    }

else if(timer >= 480 && keyboard_check_pressed(ord("3")) && firstTime = false){
sprite_index = PlanterDude;
for(i = 0; i <= sprite_get_number(PlanterDudeAnimation); i += .1){
    if (image_index >=  PlanterDudeAnimation.image_number){
image_speed = 0;
        }
    image_index +=i;
                }
                haveNotAnimd = false;
                timer = 0;
                instance_create_layer(x,y-15,"Instances", treeSpecial);
                keyboard_clear(ord("3"));
        }
 
Last edited:

xxpilotxx

Member
well the code doesn't do what i want it to. so i'm asking how to fix it to get the result i'm wishing for. Currently the code doesn't do anything and it's supposed to play an animation and leave an instance of a leaf behind.
 
Last edited:

chamaeleon

Member
A valid assignment if PlanterDudeAnimation is an ordinary number indicating frame 0 through N:
GML:
image_index = PlanterDudeAnimation;
A valid instance variable reference if PlanterDudeAnimation is an instance:
GML:
PlanterDudeAnimation.image_number
A valid assignment if PlanterDudeAnimation is a sprite:
GML:
sprite_index = PlanterDudeAnimation;
PlanterDudeAnimation can't be an ordinary number, an instance, and a sprite, at the same time.
 

xxpilotxx

Member
GML:
if(keyboard_check_pressed(ord("3"))){
        planting = true;
    
    sprite_index = PlanterDudeAnimation;
    PlanterDudeAnimation.image_speed = 8;
        
    for(i = 0; i < 480; i ++){
        continue;
    }
keyboard_clear(ord("3"));
 var leaf = instance_create_layer(x, y -15, "Instances", Leaf);
}
I simplified the code and it still doesn't work.
 

FrostyCat

Redemption Seeker
You have to learn the difference between an iteration in a loop and a frame step. NEVER try to do things over time with a closed loop.

Create:
GML:
planting_steps = 0;
sprite_index = PlanterDudeNormal;
image_speed = 0;
Step:
GML:
if (planting_steps) {
    if (--planting_steps <= 0) {
        instance_create_layer(x, y -15, "Instances", Leaf);
        sprite_index = PlanterDudeNormal;
        image_speed = 0;
    }
} else if (keyboard_check_pressed(ord("3"))) {
    sprite_index = PlanterDudeAnimation;
    image_speed = 1;
    planting_steps = 480;
}
In the long run, ideally you should learn to use the state machine pattern for things like this. You would transition between the normal and planting states, and each state has its own associated per-step action that happens over time. The normal state listens for the key press, and transitions to the planting state with the timer set when the key is pressed. The planting state counts the timer down by one step, and if it reaches 0, it creates a plant and transitions back to the normal state.
 

xxpilotxx

Member
Your code works flawlessly for the animation part but i cannot seem to get the leaf to spawn.....Leaf is the name of the object.

Also i added
keyboard_clear(ord("3"));
to the first part....so that the key would reset.
 
Top