TeamJava Forum
[ Read Responses | Post a New Response | Return to the Index ]
[ Previous | Next ]
animation in java applets
Posted by terabith on Saturday, 14 June 2003, at 9:48 p.m.
i want to make an object move in a java applet. right now i'm just trying it with a black square. i want it to move like a 'Snake' game, where it is always moving and turns when you press whatever arrow key. i can get it to move, but not continuously like Snake. there is something wrong with my code - it jsut doesn't make sense to me. this is what i have:
import java.applet.*;
import java.awt.*;
public class AnimateBee extends Applet
{
boolean pressedLeft, pressedRight, pressedUp, pressedDown, pressed;
String direction;
int x, y;
public void init ()
{
pressedRight=pressedDown=pressedUp=pressedLeft=false;
}
public boolean keyDown (Event e, int key)
{
if (key == Event.LEFT)
{
pressedLeft = true;
pressedRight = pressedDown = pressedUp = false;
}
else if (key == Event.RIGHT)
{
pressedRight = true;
pressedLeft = pressedDown = pressedUp = false;
}
else if (key == Event.UP)
{
pressedUp = true;
pressedRight = pressedDown = pressedLeft = false;
}
else if (key == Event.DOWN)
{
pressedDown = true;
pressedRight = pressedLeft = pressedUp = false;
}
repaint ();
return true;
}
public void paint (Graphics g)
{
g.fillRect (0 + x, 0 + y, 40, 33);
if (pressedLeft)
x = x - 1;
if (pressedRight)
x = x + 1;
if (pressedUp)
y = y - 1;
if (pressedDown)
y = y + 1;
}
}
can anyone tell me what to add to make it move constantly?
Responses
Post a New Response
The TeamJava Forum is maintained with WebBBS 2.24.