无法弄清楚如何在Slick2d + LWJL中跳转角色

时间:2013-12-03 06:55:13

标签: java lwjgl slick2d

我一直在考虑这个问题而且无论如何我都无法想到我可以在游戏中跳跃角色。这是一个平台游戏,基于状态的游戏在slick2d + LWJGL。

这是我的代码

package net.battle.main;

import java.io.Console;
import java.util.Arrays;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;

public class Level implements GameState {

private static final float shfitX = 0;
Animation ninja, fairy, ninja_left, fairy_left, ninja_right, fairy_right;
Image WorldMap;
boolean pause = false;
int[] duration = {200, 200, 200};
float charPosX= 0;
float charPosY= 0;
float shiftX= charPosX + 360;
float shiftY= charPosY + 160;

public Level(int level) {
}

@Override
public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {


}

@Override
public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseWheelMoved(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void inputEnded() {
    // TODO Auto-generated method stub

}

@Override
public void inputStarted() {
    // TODO Auto-generated method stub

}

@Override
public boolean isAcceptingInput() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void setInput(Input arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonPressed(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonReleased(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void enter(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public int getID() {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public void init(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    WorldMap = new Image("res/testmap.png");
    Image[] ninjaRight = {new Image("res/ninja-right.png"),new Image("res/ninja-right-walking.png"), new Image("res/ninja-right-walking-two.png")};
    Image[] ninjaLeft = {new Image("res/ninja-left.png"),new Image("res/ninja-left.png"), new Image("res/ninja-left.png")};
    Image[] ninjaStanding = {new Image("res/ninja.png"),new Image("res/ninja.png"), new Image("res/ninja.png")};

    Image[] fairyRight = {new Image("res/fairy-right.png"),new Image("res/fairy-right-walking.png"), new Image("res/fairy-right-walking-two.png")};
    Image[] fairyLeft = {new Image("res/fairy-left.png"), new Image("res/fairy-left-walking.png"), new Image("res/fairy-left-walking-two.png")};
    Image[] fairyStanding = {new Image("res/fairy.png"),new Image("res/fairy.png"), new Image("res/fairy.png")};

ninja_right = new Animation(ninjaRight, duration, true);
ninja_left = new Animation(ninjaLeft, duration, true);
ninja = new Animation(ninjaStanding, duration, false);

fairy_right = new Animation(fairyRight, duration, true);
fairy_left = new Animation(fairyLeft, duration, true);
fairy = new Animation(fairyStanding, duration, false);
}

@Override
public void leave(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public void render(GameContainer gc, StateBasedGame s, Graphics g)throws SlickException {

    WorldMap.draw(charPosX,charPosY);
    fairy.draw(shiftX,shiftY);

    g.drawString("char X: "+charPosX+"\nchar Y: "+charPosY, 400, 20);

    if(pause==true){
        g.setColor(Color.black);
        g.fillRect(240, 100, 200, 100);
        g.setColor(Color.white);
        g.drawString("Resume (Press esc)", 250, 110);
        g.drawString("Main Menu (Press M)", 250, 130);
        g.drawString("Quit (Press Q)", 250, 150);

    if(pause==false){
        g.clear();

    }
    }



}

@Override
public void update(GameContainer gc, StateBasedGame s, int delta)
        throws SlickException {
    boolean n;
    Input input = gc.getInput();
    //System.out.println(pause);

// PAUSE MENU
    if(pause==true){
    if(Keyboard.isKeyDown(input.KEY_M)){
        System.out.println("Key Press: M has been pressed");
        System.out.println(pause);
        s.enterState(0);
    }

    if(Keyboard.isKeyDown(input.KEY_Q)){

        System.out.println("Key Press: Q has been pressed \nExiting game");
        System.exit(0);
    }
    }
    if(input.isKeyPressed(input.KEY_ESCAPE)){
        System.out.println("Key Press: Escape has been pressed");

        if(pause==true){
            System.out.println(pause);
            System.out.println("boolean change");
            pause=false;
            System.out.println(pause);
            gc.setPaused(false);
            System.out.println("unpaused");
        }else if(pause==false){
            System.out.println(pause);
            System.out.println("boolean change");
            pause=true;
            System.out.println(pause);
            gc.setPaused(true);
            System.out.println("paused");
        }

    }
    // END OF PAUSE MENU
    // CHARACTER MOVEMENT

    if(Keyboard.isKeyDown(input.KEY_SPACE)){
        fairy=fairy;


    }
    if(Keyboard.isKeyDown(input.KEY_D)){

        fairy = fairy_right;


        charPosX -= delta *.1;
        if(charPosX<-1607){
            charPosX += delta *.1;
        }

    }

        if(input.isKeyDown(input.KEY_A)){
            fairy = fairy_left;
            charPosX += delta *.1;
            if(charPosX>361){
                charPosX -= delta *.1;

        }
    }
}

}

所以,如果有人能想出一个让我的角色能够跳跃的方法那就太棒了!

1 个答案:

答案 0 :(得分:1)

好的,这很容易解决。首先,我创建了一个Vector2f(我认为无论如何都称之为这种方式)并将其称为玩家速度。

if(Keyboard.isKeyDown(input.KEY_SPACE)){
    fairy=fairy;

    velocity.y = 1; //play around with that number to influence gravity
}

updatePlayer();

然后是updatePlayer()方法:

protected void updatePlayer() {

if(velocity.y > 0) {
    charPosY = charPosY + (delta * velocity.y);
    velocity.y = velocity.y - (0.1 * delta);
}

这些只是代码片段,它们不起作用。我写了它们给你以及它应该是什么样子的想法