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

Please select: Discussion Question Answer
Pulse Check - enter num: 1062424535

Your Name:

E-Mail Address:

Subject:

Message:


If you'd like to include a link to another page with your message,
please provide both the URL address and the title of the page:

Optional Link URL:

Optional Link Title:


If you'd like to have the option of deleting your post later,
please provide a password (CASE SENSITIVE!):

Password:



Password:

The TeamJava Forum is maintained with WebBBS 2.24.


TeamJavaHome | Ind. Registry | Co. Registry | Java Links
Jobs Listings Forum | TeamJava Books | Java FAQ Archives

AWN

Copyright © 1995-2003 Active Web Networks, all rights reserved.
(Note: We share no direct association with Sun Microsystems, Inc.)