![]() |
|
API >> Tutorials >> Playfield Scrolling and Pre-Collision mode >> Pre-Collision mode
Pre-collision mode: Balls behaviorThe pre-collision mode is the ability of a sprite (or a sprite wrapper) to know that a collision will be thrown before it happens. To explain this, we will construct a new sprite wrapper, which moves a ball with right hand rules.
A little explanation is needed.
Right hand rulesThe principal knowledge of a ball is to try to move in the counter clockwise order. In that way, when the direction of the ball is to the right: it tries to go first downwards, if it can not, it tries to go to the right, and so on in the clockwise order. The following pictures show how a ball tries to move in different cases:
In these 4 pictures, the ball tries to go initially to direction 1, then to direction 2, next to direction 3, and finaly to direction 4. We study right hand rules, but for left hand rules, we only need to turn in the clockwise order. We have to precise a simple thing; when a ball doesn't follow a tile, it turns in round. To avoid this, we need constants to tell the ball that it must follow a straight line.
The wrapperTo activate the pre-collision mode for a sprite, we only need to call the method setPremode(boolean) withtrue as argument.
Here is the sample Java code which tries to move a ball to the right with right hand rules (from the sprite wrapper class MovingBall.java:
Did you understand how it works?
But to know if a collision will happen, the mechanism is the same as for collisions. We have to use a CollisionManager. In our case it is once again the SpriteCollisionManager. In our example, we override the preCollisionWith(Sprite) method:
Now balls are ready, we modified the previous example in the file PathFinder.java by adding ball sprites in random positions:
We can scroll in this playfield now...
API >> Tutorials >> Playfield Scrolling and Pre-Collision mode >> Pre-Collision mode
|