| ||
Lets review the important formulas introduced in this chapter.
Remove an out-of-bounds object:
if(mc._x - mc._width / 2 > right mc._x + mc._width / 2 < left mc._y mc._height / 2 > bottom mc._y + mc._height / 2 < top) { mc.removeMovieClip(); }
Regenerate an out-of-bounds object:
if(mc._x - mc._width / 2 > right mc._x + mc._width / 2 < left mc._y mc._height / 2 > bottom mc._y + mc._height / 2 < top) { // reset mc position and velocity. }
Screen wrapping for an out-of-bounds object:
if(mc._x - mc._width / 2 > right) { mc._x = left - mc._width / 2; } else if(mc._x + mc._width / 2 < left) { mc._x = right + mc._width / 2; } if(mc._y mc._height / 2 > bottom) { mc._y = top mc._height / 2; } else if(mc._y + mc._height / 2 < top) { mc._y = bottom + mc._height / 2; }
Apply friction (the correct way):
speed = Math.sqrt(vx * vx + vy * vy); angle = Math.atan2(vy, vx); if(speed > friction) { speed -= friction; } else { speed = 0; } vx = Math.cos(angle) * speed; vy = Math.sin(angle) * speed;
Apply friction (the easy way):
vx *= friction; vy *= friction;
| ||