# Need Help With Movement Code

10 replies to this topic

### #1 Insanoflex

Insanoflex

GMC Member

• New Member
• 86 posts

Posted 02 May 2009 - 01:13 AM

I am making a platform game and working on the movement codes for the player. My code was working fine until I added the vertical movement code and wall collision event. Code is as follows:
Step Event:[codebox]//movement codes
//hortizontal movement
spd += ((keyboard_check(vk_left))/5)+((keyboard_check(vk_right))/5);
if spd >= 8
{spd = 8}
if keyboard_check(vk_left)+keyboard_check(vk_right) == 0 || keyboard_check(vk_left)+keyboard_check(vk_right) == 2
{spd = 3}
hspeed = (0-((keyboard_check(vk_left))*spd)+(keyboard_check(vk
_right))*spd)*(place_free(x-1,y)+place_free(x+1,y));
//vertical movement
gravity = (place_free(x,y-4))*4;
gravity_direction = 270;
can_jump = !place_free(x,y-4);
if can_jump == 1
{vspeed = (!place_free(x,y-4))*5*(keyboard_check(vk_space));}
if vspeed >= 12
{vspeed = 12;}
if (keyboard_check(vk_space) == 1)
{alarm[0] = 30;}
[/codebox]
For the collision event with a wall object all I have is:[codebox]move_contact_solid(direction,4);[/codebox]
The result is that the player object doesn't move when left arrow, right arrow or, space is pressed.
• 0

### #2 thundybear

thundybear

GMC Member

• New Member
• 390 posts

Posted 02 May 2009 - 01:45 AM

keyboard_check is borlean, hence returns true or false, not a value. So you can't subtrace 5 from true. Just use hspeed and vspeed.
• 0

### #3 snowyowl

snowyowl

GMC Member

• New Member
• 615 posts

Posted 02 May 2009 - 01:50 AM

No, you can: boolean values are represented by the numbers 1 and 0 respectively for true and false. I consider using this shortcut too confusing, but it does work.

I haven't managed to work out all your code yet, but I suspect the errors are down to the fact that your character has bothe horizontal and vertical speed; so using the speed and direction variables will lead to errors. Try finding different ways around the problem, for example by making the character have no horizontal speed; instead simply resetting the x-coordinate whenever you move.
Does this help?

Edited by snowyowl, 02 May 2009 - 01:50 AM.

• 0

### #4 Insanoflex

Insanoflex

GMC Member

• New Member
• 86 posts

Posted 02 May 2009 - 02:24 AM

I changed the hspeed line to:[codebox]move_towards_point((keyboard_check(vk_
left))+(0-(keyboard_check(vk_right))),y,spd);
[/codebox]
but still nothing. It still gives me the same problem.

Edited by Insanoflex, 02 May 2009 - 02:24 AM.

• 0

### #5 DavidM

DavidM

GMC Member

• New Member
• 4 posts

Posted 02 May 2009 - 06:32 AM

I changed the hspeed line to:

move_towards_point((keyboard_check(vk_left))+(0-(keyboard_check(vk_right))),y,spd);
but still nothing. It still gives me the same problem.

move_towards_point(x-(keyboard_check(vk_left)+(0-(keyboard_check(vk_right)))),y,spd);

___________________________________________

New to Game maker
• 0

### #6 Desert Dog

Desert Dog

GMC Member

• Retired Staff
• 6409 posts
• Version:Unknown

Posted 02 May 2009 - 08:20 AM

You already stated there was nothing wrong with your left/right movement.. it's just the vertical movement which started causing problems.

What does alarm[0] have in it? You realise that if you hold down space, then alarm[0] will constantly be set to 30, too, right?

~DD
• 0

### #7 Insanoflex

Insanoflex

GMC Member

• New Member
• 86 posts

Posted 02 May 2009 - 10:04 PM

You already stated there was nothing wrong with your left/right movement.. it's just the vertical movement which started causing problems.

What does alarm[0] have in it? You realise that if you hold down space, then alarm[0] will constantly be set to 30, too, right?

~DD

Well, yes the hortizontal movement code was "fine" until I added the vertical movement, I figured there was something wrong with it bc the player doesnt move at all now. Alarm[0] has "can_jump = 1". After replacing the new hort. code with the old, and deleting the vert movement code, I can move left and right again. Ill rewrite the vert. code and post an update. In the meantime, if anybody knows why my code was wrong, Id appreciate any help.
• 0

### #8 Desert Dog

Desert Dog

GMC Member

• Retired Staff
• 6409 posts
• Version:Unknown

Posted 04 May 2009 - 12:14 AM

{vspeed = (!place_free(x,y-4))*5*(keyboard_check(vk_space));}

Okay, I don't like this code at all.

1: !place_free(x,y-4) is ABOVE the player. I'm pretty sure you wanted to check if it was below the player.

2: Even if that check was !place_free(x,y+4), it wouldn't be a good jumping code. The reason? As soon as the play leaves the ground, vspeed is set to 0. Not exactly the greatest jumping, eh?

3: A positive vspeed value? That is moving DOWNward. After reading all this, I'm beginning to wonder if your game isn't inverted...

I don't know if this is why the code as packed up completely, but I really don't like the use of binary values in which you've done here for jumping. Just use good 'ol if, else statements.

if keyboard_check(vk_spacebar) // first check if he pressed space bar
{
if !place_free(x,y+1) // y+1 is just BELOW
{
vspeed=-5; // -6 is UPward movement.
}
}

I hope this helps you. I am unsure if it will fix your problem, but I DO think that your jumping code is seriously out of wack.

Good luck!

~DD

Edited by Desert Dog, 04 May 2009 - 12:15 AM.

• 0

### #9 Insanoflex

Insanoflex

GMC Member

• New Member
• 86 posts

Posted 04 May 2009 - 09:34 PM

That did help, I wasn't aware the Y-axis was inverted. Thanks everyone for the replies.
EDIT: update When the player is in the air and either left or right arrow is pressed, the player slows vspeed greatly. The player is also getting stuck on walls.
Step Event:
[codebox]//movement codes
//hortizontal movement
spd += ((keyboard_check(vk_left))/5)+((keyboard_check(vk_right))/5);
if spd >= 5
{spd = 5;}
if keyboard_check(vk_left)+keyboard_check(vk_right) == 0 || keyboard_check(vk_left)+keyboard_check(vk_right) == 2
{spd = 3;}
if keyboard_check(vk_left)
{
if place_free(x-4,y)
{move_towards_point(x-4,y,spd)}
}
if keyboard_check(vk_right)
{
if place_free(x+4,y)
{move_towards_point(x+4,y,spd)}
}
//vertical movement
gravity_direction = 270;
gravity = (place_free(x,y+1))/2;
if keyboard_check(vk_space)
{
if !place_free(x,y+1)
{
vspeed=-10;
}
}[/codebox]
collision with block object is: move_contact_solid(direction,12) and vspeed=0; which should work since its straight from the tutorial.

Edited by Insanoflex, 04 May 2009 - 10:06 PM.

• 0

### #10 Desert Dog

Desert Dog

GMC Member

• Retired Staff
• 6409 posts
• Version:Unknown

Posted 04 May 2009 - 11:46 PM

That's because..

{move_towards_point(x-4,y,spd)}

move_towards_point uses speed, no matter what variable you put in there. you put spd, but essentially all your doing is setting speed=spd;

And on another note, what if spd is larger than 4? then it will zoom right into the block (causing problems, eh?)

if keyboard_check(vk_right)
{
if place_free(x+spd,y)
{x+=spd}
}

Much better! And it doesn't use speed, so it won't effect the value of vspeed. (which is a vector value of speed. Just speed in the direction of 90 )

Hope this helps,

~DD
• 0

### #11 Insanoflex

Insanoflex

GMC Member

• New Member
• 86 posts

Posted 05 May 2009 - 03:04 AM

Thanks for the help! It seems to work the way it is supposed to now.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users