我的问题是我想让我的角色(玩家)跳过我稍后会添加的对象。我以为Tile Collision会起作用,但我不了解它的方方面面。除了我的游戏一次只有一个级别,而不是整个游戏。
这是我到目前为止所拥有的。 (我没有写代码让玩家跳上任何东西,因为我不知道从哪里开始):
Game.java:
public class Game extends gameLoop{
public void init()
{
setSize(854,480);
Thread th = new Thread(this);
th.start();
offscreen = createImage(854, 480);
d = offscreen.getGraphics();
addKeyListener(this);
}
public void paint(Graphics g){
d.clearRect(0, 0, 854, 480);
d.drawImage(background, 0 , 0 , this);
d.drawImage(Player, x, y, this);
g.drawImage(offscreen, 0 , 0, this);
}
public void update(Graphics g){
paint(g);
}
}
gameLoop.java:
public class gameLoop extends Applet implements Runnable, KeyListener {
public int x ,y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background, run1, run2, run3, run4, run5, run6, run7, run8, runL1, runL2, runL3, runL4, runL5, stand, Player;
public int counter;
public int counterLeft;
public double counter2 = 4;
public void run(){
x = 100;
y = 100;
try {
background = ImageIO.read(new File("background2.png"));
stand = ImageIO.read(new File("stick.gif"));
run1 = ImageIO.read(new File("Runner.png"));
run2 = ImageIO.read(new File("Runner1.png"));
run3 = ImageIO.read(new File("Runner2.png"));
run4 = ImageIO.read(new File("Runner3.png"));
run5 = ImageIO.read(new File("Runner4.png"));
runL1 = ImageIO.read(new File("Runner(1).png"));
runL2 = ImageIO.read(new File("Runner1(1).png"));
runL3 = ImageIO.read(new File("Runner2(1).png"));
runL4 = ImageIO.read(new File("Runner3(1).png"));
runL5 = ImageIO.read(new File("Runner4(1).png"));
} catch (IOException e1) {
e1.printStackTrace();
}
Player = stand;
while(true){
if(y <= 319 && up != true){
y+=10;
}
counter++;
counterLeft ++;
if (counter >= 90){
counter = 0;
}
if (counter >= 10 && right == true && left == false){
Player = run1;
}
if (counter >= 20 && right == true && left == false){
Player = run2;
}
if (counter >= 30 && right == true && left == false){
Player = run3;
}
if (counter >= 40 && right == true && left == false){
Player = run4;
}
if (counter >= 50 && right == true && left == false){
Player = run5;
}
if (counter >= 60 && right == true && left == false){
Player = run1;
}
if (counter >= 70 && right == true && left == false){
Player = run2;
}
if(counter >= 80 && right == true && left == false){
Player = run3;
}
// LEFT
if (counterLeft >= 55){
counterLeft = 0;
}
if (counterLeft >= 10 && left == true && right == false){
Player = runL1;
}
if (counterLeft >= 20 && left == true && right == false){
Player = runL2;
}
if (counterLeft >= 30 && left == true && right == false){
Player = runL3;
}
if (counterLeft >= 40 && left == true && right == false){
Player = runL4;
}
if (counterLeft >= 50 && left == true && right == false){
Player = runL5;
}
if (left == true){
x--;
}
if (right == true){
x++;
}
if (up == true){
counter2 += 0.05;
y = y + (int) ((Math.sin(counter2) + Math.cos(counter2))* 5);
if(counter2 >= 7){
counter2 = 4;
}
}
if(down == true){
y++;
}
if(y >= 319){
y = 319;
}
repaint();
try {
Thread.sleep(5);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37 && e.getKeyCode() != 38){ //left
left = true;
}
if (e.getKeyCode() == 38 && e.getKeyCode() != 37){ //up or jump
up = true;
}
if (e.getKeyCode() == 39){ //right
right = true;
}
if (e.getKeyCode() == 40){ //down
down = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 37){
left = false;
Player = stand;
}
if (e.getKeyCode() == 38){
up = false;
Player = stand;
}
if (e.getKeyCode() == 39){
right = false;
Player = stand;
}
if (e.getKeyCode() == 40){
down = false;
Player = stand;
}
}
public void keyTyped(KeyEvent e) {}
}
到目前为止,他所能做的就是跑步和跳跃。