GMS 2 Need Some help with a unique melee attack

Marzz19

Member
Im trying to make an attack where a spear move forward then backwards, aiming towards the mouse. the spear is a separate object to the player, it is meant to move towards the mouse then once it has move a set distance return back to the player similar to shaun spaulding's pokey poke melee attack. I am quite new and any help would be appreciated. Here is my code:


var max_extension_x = initial_x + lengthdir_x(extension_,dir_);
var max_extension_y = initial_y + lengthdir_y(extension_,dir_);
if x = max_extension_x and y = max_extension_y {
STATE_ = MELEE_RETURN_
} else {
x += lengthdir_x(extensionsp_, dir_)
y += lengthdir_y(extensionsp_, dir_)
}

I would also appreciate some help on getting it to return to the player. The spear just flys off. Thanks
 

TheouAegis

Member
Floating-point errors. When you attack, just set xstart to the current x and ystart to the current y, then send the spear out. Then check every step if the distance from x, y to xstart, ystart is greater than extension_.
 

Marzz19

Member
OK tried ur suggestion, same thing happens
if x > initial_x + extension_ and y > initial_y + extension_ {
initial_x = x
initial_y = y
STATE_ = MELEE_RETURN_
} else {
x += lengthdir_x(extensionsp_, dir_)
y += lengthdir_y(extensionsp_, dir_)
}

anymore help?
 

TheouAegis

Member
On Attack:
Code:
xstart = x;
ystart = y;
ldx = lengthdir_x(extensionsp_,dir_);
ldy = lengthdir_y(extensionsp_,dir_);
Step event:
Code:
if sign(ldx) | sign(ldy) != 0 {
    var d = point_distance(x,y,xstart,ystart)
    if d >= extension {
        ldx = -ldx;
        ldy = -ldy;
        STATE_ = MELEE_RETURN_;
    }
    else
    if d < extensionsp_ {
        ldx = 0;
        ldy = 0;
    }   
    x += ldx;
    y += ldy;
}
Try that.
 
Top