Problem with making a moving attack [SOLVED]

Z

zecton

Guest
Hello there, i have tried to solve the following problem:
I tried to make a moving attack but not holding the key, so i used the variable key_attack = keyboard_check_pressed (ord("Z")) to make this movement and i wrote the following statement;

Code:
 if key_attack 
        {
            seconds_attack   =  90;
            if seconds_attack   > 0
            {
                seconds_attack  +  =   -1;
                if image_xscale   =   1
                {
                    movespeed =  3 ;
                }
                if image_xscale=-1
                {

                    movespeed  = -3 ; 
                }
            }
            if seconds_attack = 0
            {
                hsp = movespeed = 2;
            }
        }
But it´s not working. If i press "z" , the player just move one or two pixels. I also tried alarms, "while" and "switch" command and splitted the code in cases, but the problem was not solved.

So that´s the problem, i´m looking for a way to make an attack without holding the key and then execute the code for 1.5 seconds (90 frames). i.e. in wario land 4
 
Z

zecton

Guest
excuse me, the "hsp = movespeed = 2 ;" thing is a mistake, that´s "movespeed = 2;" , but still not working
 

chamaeleon

Member
So that´s the problem, i´m looking for a way to make an attack without holding the key and then execute the code for 1.5 seconds (90 frames)/QUOTE]
So use the input management to set flags and time or frame-counter variables (unless you set an alarm) appropriately that you check in the step event to indicate whether you are currently executing such an attack, and perform the action required to make the change for one step. Once the elapsed time or number of frames has been exceeded reset necessary flags (or do it in an alarm), and that part of your code will no longer execute. In any case, the step event should simply react to whether you are in the state of performing this act or not, and if not, perform normal movement. Your input command handling should set the state for you, and unless you intend to override the action by having another command take over, it's all it needs to do.
 

samspade

Member
That is some strange code. There's a couple things you could do to improve it.

First, while not required, you might want to enforce some more rigorous formatting rules on yourself. Redoing your code with more standard formatting (using brackets around if statements, using = or == when appropriate, and using more consistent spacing) makes it easier to distinguish what is what in your code, and helps to show what the problem is.

Code:
 if (key_attack)
       {
           seconds_attack =  90;
           if (seconds_attack > 0)
           {
               seconds_attack += -1;
               if (image_xscale == 1)
               {
                   movespeed = 3;
               }
               if (image_xscale == -1)
               {
                   movespeed  = -3;
               }
           }
           if (seconds_attack == 0)
           {
               hsp = movespeed == 2;
           }
       }

Second, I assume that hsp = movespeed == 2 is not actually what you mean either? It isn't technically incorrect, but you're setting the value of hsp to true or false because you're comparing movespeed to 2 (this is part of why it is important to use = or == as appropriate even though GML doesn't require it). Since true and false are just 1 and 0 in GML, you're setting hsp to 1 or 0.

Third you're doing == comparisons with numbers, something you generally want to avoid. The image_xscale comparisons are probably okay, but seconds_attack should be <= 0. You could also improve your movespeed calculation by doing movespeed = 3 * sign(image_xscale). This gets rid of the comparison problem and also turns the two if statements into a single line.

However, the biggest issue is your if statements themselves. Everything is cancelling everything else out. You set seconds_attack to 90 every step, so seconds_attack += -1 does no good because it is reset every step. You set movespeed, but then you set hsp to a true false variable rather than to movespeed.

Probably you want something like this:

Code:
if (key_attack) {
    seconds_attack = 90;
    movespeed = 3 * sign(image_xscale);
}

if (seconds_attack > 0) {
    seconds_attack -= 1;
    if (seconds_attack <= 0) {
        movespeed = 2 * sign(image_xscale);
    }
}

hsp = movespeed;
 
Z

zecton

Guest
Chamaeleon and samspade, thank you so much, due to your suggests i performed my code like this;

Code:
if (key_attack)   &&  (seconds_attack  <= 0 )
{
    seconds_attack = 90; 
}

if (seconds_attack > 0)
{
    seconds_attack += -1;
    hsp = 3 * sign (image_xscale);
  
    if seconds_attack <=0
    {
        hsp = move * movespeed ;
    }
}
Then, in the first frame i press the key, then seconds_attack restart to 90 frames (1.5 seconds because i´m playing the game with 60fps) so each frame the seconds_attack are reduced in 1 second because the code is in a step event ,then the code is being executed in each frame.

I also used the "if (key_attack) && (seconds_attack <= 0 )" restriction because the seconds restriction make you as a player to not restart the seconds while is counting.

In this case, if i press the button, the movespeed (in normal state is 2) is replaced by 3.

Sorry too for my bad typing Samspade, I´m new in this stuff.

I´m going to put the thread like solved if i know how to do!
 
Last edited by a moderator:
Top