Movement Question

M

Mr.Meow.Games

Guest
I have a problem with the movement code in a top-view rpg I am working on, when the player presses up and right/left or down and left/right the player begins moving much much faster also the player will continue moving in the right-up direction even when i release one of the keys, I would like help on both of theses problems sorry for sounding like a pest, Here is my player move code:
///Platform Physics
var rkey = keyboard_check(vk_right)
var ukey = keyboard_check(vk_up)
var lkey = keyboard_check(vk_left)
var dkey = keyboard_check(vk_down)


//Moving Up
if (ukey) {
vspd = -spd
}
//Moving Down
if (dkey) {
vspd = spd
}
//Moving to the right
if (rkey) {
hspd=spd
}
//Moving Left
if (lkey) {
hspd = -spd
}
//Check for not moving
if ((!rkey && !lkey && !ukey && !dkey) || (rkey && lkey) || (ukey && dkey)) {
hspd=0
vspd = 0
}

//horizontal collisions
if (place_meeting(x+hspd, y, obj_wall)) {
while (!place_meeting(x+sign(hspd), y, obj_wall)) {
x+= sign(hspd)
}
hspd=0
}
//Move Horizontally
x+=hspd

//Vertiacal collisions
if (place_meeting(x, y+vspd, obj_wall)) {
while (!place_meeting(x, y+sign(vspd), obj_wall)) {
y+= sign(vspd)
}
vspd=0
}
//Move Verticaally
y+=vspd

//control sprites
if(yprevious != y) {
sprite_index= spr_player
image_speed= 0
image_index = y>yprevious
}else{
if (xprevious != x) {
sprite_index=spr_player_walk
image_speed=.2
}else{
sprite_index=spr_player
}
}

//control direction
if(xprevious < x) {
image_xscale=1
}else if (xprevious > x) {
image_xscale=-1
}
 

bojack29

Member
A great way of moving in a top down game is as such

Code:
x += (keyboard_check(vk_right) - keyboard_check(vk_left)) * yourSpeed;

y += (keyboard_check(vk_down) - keyboard_check(vk_up)) * yourSpeed;
 

Still57

Member
Your object will move faster when travelling diagonally because you are manually changing the position of the object one step at a time both on the x axis and on the y axis. If your variable 'spd' is equal to 5 and you move diagonally your speed will be more than 5. It will be 5sqrt(2) in your case. Seeing that you are 15 years old (as it says on your profile) you should have learned the Pythagorean theorem by now. If you haven't though, don't worry it's really easy. Check out this link. If you read the introduction and take a look at the diagram on the right of it you'll be just fine.

also the player will continue moving in the right-up direction even when i release one of the keys
I don't know exactly what you mean by "even when I release one of the keys". Are you pressing any keys or not?
 
Top