I've changed some of the code but the animation plays when you keep pressing the space bar. I want it to animate my bullet across the screen just by pressing the space bar(not by pressing the space bar mulitple times). Thank for your help!
*Note: the old code was posted under "Bullet Animation" by my classmate. if you can't fix this code, try the old code.
// The "SpaceShootewr" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SpaceShooter extends Applet implements KeyListener
{
int x = 10, dx, y = 10, dy, a = 0, by = 47, bx = 95;
char m;
private Image dbImage;
private Graphics dbg;
Thread thread;
Image img, tile, background, bullet, blank, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
Graphics backg;
boolean triMove = false;
// Place instance variables here
public void init ()
{
img = getImage (getDocumentBase (), "Shoot.jpg");
bullet = getImage (getDocumentBase (), "bullet.jpg");
blank = getImage (getDocumentBase (), "Blank.jpg");
mon1 = getImage (getDocumentBase (), "mon1.jpg");
mon2 = getImage (getDocumentBase (), "mon2.jpg");
mon3 = getImage (getDocumentBase (), "mon3.jpg");
mon4 = getImage (getDocumentBase (), "mon4.jpg");
mon5 = getImage (getDocumentBase (), "mon5.jpg");
mon6 = getImage (getDocumentBase (), "mon6.jpg");
mon7 = getImage (getDocumentBase (), "mon7.jpg");
addKeyListener (this);
/*tile = createImage (1280, 690);
backg = tile.getGraphics ();
backg.setColor (Color.blue);
*/
// Place the body of the initialization method here
} // init method
public void paint (Graphics g)
{
g.drawImage (img, x, y, 100, 100, this);
if (triMove == true)
{
for (int i = 0 ; i < 50 ; i++)
{
bx += 1;
for (int q = 0 ; q < 1000000 ; q++)
{
}
g.drawImage (blank, bx - 1, y + 37, 40, 10, this);
g.drawImage (bullet, bx + i, y + 37, 40, 10, this);
}
triMove = false;
}
if (bx >= 250 + 95)
{
bx = 95;
}
//backg.drawImage(background,0,0,this);
//Place the body of the drawing method here
} //paintmethod
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
/*if(key==KeyEvent.VK_RIGHT)
{
//backg.drawImage(background,0,0,1280, 960, this);
dx=2;
dy=0;
x=x+dx;
repaint();
}
elseif(key==KeyEvent.VK_LEFT)
{
//backg.drawImage(img,0,0,1280, 960, this);
dx=-2;
dy=0;
x=x+dx;
repaint();
}*/
if (key == KeyEvent.VK_UP)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
dy = -10;
dx = 0;
y = y + dy;
repaint ();
}
else if (key == KeyEvent.VK_DOWN)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
dy = 10;
dx = 0;
y = y + dy;
repaint ();
}
else if (key == KeyEvent.VK_SPACE)
{
triMove = true;
repaint ();
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
public void update (Graphics g)
{
// initialize doublebuffers
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
// save background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
// draw foreground on background
dbg.setColor (getForeground ());
paint (dbg);
// Now indicate ready drawn picture Offscreen on the right screen
g.drawImage (dbImage, 0, 0, this);
}
} // SpaceShooter class