[SOLVED] Collision with Object problem

T

Thanos

Guest
Hello, I've been struggling with a collision problem.
I've got an obstacle object that moves at a negative hspeed towards the player. When the player collides with it the player also moves at that negative hspeed and if you use the jump key the player can jump to overcome the obstacles.
My real problem is that if the player object collides with the obstacle and the user presses the jump key while the player is in mid-air the player jumps again. Theoretically the solution is simple for this one but i can't seem to get it.
I tried statements like this without result :
Code:
if place_meeting(x, y + 1, obj_jumpable) && keyboard_check_pressed(jump_key) {
    // JUMP CODE HERE
}
Here's the code for the jumpkey which doesn't work correctly:

Code:
if(keyboard_check_pressed(ord(global.up)) && place_meeting(x,y+1,Object_obstacles))
{
  sprite_index=Running
  image_speed=0.10
  vspd=-jumpspd

}
Here is the collision:

Code:
if(place_meeting(x+hspd,y,ground)|| place_meeting(x+hspd,y,Object_obstacles))
 {
    while(!place_meeting(x+sign(hspd),y,ground) && !place_meeting(x+sign(hspd),y,Object_obstacles))
    {
      x+=sign(hspd)
    }
    hspd=ground.hspeed
}
x+=hspd

if(place_meeting(x,y+vspd,ground) || place_meeting(x,y+vspd,Object_obstacles))
{
    while(!place_meeting(x,y+sign(vspd),ground) && !place_meeting(x,y+sign(vspd),Object_obstacles))
    {
      y+=sign(vspd)
    }
    vspd=0
}
y+=vspd
The Object_obstacles is a parent object that includes these 3 objects in the image:

If anyone can help i'd be much obliged.
 

Attachments

Last edited by a moderator:
M

maratae

Guest
Why don't you have 1 single parent object to be used as a platform?
At first glance, you're doing a while loop with a "&&" what will never be met unless the two objects are overlapping.
 
T

Thanos

Guest
the ground is now a child to the object_obstacles
Code:
if(place_meeting(x+hspd,y,Object_obstacles))
 {
    while(!place_meeting(x+sign(hspd),y,Object_obstacles))
    {
      x+=sign(hspd)
    }
    hspd=ground.hspeed
}
x+=hspd

if(keyboard_check_pressed(ord(global.up)) && place_meeting(x,y+1,Object_obstacles))
{
 
  sprite_index=Running
  image_speed=0.10
  vspd=-jumpspd
 
}
Same thing for the vertical collision as well
 
Last edited by a moderator:
T

Thanos

Guest
when the player collides with ground.x or obstacle.x he gets its speed
 
T

Thanos

Guest
When the player touches the obstacles and the keyjump is pressed the player jumps again. I want my player to be able to jump only when he s on the ground( either ground or on top of the crates)
 
M

maratae

Guest
Ok, so if it was up to me, what would I do?
First of all count the jumps, so the player doesn't jump mid-air.
Next, use the regular horizontal collision, and add a push script: if place_meeting(x+sign(hspd),y,obj_obstacle)) {x += obj_obstacle.x}

EDIT: Actually, you can't be dependent on the player movement, so something like:
Code:
if place_meeting(x+1,y,obj_obstacle)) {x += obj_obstacle.x}
Assuming every box is moving from right to left
 
Last edited by a moderator:
M

maratae

Guest
Code:
if place_meeting(x,y+1,floor) {jumps = 1};
if (jumpKey) && (jumps >0) {
vspd -= jspd;
jumps -= 1;
};
EDIT: For the future generations:
Solution included having in consideration that the whole world was moving left.

Code:
if place_meeting(x+ground.hspeed,y+1,floor) {jumps = 1}; // or x-7
if (jumpKey) && (jumps >0) {
vspd -= jspd;
jumps -= 1;
};
 
Last edited by a moderator:
Top