GMS 2 Need Some help with a unique melee attack

Discussion in 'Programming' started by Marzz19, Jan 12, 2020.

  1. Marzz19

    Marzz19 Member

    Joined:
    Jan 12, 2020
    Posts:
    3
    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
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,367
    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_.
     
  3. Marzz19

    Marzz19 Member

    Joined:
    Jan 12, 2020
    Posts:
    3
    Thanks for the help, and I’m guessing it is the same in reverse
     
  4. Marzz19

    Marzz19 Member

    Joined:
    Jan 12, 2020
    Posts:
    3
    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?
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,367
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice