making images move

ive gotta a question about my code, particularly the keylistener interface.
Im hoping to (eventually) make some small stupid game, sorta like the original mario for snes or something. not to compilcatied, just a character jumping around, shooting bad guys and having a half decent background and theme music. however i cant even make the stupid object jump up or down. cn someone plz explain why this
[code]
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class keyjump extends Applet implements KeyListener,Runnable
{
Image dot;
Thread timer;
Point at;
int key;
public void init()
{
dot=getImage(getCodeBase(),"dot.jpg");
at=new Point(50,500);
requestFocus();

}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
if(timer!=null)
timer=null;
}
public void run() {}
public void pause(int time)
{
try
{
Thread.sleep(time);
}
catch(InterruptedException ex) {}
}
public void paint(Graphics g)
{
setBackground(Color.white);
g.drawImage(dot,at.x,at.y,this);
}
public void keyPressed(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) {}
public void keyTyped(KeyEvent evt)
{
switch(evt.getKeyCode())
{
case KeyEvent.VK_UP:
for(int i=0;i<10;i++)
{
at.y-=5;
repaint();
pause(100);
}
break;
case KeyEvent.VK_DOWN:
for(int i=0;i<10;i++)
{
at.y+=5;
repaint();
pause(100);
}
break;
case KeyEvent.VK_LEFT:
for(int i=0;i<10;i++)
{
at.x-=5;
repaint();
pause(100);
}
break;
case KeyEvent.VK_RIGHT:
for(int i=0;i<10;i++)
{
at.y+=5;
repaint();
pause(100);
}
break;
default:
key=(char)KeyEvent.KEY_RELEASED;
}
}
}
[/code]
doesnt work.(p.s i put the thread in there so it looks like the dot is moving, not just quickly "jumping" 50 pixels up.
thx

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion