使用Slick库在2D游戏中移动的问题

问题描述 投票:1回答:1

不知道为什么,你就是一直向左移动,我也不知道为什么。键盘输入读数不是问题,因为向任何其他方向移动都能完美地工作。忽略isClipped()方法,因为我还没有完成它。谢谢你的帮助。

主类

package Genisis;

import java.io.FileNotFoundException;
import java.util.ArrayList;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState
{
int mouseX;
int mouseY;
Level testLevel;
boolean[][] blocked;

MainCharacter mc;

public Play(int state)
{

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{

    gc.setMouseCursor("res/sprites/Cursor.png", 0, 0);

    try {
        testLevel = new Level(0, 0, "res/maps/testmap.tmx");
    } catch (FileNotFoundException e) {
        System.out.println("Failed to load map.");
        e.printStackTrace();
    }

    mouseX = Mouse.getX();
    mouseY = gc.getHeight() - Mouse.getY();

    mc = new MainCharacter(50, 50, new Image("res/sprites/mcg.png"));   
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws 
                                                                         SlickException
{
    testLevel.render(mc.x, mc.y);
    mc.render(mouseX, mouseY);
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws 
                                                                        SlickException
{
    mc.move(gc, testLevel);
    mc.render(gc.getWidth()/2, gc.getHeight()/2);
    testLevel.render(mc.x, mc.y);
}

public int getID()
{
    return 1;
}

public void spawnMC(float spwnMCX, float spwnMCY)
{
    testLevel.render(spwnMCX, spwnMCY);
    testLevel.render(spwnMCX, spwnMCY);
}

public void spawnOC()
{
    //todo
}
}

玩家类

        package Genisis;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;


public class MainCharacter extends Player
{

public MainCharacter(float newX, float newY, Image newSprite) throws SlickException
{
    sprite = newSprite;
    x = newX;
    y = newY;
}

public void move(GameContainer gc, Level map)
{   
    Input in = gc.getInput();

    if(in.isKeyDown(Input.KEY_W))
        if(map.isClipped(x, y + 1))
            y += 1;
    if(in.isKeyDown(Input.KEY_D))
        if(map.isClipped(x + 1, y))
            x -= 1;
    if(in.isKeyDown(Input.KEY_S))
        if(map.isClipped(x, y - 1))
            y -= 1;
    if(in.isKeyDown(Input.KEY_A));
        if(map.isClipped(x - 1, y))
            x += 1;
}

public void render(float mX, float mY)
{
    float xDist = mX - (500 + (sprite.getWidth() / 2));
    float yDist = mY - (250 + (sprite.getHeight() / 2));

    double angleToTurn = Math.toDegrees(Math.atan2(yDist, xDist));
    sprite.setRotation((float)angleToTurn);

    sprite.draw(500, 250);
}
}

级数

package Genisis;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.tiled.TiledMap;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Level 
{

//height of background (in tiles)
public int height;

//width of background (in tiles)
public int width;

public boolean[][] clipped;

public TiledMap map;


public Level(int newHeight, int newWidth, String path) throws 
                                                  FileNotFoundException, SlickException
{
    map = new TiledMap(path);
    clipped = new boolean[map.getHeight()][map.getWidth()];

    for(int i = 0; i < map.getHeight(); i++)
        for(int j = 0; i < map.getWidth(); i++)
        {
            if("true".equals(map.getTileProperty(map.getTileId(i, j, 
                            0), "blocked", "false")))
                clipped[i][j] = false;
        }
}

//render map
public void render(float mcX, float mcY)
{
    map.render((int)(mcX), (int)(mcY));
}

//return height of level (in pixels)
public int getHeight()
{
    return map.getHeight();
}

//return width of level (in pixels)
public int getWidth()
{
    return map.getWidth();
}

public boolean isClipped(float charX, float charY)
{
    //return clipped[(int)(charX / 50)][(int)(charY / 50)];
        return true;
}
}
controls 2d slick2d
1个回答
0
投票

找到问题所在了。向左移动的if语句后面多了一个分号(总是分号-_-)。

 if(in.isKeyDown(Input.KEY_A));
    if(map.isClipped(x - 1, y))
        x += 1;
© www.soinside.com 2019 - 2024. All rights reserved.