1

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 + ")");
} }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Why code an applet? If it is due to spec by your instructor, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Aug 19 '14 at 00:54
  • 1) A single blank line of white space is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical & consistent code formatting style! The code indentation is intended to help people follow the program flow. – Andrew Thompson Aug 19 '14 at 00:56

1 Answers1

2

Those while loops will tie up your GUI thread, rendering your animation useless. Shouldn't they be if blocks instead? Also, you should enclose all blocks in curly braces. Your indentation is lying to you, and the braces will show you why.

Also you will need to call your super.paint(g) method in your paint override.

Also, is this an assignment, and if so are you required to code with AWT/Applets and not Swing?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373