Legacy GM Changing Sprites: How do I set an animation?

Dr_Nomz

Member
I want to have a normal sprite on a character for most of the time, until they start an attack. Once they do, their sprite changes to a different sprite (attack animation) and it'll stay that way until the sprite animation is over.

I also want to keep this all in the DRAW event if possible. How do I this?
 

TheouAegis

Member
if image_index + image_speed >= image_number {
//go back to idle or whatever if attacking
//or set image speed to 0 if jumping if you want
//and don't do anything if running or idle so it loops on its own
}

Note: This does effectively reduce 1 step from the animation. If you be sure to draw the sprite before calling this code, then you effectively saved the step and the player will see the full animation.
 
Use states - there are loads of tutorials for these if you go look for them.
Basically you would change to the attacking state and switch the sprite to be your attack sprite. The attack state would only ever switch back to the idle/normal sprite and state when the attack animation ends (you can use the end animation event and just check what the sprite is in there).
Why do you want to do everything in the Draw Event? That seems counter-intuitive as you should be trying to keep as much logic out of the Draw Event, and you don't need to use it as there are other events better suited to doing the job.
 

Dr_Nomz

Member
Okay but how do I actually CHANGE the sprite? I can't find the command, or what I'm doing just isn't working for some reason.
 
Okay but how do I actually CHANGE the sprite? I can't find the command, or what I'm doing just isn't working for some reason.
Code:
sprite_index = <name of new sprite>
Simple as that.
From the manual:
sprite_index
This is the index of the current sprite for the instance.
Description
This variable returns the index of the current sprite for the instance, or -1 if the instance has no sprite associated with it. You can change it to give the instance a different sprite by giving it the name of a sprite from the resource tree or by using a variable that has an externally loaded sprite indexed in it. Changing the sprite does not change the index of the currently visible sub-image, so if you change the sprite on sub-image number 3, the new sprite will be drawn with that sub-image visible (assuming it has the same number of sub-images).


Example:
with (obj_Check)
{
if !collision_line(x, y, other.x, other.y, obj_Wall, false, true)
{
sprite_index = spr_spotted;
}
else
{
sprite_index = spr_clear;
}
}

The above code will loop through all instances of "obj_Check" checking for a collision line between them and the instance running the code. The sprite of those instances will be changed depending on the return value (true or false) for the collision line.
http://docs.yoyogames.com/index.htm...stances/instance properties/sprite_index.html
 

Dr_Nomz

Member
Yeah I figured that out with some testing, also helped that I changed it to "sprite" index from "image" index. >_<

I just left that question in case there was a better/different way of doing it, but what I have seems fine. I'll leave it open in case I need more help though.
 
R

robproctor83

Guest
Here is a simple and easy way to use states, though you will obviously need to elaborate on the code more, but it should give you the idea.

Code:
//Create Event
enum States {
    Idle,
    Walk,
    Attack,
    Die
}

state = States.Idle;

//Step Event
if(<keypress> && state != States.Attack){
    state = States.Attack;
}
...

//Draw || Step Event
switch (state){
    case States.Idle:
        swap_sprite(<sprite_idle>);
        break;
    case States.Walk:
        swap_sprite(<sprite_walk>);
        break;
    case States.Attack:
        swap_sprite(<sprite_attack>);
        break;
    case States.Die:
        swap_sprite(<sprite_die>);
        break;
}

//Script sprite_swap(<sprite_index>)
if(sprite_index != argument[0]){
    sprite_index = <spr_idle>;
    image_index = 0; //reset animation to beginning
}
 
Top