Java slick:翻译

时间:2014-05-21 10:23:44

标签: java slick2d

我是Java新版本的新手,我对于#34;相机"的翻译有一点问题。 我有这段代码:

package chapterOne;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class GameState extends BasicGameState {

    public static final int ID = 1;

    private TiledMap map;
    private float posHumanX = 400;
    private float posHumanY = 600;
    private int direction = 0;
    private Animation[] animationHumain = new Animation[8];

    private boolean isMovingUp = false;
    private boolean isMovingDown = false;
    private boolean isMovingLeft = false;
    private boolean isMovingRight = false;

    private float vitesse = .1f;

    @Override
    public void init(GameContainer container, StateBasedGame game)
            throws SlickException {
        map = new TiledMap("src/main/resources/map/exemple.tmx");
        SpriteSheet sprite = new SpriteSheet("src/main/resources/sprites/soldier.png", 64, 64);

        animationHumain[0] = loadAnimation(sprite, 0, 1, 0);
        animationHumain[1] = loadAnimation(sprite, 0, 1, 1);
        animationHumain[2] = loadAnimation(sprite, 0, 1, 2);
        animationHumain[3] = loadAnimation(sprite, 0, 1, 3);
        animationHumain[4] = loadAnimation(sprite, 1, 9, 0);
        animationHumain[5] = loadAnimation(sprite, 1, 9, 1);
        animationHumain[6] = loadAnimation(sprite, 1, 9, 2);
        animationHumain[7] = loadAnimation(sprite, 1, 9, 3);
    }

    private Animation loadAnimation(SpriteSheet spriteSheet, int startX, int endX, int y) {
        Animation animation = new Animation();
        for (int x = startX; x < endX; x++) {
            animation.addFrame(spriteSheet.getSprite(x, y), 100);
        }
        return animation;
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException {
        map.render(0, 0);

        g.setColor(new Color(0, 0, 0, .5f));
        g.fillOval(posHumanX - 16, posHumanY - 8, 32, 16);
        boolean isMoving = getIsMoving();
        g.drawAnimation(animationHumain[direction + (isMoving ? 4 : 0)], posHumanX-32, posHumanY-60);

        g.translate(container.getWidth() / 2 - this.posHumanX, container.getHeight() / 2 - this.posHumanY);
    }

    private boolean getIsMoving(){

        if(isMovingUp || isMovingDown || isMovingLeft || isMovingRight){
            return true;
        }

        return false;
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException {
        if (this.isMovingUp) {
            this.posHumanY -= vitesse * delta;
        }

        if (this.isMovingDown) {
            this.posHumanY += vitesse * delta;
        }

        if (this.isMovingLeft) {
            this.posHumanX -= vitesse * delta;
        }

        if (this.isMovingRight) {
            this.posHumanX += vitesse * delta;
        }
    }

    @Override
    public void keyPressed(int key, char c) {
        switch (key) {
            case Input.KEY_UP:    this.direction = 0; this.isMovingUp = true; break;
            case Input.KEY_LEFT:  this.direction = 1; this.isMovingLeft = true; break;
            case Input.KEY_DOWN:  this.direction = 2; this.isMovingDown = true; break;
            case Input.KEY_RIGHT: this.direction = 3; this.isMovingRight = true; break;
            case Input.KEY_A: vitesse = vitesse + 0.2f; break;
        }
    }

    @Override
    public void keyReleased(int key, char c) {
        switch (key) {
            case Input.KEY_UP:    
                this.isMovingUp = false; 
                if(isMovingDown){ direction = 2; break;}
                if(isMovingLeft){ direction = 1; break;}
                if(isMovingRight){ direction = 3; break;}
                break;
            case Input.KEY_LEFT:  
                this.isMovingLeft = false; 
                if(isMovingUp){ direction = 0; break;}
                if(isMovingDown){ direction = 2; break;}
                if(isMovingRight){ direction = 3; break;}
                break;
            case Input.KEY_DOWN:  
                this.isMovingDown = false; 
                if(isMovingUp){ direction = 0; break;}
                if(isMovingLeft){ direction = 1; break;}
                if(isMovingRight){ direction = 3; break;}
                break;
            case Input.KEY_RIGHT: 
                this.isMovingRight = false; 
                if(isMovingUp){ direction = 0; break;}
                if(isMovingDown){ direction = 2; break;}
                if(isMovingLeft){ direction = 1; break;}
                break;
            case Input.KEY_A: vitesse = vitesse - 0.2f; break;
        }
    }

    @Override
    public int getID() {
        return ID;
    }

}

但是,它不起作用。我想我在渲染()中遗漏了一些东西,但我不知道:/

由于

1 个答案:

答案 0 :(得分:1)

您需要翻译图形对象以绘制您想要翻译的内容。因此,将g.translate(...)行调到渲染函数的顶部。唯一的问题是(如果你不想要翻译其他任何内容)你必须在将它翻译成开头之前重新翻译它。