Jump to content


Photo

Holding 'Shift' to slow down


  • Please log in to reply
7 replies to this topic

#1 Drydareelin

Drydareelin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 22 June 2012 - 10:42 AM

Hey,

I am making a Top-Down shooter for my brother. Having not made any games in ages, I thought it would be an idea to go into more in-depth than I previously did.
I've set up the simple W,A,S,D movement and pointing towards the mouse in code, but no matter how hard I try, I can't seem to implement a 'slowing' feature where if you hold shift, the character stops running and goes into a 'stable' or 'slow' mode where it's able shoot again.

The 'Step'/Movement code I have at the moment is:
// Face Mouse ++++++++++++++++
moving = false
image_angle = direction;
move_towards_point(mouse_x,mouse_y,0);
// Movement ++++++++++++++++++
    if keyboard_check(ord("A")) //Move with A key
{
x-=5
sprite_index = spr_pitaboo_running
moving = true
}
    if keyboard_check(ord("D"))
{
x+=5
sprite_index = spr_pitaboo_running
moving = true
}
    if keyboard_check(ord("W"))
{
move_towards_point(mouse_x,mouse_y,5)
sprite_index = spr_pitaboo_running
moving = true
}
    if keyboard_check(ord("S"))
{
move_towards_point(mouse_x,mouse_y,-2)
sprite_index = spr_pitaboo_shooting
moving = false
}
// Image Speed ++++++++++++++++++++++++
    if speed=0
{
image_speed=0
image_index=0
}
    if speed>0
{
image_speed=1
}

Can anyone help me here?
I have Game Maker 8.0 pro, if you haven't already worked that out.

Thanks,
Dan

Edited by Nightmax, 22 June 2012 - 12:36 PM.

  • 0

#2 Yumegiwa

Yumegiwa

    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 22 June 2012 - 11:05 AM

The function you're using, move_towards_point(), sets the object's hspeed and vspeed values according to the arguments you give it. To slow down or stop the character, you have to manually control the "speed" variable, or simply decrease the third argument of move_towards_point() when the shift key is held.
  • 1

#3 Yumegiwa

Yumegiwa

    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 22 June 2012 - 11:28 AM

This is a simple definition of move_towards_point(x,y,speed). If you write your own script and replace move_towards_point() with the following contents, it does the exact same thing.
direction = point_direction(x,y,argument0,argument1);
speed = argument2;

More in depth version of move_towards_point(), with minimal pre-defined functions:
x_norm = argument0-x;
y_norm = argument1-y;
angle = 2*arctan( y_norm / ( sqrt( power(x_norm,2) + power(y_norm,2) ) + x_norm ) );
hspeed = argument2*cos(angle);
vspeed = argument2*sin(angle);

  • 1

#4 Drydareelin

Drydareelin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 22 June 2012 - 11:45 AM

This is a lot more confusing that I thought it would be O.o
My code was in the 'Step' even of the player, not too sure where the script comes into it

Sorry :/ Do you mean me to create a new script, then replace the 'move_towards_point()' with the 'script_execute()' function?
  • 0

#5 Yumegiwa

Yumegiwa

    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 22 June 2012 - 11:58 AM

This is a lot more confusing that I thought it would be O.o
My code was in the 'Step' even of the player, not too sure where the script comes into it

Sorry :/ Do you mean me to create a new script, then replace the 'move_towards_point()' with the 'script_execute()' function?


I'm just trying to get you to understand exactly what move_towards_point() does.

Every object has hspeed (horizontal speed) and vspeed (vertical speed) variables. If these variables are not zero, the object automatically moves on its own each step.

For example, if you set in the create event of an object:
hspeed = 5;
vspeed = 0;
The object will automatically move right 5 pixels every frame, even if you have nothing in the step event.

The move_towards_point() function merely sets these values. To stop an object after it has been set in motion with this function, you can either say somewhere:
speed = 0;
or
move_towards_point(x,y,0);

In other words, to stop an object, you need to tell it to have zero speed. The "speed" variable is just an extension of hspeed and vspeed that sets the overall speed. In fact, the speed and direction variables can be found from the hspeed and vspeed variables and vice a versa.
  • 2

#6 Drydareelin

Drydareelin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 22 June 2012 - 12:19 PM

Ah okay :) I get you!
Thanks for your help, I will mess around with the speeds and see what that accomplishes.
  • 0

#7 Yumegiwa

Yumegiwa

    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 22 June 2012 - 12:29 PM

You want something like this, right?

ADD TO CREATE EVENT:
moveSpeed = 0;

STEP EVENT:
moving = false;
direction = point_direction(x,y,mouse_x,mouse_y);
image_angle = direction;
speed = 0;

if (keyboard_check(vk_shift)) 
{
  // create bullet object here
  moveSpeed = 2;
}
else {
  moveSpeed = 5;
}
if (keyboard_check(ord("A"))) 
{
  sprite_index = spr_pitaboo_running;
  moving = true;
  x += moveSpeed*(cos((direction+90)*pi/180));
  y -= moveSpeed*(sin((direction+90)*pi/180));
}
if (keyboard_check(ord("D")))
{
  sprite_index = spr_pitaboo_running;
  moving = true;
  x += moveSpeed*(cos((direction-90)*pi/180));
  y -= moveSpeed*(sin((direction-90)*pi/180));
}
if (keyboard_check(ord("W")))
{
  move_towards_point(mouse_x,mouse_y,moveSpeed);
  sprite_index = spr_pitaboo_running;
  moving = true;
}
if (keyboard_check(ord("S")))
{
  move_towards_point(mouse_x,mouse_y,-2);
  sprite_index = spr_pitaboo_shooting;
  moving = false;
}

if (speed=0)
{
  image_speed = 0;
  image_index = 0;
}
if (speed>0)
{
  image_speed = 1;
}

  • 2

#8 Drydareelin

Drydareelin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 22 June 2012 - 12:36 PM

Oh wow that did it perfectly!

Sorry for seeming like a complete idiot, it's been near a year since I've last used this program
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users