Tuesday 30 December 2008

The Gull sprite


Another sprite, the gull, which is treated differently from a gettable sprite like the key. It moves in a sort of spiral, using its "phase" to decide where it is on its circle, and in choosing a suitable frame. The phase has 96 possible values and deflect is used to determine the x position with respect to start. Meanwhile, the y position just goes up and down in a vertical range. If you look at the sample game, you will see how they smoothly pass over the scenery and even over each other.

There is a certain jerkiness in the animation of the Gull at certain phases - it is not refected in the smooth animation of the animated gif (see right). I think it probably has to do with the calculation of position versus phase and may be improved with a different set of deflections. It is a subject worth revisiting.

Several GullA sprites can coexist; they simply require different start posiitions and phases.

They don't require save and restore code, because they are restored as part of scenery restoration.

import java.awt.Cursor;
import java.awt.Rectangle;
public class GullA extends Sprite
{
int [] deflect = {70, 70, 69, 68, 67, 66, 64, 62, 59,
57, 54, 50, 47, 43, 39, 35, 31, 27, 23, 19, 15, 10, 5, 0};
boolean up;
private int startx, starty, phase;
private final int MAXPHASE = 96;
public GullA(String spriteName, String Scenename, int StartX, int StartY, int Phase)
{
super(spriteName, Scenename, "gull.gif", MULTIPLE,
24, UNMOVING, NOACTION);
startx = StartX;
starty = StartY;
phase = Phase;
setPosition(startx+640, starty);
setSize(28, 27);
up = false;
}
public void tick(long ticks, int mousex, int mousey)
{
int coeff = 0;
increment(ticks);
phase += 1;
if (phase >= MAXPHASE) phase = 0;
setFrame(phase/4);
Rectangle r = getExtent();
if (phase < 24) coeff = deflect[phase];
if ((phase > 23) && (phase < 48))
{
coeff = -deflect[47-phase];
}
if ((phase > 47) && (phase < 72))
{
coeff = -deflect[phase - 48];
}
if ((phase > 71) && (phase < 96))
{
coeff = deflect[95-phase];
}
r.x = startx + coeff;
if (r.y < 28) up = false;
if (r.y > 250) up = true;
if (up)
r.y -= 1;
else
r.y += 1;
setPosition(r.x, r.y);
}
}

No comments: