wall collision | gml
Type | Desc | |
---|---|---|
place_meeting(x, y, obj) | function | check for a collision between 2 instances |
vspeed | variable | vertical speed |
hspeed | variable | horizontal speed |
built-in bounce functions, use in Step event: | ||
move_bounce_all(advanced_bouncing?) | function | built-in "bouncing", argument for the function takes a true or false value |
move_bounce_solid(advanced_bouncing?) | function | same as above but only for objects marked as solid |
Sprite | Images |
---|---|
sprBall | |
sprWall |
Object | Sprite | Properties |
---|---|---|
objBall | sprBall | Visible |
objWall | sprWall | Visible |
objBall Create
randomize(); speed = irandom_range(2,5); direction = random(360);
objBall Step
if (place_meeting(x + hspeed, y, objWall)){ hspeed *= -1; } if (place_meeting(x, y + vspeed, objWall)){ vspeed *= -1; }
Extension Activities
1. Add a few more balls, and update the objBall→Step event:objBall Step
if (place_meeting(x + hspeed, y, objWall)){ hspeed *= -1; } if (place_meeting(x, y + vspeed, objWall)){ vspeed *= -1; } if (place_meeting(x + hspeed, y + vspeed, objBall)){ move_bounce_all(true); }2. You may need to update the collision mask:
3. Try alternative
step
event for objBall (can remove create event code):
switch(keyboard_key){ case vk_left: if not(place_meeting(x - 3, y, objWall)){ x -= 3; } break; case vk_right: if not(place_meeting(x + 3, y, objWall)){ x += 3; } break; }