# Am I Doing This Right?

13 replies to this topic

### #1 MrEvil178

MrEvil178

GMC Member

• New Member
• 9 posts

Posted 26 May 2010 - 09:14 PM

Hey I've been trying to learn GML recently and have come across a little problem.

right = vk_right
left = vk_left
jump = vk_up

if !keyboard_check(left) && keyboard_check(right)
{
if place_free(x+hspeed,y) then hspeed = 12 else hspeed = 0
}

if !keyboard_check(right) && keyboard_check(left)
{
if place_free(x-hspeed,y) then hspeed = -12 else hspeed = 0
}

Now here I want it to stop moving when nobody is pressing the keys. However instead the object keeps moving the direction you last pressed it. It seems to ignore the second part of the if statment. If anyone is wondering, yes this is from the SparkEngine I was seeing how I could use it to learn GML.

if !instance_place(x,y+1,WALL)
gravity=0.5
if instance_place(x,y+1,WALL)
gravity=0
if vspeed>10
vspeed=10

(the object WALL is not solid by the way)

The player should not fall through the wall as gravity is turned off when they are near it. Also here, the player falls through the walls as if it ignores the second if statement again. I'm really uncertain what I am doing wrong in both of these instances.

Edited by MrEvil178, 26 May 2010 - 09:15 PM.

• 0

### #2 element_boy6522

element_boy6522

• New Member
• 418 posts

Posted 26 May 2010 - 09:20 PM

To make the player stop:
if keyboard_check_released(left)
or keyboard_check_released(right)
hspeed=0

For proper collision, i dont know a way without solid walls
Why arent they solid?
• 0

### #3 fatmike19931

fatmike19931

GMC Member

• New Member
• 41 posts

Posted 26 May 2010 - 09:20 PM

Dose your character simply fall thougth or slowly float?

Correct me if im wrong, but shouldn't all ground/wall objects be solid?
• 0

### #4 fatmike19931

fatmike19931

GMC Member

• New Member
• 41 posts

Posted 26 May 2010 - 09:24 PM

[codebox]right = vk_right
left = vk_left
jump = vk_up

if !keyboard_check(left) && keyboard_check(right)
{
if place_free(x+hspeed,y) then hspeed = 12 else hspeed = 0
}

if !keyboard_check(right) && keyboard_check(left)
{
if place_free(x-hspeed,y) then hspeed = -12 else hspeed = 0
}[/codebox]

You are never telling it to stop if keys are released, This:

[codebox]if place_free(x+hspeed,y) then hspeed = 12 else hspeed = 0[/codebox] applys only to this line
_

put it like this:

[codebox]
if !keyboard_check(left) && keyboard_check(right)
{
if place_free(x+hspeed,y) then hspeed = 12 else hspeed = 0
}
else
{
hspeed = 0
}[/codebox]

and it should work

ELSE applys to the last IF, because "if place_free(x+hspeed,y) then" is the last IF, it only applys to that

Edited by fatmike19931, 26 May 2010 - 09:28 PM.

• 0

### #5 MrEvil178

MrEvil178

GMC Member

• New Member
• 9 posts

Posted 26 May 2010 - 09:27 PM

To make the player stop:

if keyboard_check_released(left)
or keyboard_check_released(right)
hspeed=0

For proper collision, i dont know a way without solid walls
Why arent they solid?

This code helps because it creates gravity ONLY if there is space under you that doesn't have a solid object.

in the GML Tutorial by General Neo. Ah whoops. Now I get it. That's what I get for reading things too quicky.

Edited by MrEvil178, 26 May 2010 - 09:28 PM.

• 0

### #6 element_boy6522

element_boy6522

• New Member
• 418 posts

Posted 26 May 2010 - 09:30 PM

To make the player stop:

if keyboard_check_released(left)
or keyboard_check_released(right)
hspeed=0

For proper collision, i dont know a way without solid walls
Why arent they solid?

This code helps because it creates gravity ONLY if there is space under you that doesn't have a solid object.

in the GML Tutorial by General Neo. Ah whoops. Now I get it. That's what I get for reading things too quicky.

I guess nobodys reading anything through... Uhmm.. anyways
• 0

### #7 fatmike19931

fatmike19931

GMC Member

• New Member
• 41 posts

Posted 26 May 2010 - 10:32 PM

[codebox]
//Move left&right
//right
if !keyboard_check(left)&&keyboard_check(right)
{
if place_free(x+hspeed,y) then hspeed = 12 else hspeed = 0
}
else if !keyboard_check(left)
{
hspeed = 0
}

//left
if !keyboard_check(right)&&keyboard_check(left)
{
if place_free(x-hspeed,y) then hspeed = -12 else hspeed = 0
}
else if !keyboard_check(right)
{
hspeed = 0
}[/codebox]

Sorry, use this, this WILL work....

Edited by fatmike19931, 26 May 2010 - 10:34 PM.

• 0

### #8 snowyowl

snowyowl

GMC Member

• New Member
• 615 posts

Posted 26 May 2010 - 11:58 PM

As far as I can tell, you never reset vspeed to 0 when you want the player to stop over a block.
• 0

### #9 MrEvil178

MrEvil178

GMC Member

• New Member
• 9 posts

Posted 27 May 2010 - 03:46 PM

Thanks fatmike, that works just fine! I think I understand what the code means (which is what's the most important thing) as well. I'm still having a lot of trouble getting gravity working properly though. The character just falls down through the walls (they are solid). I have used this code:

if place_empty(x,y+1)
{
gravity = 0.5
}
else
{
gravity = 0
}

As well variations on it. Either the character falls through the floor, or doesn't fall at all.
• 0

### #10 YellowAfterlife

YellowAfterlife

GMC Member

• Global Moderators
• 3490 posts
• Version:GM:Studio

Posted 27 May 2010 - 03:49 PM

1. Make sure that walls are solid and have collision sprites.
move_contact_solid(direction,speed);
vspeed = 0;
to collision event of character with wall.
• 0

### #11 Hiddenaces

Hiddenaces

GMC Member

• New Member
• 418 posts

Posted 27 May 2010 - 04:08 PM

it might not help you out with your problem but i was reading the replys and they have not told the hole truth.... you dont have to have solid object for collisions,(just check out this in the novice and intermediate Faq page click here)
• 0

### #12 MrEvil178

MrEvil178

GMC Member

• New Member
• 9 posts

Posted 27 May 2010 - 08:59 PM

Thanks guys. I was reading thiscode and I am pretty sure what most of it means apart from this part:

if (sign(xmove) == 1)
move_contact_solid(0,abs(xmove));

I'm uncertain what value "abs(xmove)" returns. The GM Manual says that the "abs() function returns the absoloute function of x". Does that mean it returns the the x co-ordinant relative to the objects position?
• 0

### #13 fatmike19931

fatmike19931

GMC Member

• New Member
• 41 posts

Posted 29 May 2010 - 01:48 AM

If you are learning then this might not be of any help but here you go anyways...

[codebox]
if (place_free(x+0,y+vspeed+1))
{
{
gravity_direction=270;
gravity=1;
}
}
else
{
gravity_direction=270;
gravity=0;
if !(place_free(x+12,y+13))
{
move_contact_solid(270,-1)
vspeed=0;
}
} [/codebox]

Where you see "!(place_free(x+12,y+13))" fiddle around with the "x+12" and "y+13", I wrote this code a while ago and forgot exactly what the values should be (I know they have something to do with the objects sprite width and height)
• 0

### #14 MrEvil178

MrEvil178

GMC Member

• New Member
• 9 posts

Posted 31 May 2010 - 01:39 PM

That works really well, thanks. Trying to add in jumping, it seems to get stuck when jumping and hitting a block from beneath.

Edited by MrEvil178, 31 May 2010 - 02:16 PM.

• 0

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

0 members, 0 guests, 0 anonymous users