How do you make a character jump, both on objects and just normal jump?

Posted by haxerflaxer on Stack Overflow See other posts from Stack Overflow or by haxerflaxer
Published on 2010-05-31T11:53:31Z Indexed on 2010/05/31 19:03 UTC
Read the original article Hit count: 212

Filed under:
|
|
|
|

Hi, I'm kind of a beginner when it comes to java programming, and I have a project in school where I'm going to create a game much like Icy Tower. And my question is, how am I going to write to make the character stand on the ground and be able to jump up on objects?

Here's my code so far:

Part one

package Sprites;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class jumper {

private String jump = "oka.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

public jumper() {
 ImageIcon ii = new ImageIcon(this.getClass().getResource(jump));
 image = ii.getImage();
 x = 50;
 y = 100;
    }

public void move() {
 x += dx;
 y += dy;
}

public int getX() {
 return x;
}

public int getY() {
 return y;
}

public Image getImage() {
 return image;
}



public void keyPressed(KeyEvent e) {
 int key = e.getKeyCode();



 if (key == KeyEvent.VK_LEFT) {
     dx = -5;

     ImageIcon ii = new ImageIcon(this.getClass().getResource("oki.png"));
     image = ii.getImage();
 }

 if (key == KeyEvent.VK_RIGHT){
     dx = 5;

     ImageIcon ii = new ImageIcon(this.getClass().getResource("oka.png"));
     image = ii.getImage();
 }

 if (key == KeyEvent.VK_SPACE) {
     dy = -5;
 }



 if (key == KeyEvent.VK_DOWN) {
     dy = 5;
 }


} 

public void keyReleased(KeyEvent e) {
 int key = e.getKeyCode();


 if (key == KeyEvent.VK_LEFT) {
     dx = 0;
 }

 if (key == KeyEvent.VK_RIGHT){
     dx = 0;
 }

 if (key == KeyEvent.VK_SPACE) {
     dy = 0;
 }



 if (key == KeyEvent.VK_DOWN) {
     dy = 0;
 }
    }

}

Part two

package Sprites;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class board extends JPanel implements ActionListener {

    private Timer klocka;
    private jumper jumper;

    public board() {

 addKeyListener(new TAdapter());
 setFocusable(true);
 setBackground(Color.WHITE);
 setDoubleBuffered(true);

 jumper = new jumper();

 klocka = new Timer(5, this);
 klocka.start();
    }

    public void paint(Graphics g) {
 super.paint(g);

 Graphics2D g2d = (Graphics2D)g;
 g2d.drawImage(jumper.getImage(), jumper.getX(), jumper.getY(), this);

 Toolkit.getDefaultToolkit().sync();
 g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
 jumper.move();
 repaint();
    }

    private class TAdapter extends KeyAdapter {

 public void keyReleased(KeyEvent e) {
     jumper.keyReleased(e);
    }
 public void keyPressed(KeyEvent e) {
     jumper.keyPressed(e);
 }
  }
}

Part three

package Sprites;

import javax.swing.JFrame;

public class RType extends JFrame {
    public RType() {

 add(new board());

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setSize(800, 600);
 setLocationRelativeTo(null);
 setTitle("R - type");
 setResizable(false);
 setVisible(true);
    }

public static void main(String[] args) {
    new RType();   

}

}

I really appreciate all the help I can get!

© Stack Overflow or respective owner

Related posts about java

Related posts about game