阻止玩家陷入某些坐标?

时间:2014-04-10 21:22:48

标签: java game-engine slick2d

是的,过去几天我一直在学习Slick2D,并想知道为什么以及如何修复我的玩家卡在碰撞坐标if语句中。

这是我的主Play状态的代码,Update void的前几行是if条件,看看角色(窗口的中间)是否在coords内...如果玩家然后移动em基本上

package com.ohdanbo.game;

import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState {

    Animation dude, movingUp, movingDown, movingLeft, movingRight;
    Image map;
    boolean quit = false;
    int[] duration = { 200, 200 };
    float dudePositionX = 0;
    float dudePositionY = 0;
    float shiftX = (640 / 2) - (40 / 2);
    float shiftY = (360 / 2) - (40 / 2);
    Image pauseBG;
    Shape rectangle;
    float rectX = 0;
    float rectY = 0;

    public Play(int state) {
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        //rectangle = new Rectangle(100,100);
        pauseBG = new Image("res/bg.png");
        map = new Image("res/worldTemp.png");
        Image[] walkUp = { new Image("res/charBack.png"), new Image("res/charBack.png") };
        Image[] walkDown = { new Image("res/charFront.png"), new Image("res/charFront.png") };
        Image[] walkLeft = { new Image("res/charLeft.png"), new Image("res/charLeft.png") };
        Image[] walkRight = { new Image("res/charRight.png"), new Image("res/charRight.png") };

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);
        dude = movingDown;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        map.draw(dudePositionX, dudePositionY);
        dude.draw(shiftX, shiftY);
        //g.draw(rectangle);
        // g.setColor(Color.black);
        g.drawString("Dude's X: " + dudePositionX + "\nDude's Y: " + dudePositionY, 400, 20);

        if (quit == true) {
            pauseBG.draw(0, 0);
            g.drawString("Continue (C)", 250, 100);
            g.drawString("Main Menu (M)", 250, 150);
            g.drawString("Quit (Q)", 250, 200);
            if (quit == false) {
                g.clear();
            }
        }
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if ((dudePositionX > 100) && (dudePositionX < 267) && (dudePositionY > -68) && (dudePositionY < 156)) {
            if(input.isKeyDown(Input.KEY_UP)){dudePositionY -= delta * .1f;}
            if(input.isKeyDown(Input.KEY_DOWN)){dudePositionY += delta * .1f;}
            if(input.isKeyDown(Input.KEY_LEFT)){dudePositionX -= delta * .1f;}
            if(input.isKeyDown(Input.KEY_RIGHT)){dudePositionX += delta * .1f;}
        }

        if (input.isKeyDown(Input.KEY_UP)) {
            dude = movingUp;
            dudePositionY += delta * .1f; // increase the Y coordinates of bucky
                                            // (move him up)
            if (dudePositionY > 162) {
                dudePositionY -= delta * .1f; // dont let him keep going up if
                                                // he reaches the top
            }
        }
        if (input.isKeyDown(Input.KEY_DOWN)) {
            dude = movingDown;
            dudePositionY -= delta * .1f;
            if (dudePositionY < -600) {
                dudePositionY += delta * .1f;
            }
        }
        if (input.isKeyDown(Input.KEY_LEFT)) {
            dude = movingLeft;
            dudePositionX += delta * .1f;
            if (dudePositionX > 324) {
                dudePositionX -= delta * .1f;
            }
        }
        if (input.isKeyDown(Input.KEY_RIGHT)) {
            dude = movingRight;
            dudePositionX -= delta * .1f;
            if (dudePositionX < -840) {
                dudePositionX += delta * .1f;
            }
        }

        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            quit = true;
        }

        if (quit == true) {
            if (input.isKeyDown(Input.KEY_C)) {
                quit = false;
            }
            if (input.isKeyDown(Input.KEY_M)) {
                sbg.enterState(0);
                quit = false;
            }
            if (input.isKeyDown(Input.KEY_Q)) {
                System.exit(0);
            }
        }
    }

    public int getID() {
        return 1;
    }

}

1 个答案:

答案 0 :(得分:0)

您希望如何处理此类碰撞检测/碰撞响应问题,以了解碰撞通常应如何在游戏循环中发挥作用。

基本上,所有这些都是基于操作顺序来获得正确的结果:

  • 考虑字符的初始位置,然后保存。
  • 轮询播放器的控制器输入,并更新播放器坐标。
  • 如果玩家“超出界限”或与您不想要的任何内容发生碰撞,请将其返回到之前的位置(例如您在步骤1中保存的初始位置)

尝试这样的方法,而不是直接集成到您的控制器代码中,因为我在下面的代码片段中指出:

    if (input.isKeyDown(Input.KEY_RIGHT)) {
        dude = movingRight;
        dudePositionX -= delta * .1f;

        // Do these lines LATER after all the 
        //   controls of movement have been seen and the player updated
        if (dudePositionX < -840) { 
            dudePositionX += delta * .1f;
            // Instead of this line, you would have it be something like:
            // dudePositionX = previousDudePositionX;
        }
    }