GameMaker Starting an animation and stopping it in the middle of the animation.

C

Crysillion

Guest
Essentially, I have a sword and inside the image index of the sword is multiple different swing animations. Frames 0~4 are a swing down, frames 5~8 are a swing up, for example.

I want to make it so when you use the LMB, frames 1, 2, 3 are played, but then it stops there. The next time you press LMB, it plays 5, 6, 7 then back to 0.

Wrapping my head around making it so the weapon starts with an image_speed of 0 and then increases when the player LMB's was easy enough, but having it stop somewhere in the middle is the hard part.

This is what I've tried:

Code:
if (mouse_check_button_pressed(mb_left) && lmbcombo == 0)
{
    image_speed = 30;
    if (image_index == 3) image_speed = 0;
    lmbcombo = 1;
}

if (mouse_check_button_pressed(mb_left) && lmbcombo == 1)
{
    image_speed = 30;
    if (image_index == 7)
    {
        image_index = 0;
        image_speed = 0;
    }
    lmbcombo = 0;
}
When I do LMB, it animates. The problem is that it keeps animating. If I mash LMB, sometimes it will stop the way that it should.

What am I doing wrong?
 

Slyddar

Member
Just looking at the way you are checking image_index, the problem may be because depending on the image_speed you are using, and the room_speed of your game, image_index is not going to be an integer. If you use something like draw_text to show it, you will see it will be a decimal most times, and the integer you want probably never. You will need to make the check between 2 values, and the upper and lower limit will depend on your image_speed/room_speed combination.

For image_index == 3 I would just try 2.8 to 3.2 and if it doesn't work, just adjust till you get what you want.
 
C

Crysillion

Guest
Just looking at the way you are checking image_index, the problem may be because depending on the image_speed you are using, and the room_speed of your game, image_index is not going to be an integer. If you use something like draw_text to show it, you will see it will be a decimal most times, and the integer you want probably never. You will need to make the check between 2 values, and the upper and lower limit will depend on your image_speed/room_speed combination.

For image_index == 3 I would just try 2.8 to 3.2 and if it doesn't work, just adjust till you get what you want.
I've tried a couple things now, taking your advice.

Code:
if (mouse_check_button_pressed(mb_left) && lmbcombo == 0)
{
    image_speed = 30;
    if (image_index >= 2.1) image_speed = 0;
    lmbcombo = 1;
}
Code:
if (mouse_check_button_pressed(mb_left) && lmbcombo == 0)
{
    image_speed = 30;
    if (image_index >= 2.1 || image_index <= 2.9) image_speed = 0;
    lmbcombo = 1;
}
Neither seem to yield results.


EDIT: Okay, I think I know what's happening. It's running the if statement a single time when I do LMB, which is fine in theory, but it does mean that the check for image_index is not true when it runs the first time because it can't be, so it never happens, which is why it sometimes stops if I LMB again.

Now to figure out how to fix it.
 

TheouAegis

Member
Your code shouldn't even be working as it is anyway. You are checking if the mouse is pressed and witch combo you're on and then regardless of whatever image index you are on you are increasing the combo. Furthermore you only check the image index when the mouse is pressed.

Is the mouse pressed? If yes, is the image_speed == 30? If no, set the image_index based on your combo and increase the combo and set image_speed to 30.

Outside that mouse conditional, check if you've reached the end of the animation based on the current combo value then set image_speed to 0. Of ciurse you will need to verify the sprite is correct.
 
C

Crysillion

Guest
Your code shouldn't even be working as it is anyway. You are checking if the mouse is pressed and witch combo you're on and then regardless of whatever image index you are on you are increasing the combo. Furthermore you only check the image index when the mouse is pressed.

Is the mouse pressed? If yes, is the image_speed == 30? If no, set the image_index based on your combo and increase the combo and set image_speed to 30.

Outside that mouse conditional, check if you've reached the end of the animation based on the current combo value then set image_speed to 0. Of ciurse you will need to verify the sprite is correct.
I figured it out, and you're absolutely right. That code was completely wack and made no sense. My solution isn't the most elegant, but it does work:

Code:
if (mouse_check_button_pressed(mb_left))
{
    attacking = 1;
}

if (attacking == 1)
{
    image_speed = 30;
    if (lmbcombo == 1)
    {
       
    }
    else
    if (image_index >= 2.2)
    {
        image_speed = 0;
        image_index = 4;
        lmbcombo = 1;
        attacking = 0;
    }
}
And I put this in the Animation End event:

Code:
if (lmbcombo = 1)
    {
    if (image_index >= 0)
        image_speed = 0;
        image_index = 0;
        lmbcombo = 0;
        attacking = 0;
    }
Definitely feels like I did it in a very roundabout way, but I suppose if it works with no issues then it's acceptable.
 

Slyddar

Member
As I mentioned, I wasn't looking at your logic, but more your use of image_index. Having another look at it, I've tested this and it works fine. I'm in GMS2, so the sprite has a speed of 30, meaning spd = 1 is 30 fps.
Code:
var spd = 1;
if (mouse_check_button_pressed(mb_left))
{
    image_speed = spd;
}

if lmbcombo == 0 and image_speed == spd {
    if image_index >= 4 {
        image_speed = 0;
        lmbcombo = 1;
    }
}

if lmbcombo == 1 and image_speed == spd {
    if image_index >= image_number - 1 {
        image_speed = 0;
        image_index = 0;
        lmbcombo = 0;
    }
}
EDIT: Ah, you've already solved it. Great, glad you got it sorted.
 

Slyddar

Member
Code:
if (lmbcombo = 1)
   {
  if (image_index >= 0)
    image_speed = 0;
    image_index = 0;
    lmbcombo = 0;
    attacking = 0;
}
Just a note, image_index is always going to be >= 0, so probably don't need that check. Are you missing brackets there, or did you want the check to only apply to image_speed = 0?
 
Top