External Exam Download Resources Web Applications Games Recycle Bin

wall collision | gml

 TypeDesc
place_meeting(x, y, obj)functioncheck for a collision between 2 instances
vspeedvariablevertical speed
hspeedvariablehorizontal speed
built-in bounce functions, use in Step event:
move_bounce_all(advanced_bouncing?)functionbuilt-in "bouncing", argument for the function takes a true or false value
move_bounce_solid(advanced_bouncing?)functionsame as above but only for objects marked as solid
SpriteImages
sprBall
sprWall
ObjectSpriteProperties
objBallsprBallVisible
objWallsprWallVisible

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;
}