GML Help with rotating sword attack

C

ChrisEXEL

Guest
I've been trying for a while and I can't figure out a way to "animate" a sword swing attack. What I want is for the sword to swing down really fast and then return to its previous state every time the left mouse button is pressed. I want this to be able to happen at any angle the sword is at and have it swing for the same amount every time. Sword movement example

This is the code I used to set up the default movement of the sword around the player

Create event
GML:
dis = 11;  // Distance from the player to the cursor
Step event
GML:
dir = point_direction(obj_Player.x, obj_Player.y, mouse_x, mouse_y);   // Cursor pointing at mouse coords

// Clamp the direction (if you want the cursor to rotate only within a specific range).
   //dir = clamp(dir, 0, 180);

  // Calculate x and y coords for the cursor
   x = obj_Player.x + lengthdir_x(dis, dir);
   y = obj_Player.y + lengthdir_y(dis, dir);

   // Rotate the cursor sprite
   image_angle = dir;
  
///change player sprite
if obj_Player.x < mouse_x
{
image_xscale = 1;
image_yscale = 1;
}
else
{
//image_xscale = -1;
image_yscale = -1;
}
This is how I imagine it should look
attack example.png
 

Xer0botXer0

Senpai
Create Event:

Code:
boostx = 0;
boosty = 0;
dis = 11;  // Distance from the player to the cursor

Step Event:

GML:
if mouse_check_button_pressed(mb_left)
{
    if image_yscale == -1
    {
        boostx = mouse_x + 50;
        boosty = mouse_y + 50;
    }else
    {
        boostx = mouse_x - 50;
        boosty = mouse_y - 50;
    }
    dir = point_direction(obj_Player.x, obj_Player.y,boostx,boosty );
    // Calculate x and y coords for the cursor
    x = obj_Player.x + lengthdir_x(dis, dir);
    y = obj_Player.y + lengthdir_y(dis, dir);

    // Rotate the cursor sprite
    image_angle = dir;
}

else if mouse_check_button_released(mb_left)
{
    if image_yscale == -1
    {
    boostx -= 50;
    boosty -= 50;
    }
    else
    {
        boostx += 50;
        boosty += 50;
    }
    dir = point_direction(obj_Player.x, obj_Player.y,boostx,boosty );
    // Calculate x and y coords for the cursor
    x = obj_Player.x + lengthdir_x(dis, dir);
    y = obj_Player.y + lengthdir_y(dis, dir);

    // Rotate the cursor sprite
    image_angle = dir;  
}
else
{
    dir = point_direction(obj_Player.x, obj_Player.y, mouse_x, mouse_y);   // Cursor pointing at mouse coords
    // Calculate x and y coords for the cursor
    x = obj_Player.x + lengthdir_x(dis, dir);
    y = obj_Player.y + lengthdir_y(dis, dir);

    // Rotate the cursor sprite
    image_angle = dir;  
}
 
Last edited:
C

ChrisEXEL

Guest
GML:
if mouse_check_button_pressed(mb_left)
{
if image_yscale == -1
{
var boostx = mouse_x + 50;
var boosty = mouse_y + 50;
}else
{
var boostx = mouse_x - 50;
var boosty = mouse_y - 50;
}

dir = point_direction(obj_Player.x, obj_Player.y,boostx,boosty );
}
else if mouse_check_button_released(mb_left)
{
if image_yscale == -1
{
boostx -= 50;
boosty -= 50;
}
else
{
boostx += 50;
boosty += 50;
}
dir = point_direction(obj_Player.x, obj_Player.y,boostx,boosty );
}
else
{
dir = point_direction(obj_Player.x, obj_Player.y, mouse_x, mouse_y);   // Cursor pointing at mouse coords
}
Probably not what you're looking for.
I get an error every time I try to run it, it has something to do with the variables. I'm putting this in the step event under all of my other code right?
 
C

ChrisEXEL

Guest
Uhh.. I'll update it again, you probably need more of the old code in there for it to do anything.
It just moves the sword in different directions for like a frame then goes back, I appreciate you trying to help but I might just have to figure something else out because this thing seems like its never gonna work
 
P

ParodyKnaveBob

Guest
If you're not using something like Spine, why not just animate the sword swinging as a sprite? $:^ ]
 
Top