The problem is that when I press the down button, the image (blueboy) disappears, and it also doesn't move up but it can move side to side, I cant understand why? please help. (I'm using RealJ). If there is a website that can help me on 2d side scrolling games that would help me as well.
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class projectblue extends Applet implements ActionListener,KeyListener,MouseListener {
Image blueboy,trees; //image variable
int size = 4,jump = 50, cx = 1, cy = 1;
public void init() {
blueboy = getImage(getDocumentBase(),"png.png");
trees= getImage(getDocumentBase(),"background.jpg");
addKeyListener( this );
addMouseListener( this );
}
public void paint(Graphics g)
{
int width = trees.getWidth(this);
int height = trees.getHeight(this);
//System.out.println("" + cx +","+ cy);
//The later images are drawn on top of the earlier ones.
g.drawImage(trees, 1 ,1, width*size,height*7,this);
g.drawImage(blueboy, cx, cy, this);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void left(){
cx = cx -100;
// blueboy moves left
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
System.out.println("key pressed");
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
left();
}
if (key == KeyEvent.VK_RIGHT) {
cx = cx + 100;
//blueboy moves right
}
if (key == KeyEvent.VK_UP) {
while (cy >= 0 && cy <=1000)
cy = cy - 100;
if (cy < 0){
cy = 1;
//blueboy moves up, but if it goes out of bounds it goes back to the top
}
}
if (key == KeyEvent.VK_DOWN) {
while (cy >= 0 && cy <=1000)
cy = cy + 100;
if (cy > 1001){
cy = 999;
}
//blueboy moves down, but if it goes out of bounds it goes back to the bottim
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) { }
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) {
int x = getX();
int y = getY();
System.out.println("clicked at (" + x + ", " + y + ")");
} }