GameMaker Solved: Moving in the direction obj is currently facing.

L

Lonster31185

Guest
Alright, so being a dumb dumb, and not really having alll the much experience programming in general....I have a question. I have attempted to find an answer, but the closest I could find was related to using your mouse and moving in that direction...although I may have missed something....

In GM2s help file, looking up switch you easily find how to use a switch statement? to use both arrow keys and WASD for movement. I want something more similar to tank controls, but more movement on a grid, like if you were exploring a first person dungeon crawler (games like Arcana, Eye of the Beholder, Legend of Grimrock 1+2, etc).

So I got the idea from the mouse and most other tutorials to set

image_angle = direction;

and then me in my, this sounds like a good idea! BUT I am not sure what I am doing decides to edit the switch statement to do.... (Bolded what I changed)

{
switch (keyboard_key)
{
case vk_left:
case ord("A"):
direction += 90;
break;

case vk_right:
case ord("D"):
direction -= 90;
break;

case vk_up:
case ord("W"):
y -= 4;
break;

case vk_down:
case ord("S"):
direction += 180;
break;
}}


...basically changing the direction of the image_angle to rotate? 90 degrees...now. So the obj_player would rotate left and right, and if you press down or s, you would rotate and look backwards the direction you were coming from. I now have two questions.

1. I have a horrible feeling its obviously not rotating in the 0-359 degree circle, and that adding 90 and 180 would be moving past that number range. Should I be worried about this. My first thoughts are YES, but I still don't know what I am doing exactly. (Guess I was wrong, it is staying in range. Using draw_text(x, y+256, direction); I threw the text on the screen of direction and it does remain in the 360 degrees >.<;; )


2. What would be the easy way of making W or the Up arrow move the obj_player in the current direction it is facing say 16 or 32 pixels in a grid? And SHOULD I be doing this another way?
 
Last edited by a moderator:
M

maratae

Guest
Either you set the direction to 90, 180, etc, or you += and -= increments, depending on the speed you want the object to turn, like direction += 3..
In there, you're adding like.. 180 degrees each step?

Edit: Also, beware that if you use a switch you can only do one of those things at a time. So no moving and turning.
 
L

Lonster31185

Guest
Either you set the direction to 90, 180, etc, or you += and -= increments, depending on the speed you want the object to turn, like direction += 3..
In there, you're adding like.. 180 degrees each step?

Edit: Also, beware that if you use a switch you can only do one of those things at a time. So no moving and turning.
Mmmm...my rotations are working just fine. The 180 is basically turning the sprite around so its facing its opposite direction, or looking backwards. That works just fine...I just cant seem to figure out how to make pressing the up key move the direction the obj is currently facing.
An example, if obj_player is facing
0 degrees, I want to press the up or W key to make it head right....
while if it was facing 90 degrees then move up...
180 to move left...
and 270 to move down

in the four directions, like a grid. But as for the switch doing one thing at a time, I was wondering why I couldn't move diagonally. That does answer that...but I don't need to move diagonally since I am attempting to do a grid...but still, that does answer that one lol.
 

samspade

Member
Alright, so being a dumb dumb, and not really having alll the much experience programming in general....I have a question. I have attempted to find an answer, but the closest I could find was related to using your mouse and moving in that direction...although I may have missed something....

In GM2s help file, looking up switch you easily find how to use a switch statement? to use both arrow keys and WASD for movement. I want something more similar to tank controls, but more movement on a grid, like if you were exploring a first person dungeon crawler (games like Arcana, Eye of the Beholder, Legend of Grimrock 1+2, etc).

So I got the idea from the mouse and most other tutorials to set

image_angle = direction;

and then me in my, this sounds like a good idea! BUT I am not sure what I am doing decides to edit the switch statement to do.... (Bolded what I changed)

{
switch (keyboard_key)
{
case vk_left:
case ord("A"):
direction += 90;
break;

case vk_right:
case ord("D"):
direction -= 90;
break;

case vk_up:
case ord("W"):
y -= 4;
break;

case vk_down:
case ord("S"):
direction += 180;
break;
}}


...basically changing the direction of the image_angle to rotate? 90 degrees...now. So the obj_player would rotate left and right, and if you press down or s, you would rotate and look backwards the direction you were coming from. I now have two questions.

1. I have a horrible feeling its obviously not rotating in the 0-359 degree circle, and that adding 90 and 180 would be moving past that number range. Should I be worried about this. My first thoughts are YES, but I still don't know what I am doing exactly. (Guess I was wrong, it is staying in range. Using draw_text(x, y+256, direction); I threw the text on the screen of direction and it does remain in the 360 degrees >.<;; )


2. What would be the easy way of making W or the Up arrow move the obj_player in the current direction it is facing say 16 or 32 pixels in a grid? And SHOULD I be doing this another way?
I think you might get the result you want if instead of using keyboard_check_pressed. Instead of keyboard_key. However, I don't know if that would work in this situation as I am also new and haven't used a switch statement for this purpose before. I would do it using if statements, which is more code, but also works:

Code:
if (keyboard_check_pressed(vk_left)) || (keyboard_check_pressed(ord("A")) {
    direction += 90;
}
if (keyboard_check_pressed(vk_right)) || (keyboard_check_pressed(ord("D")) {
    direction -= 90;
}
if (keyboard_check_pressed(vk_down)) || (keyboard_check_pressed(ord("S")) {
    direction += 180;
}

movement_length = // however far you want them to move each time you press up or w

if (keyboard_check_pressed(vk_up)) || (keyboard_check_pressed(ord("W")) {
    x = x + lengthdir_x(movment_length, direction);
    y = y + lengthdir_y(movment_length, direction);
}

image_angle = direction;
I'm not in a place where I can test this, but I believe this will work. I'm a little surprised that your code isn't over spinning your character, but I have never used the keyboard_key function so I don't think I fully understand how it works. If it is working, you might just be able to the following into your code, but I'm not sure.

Code:
case vk_up:
case ord("W"):  
    x = x + lengthdir_x(movment_length, direction);
    y = y + lengthdir_y(movment_length, direction);    
break;
 
L

Lonster31185

Guest
I think you might get the result you want if instead of using keyboard_check_pressed. Instead of keyboard_key. However, I don't know if that would work in this situation as I am also new and haven't used a switch statement for this purpose before. I would do it using if statements, which is more code, but also works:

Code:
if (keyboard_check_pressed(vk_left)) || (keyboard_check_pressed(ord("A")) {
    direction += 90;
}
if (keyboard_check_pressed(vk_right)) || (keyboard_check_pressed(ord("D")) {
    direction -= 90;
}
if (keyboard_check_pressed(vk_down)) || (keyboard_check_pressed(ord("S")) {
    direction += 180;
}

movement_length = // however far you want them to move each time you press up or w

if (keyboard_check_pressed(vk_up)) || (keyboard_check_pressed(ord("W")) {
    x = x + lengthdir_x(movment_length, direction);
    y = y + lengthdir_y(movment_length, direction);
}

image_angle = direction;
I'm not in a place where I can test this, but I believe this will work. I'm a little surprised that your code isn't over spinning your character, but I have never used the keyboard_key function so I don't think I fully understand how it works. If it is working, you might just be able to the following into your code, but I'm not sure.

Code:
case vk_up:
case ord("W"): 
    x = x + lengthdir_x(movment_length, direction);
    y = y + lengthdir_y(movment_length, direction);   
break;
Thank you GOOD SIR! That totally works and fixed my problem as well lol. ^^ You are right though, it was spinning really fast less I typed the key really quick, was not sure if there was a way to delay each..action or movement or such, but your way works much much better! (Did have to add a few ) at the end of each one, but otherwise it worked great! Thanks so much!
 
Top