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;
}
}
4

1 に答える 1

0

問題が見つかりました。左に移動するためのifステートメントの後に余分なセミコロン(常にセミコロン-_-)があります。

 if(in.isKeyDown(Input.KEY_A));
    if(map.isClipped(x - 1, y))
        x += 1;
于 2013-03-13T12:39:55.957 に答える