0

亲爱的先生们美好的一天。我在角色的动画和控件方面遇到了问题。我的问题是,每当我尝试按左右箭头键时,我的播放器都会上下移动,而不是左右移动。我确实尝试过调试我的代码,但我仍然无法找出代码的哪一部分是错误的。这就是为什么我要求你们查看我的代码,并可能知道代码的哪一部分是错误的或需要改进。

这是我的播放器代码:

public class DugmanPlayer {
    public enum state
    {
        standby,
        walking,
        digging,
        dying
    }

    public enum dir
    {
        left,
        right,
        up,
        down
    }

    public static float WIDTH;
    public static float HEIGHT;
    static float speedmax = 30f;
    static float damping = 0.9f;
    dir d = dir.up;

    public Vector2 position = new Vector2();
    public Vector2 speed = new Vector2();
    state st = state.walking;

    public float statetime = 0;

    public void UpdatePlayer(float deltatime)
    {
        if(deltatime==0)return;
        statetime += deltatime;
        //rightwalking
        if(Math.abs(speed.x) > speedmax) {
            speed.x = Math.signum(speed.x) * speedmax;
            if(d != dir.left)d = dir.right;
            st = state.walking;
        }

        if(Math.abs(speed.x) < 1) {
            speed.x = 0;
            if(st!=state.walking)st = state.standby;
            d=dir.right;
        }
        //upwalking
        if(Math.abs(speed.y) > speedmax) {
            speed.y = Math.signum(speed.y) * speedmax;
            if(d != dir.down)d = dir.up;
            st = state.walking;
        }

        if(Math.abs(speed.y) < 1) {
            speed.y = 0;
            if(st != state.walking)st = state.standby;
            d=dir.up;
        }

        if(Math.abs(-speed.x)>speedmax)
        {
            speed.x = Math.signum(-speed.x) * speedmax;
            if(d != dir.right)d = dir.left;
            st = state.walking;
        }
        if(Math.abs(-speed.x)<1)
        {
            speed.x = 0;
            if(st!=state.walking)st = state.standby;
            d=dir.up;
        }

        if(Math.abs(-speed.y)>speedmax){
            speed.y = Math.signum(-speed.y) * speedmax;
            if(d != dir.up)d = dir.down;
            st = state.walking;
        }
        if(Math.abs(-speed.y)<1)
        {
            speed.y = 0;
            if(st!=state.walking)st = state.standby;
            d=dir.down;
        }

        speed.scl(deltatime);

        position.add(speed);
        speed.scl(1/deltatime);

        // Apply damping to the velocity on the x-axis so we don't
        // walk infinitely once a key was pressed
        speed.x *= damping;
        speed.y *= damping;

        InputProcess();
    }

    TextureRegion left;
    TextureRegion right;
    TextureRegion up;
    TextureRegion down;
    TextureRegion standby;
    Animation Walkingleft;
    Animation WalkingRight;
    Animation WalkingUp;
    Animation WalkingDown;
    Animation stndbyleft,stndbyright,stndbyup,stndbydown;

    public void LoadPlayerTexture()
    {
        TextureAtlas dug = new TextureAtlas(Gdx.files.internal("character/walking.pack"));
        left = dug.findRegion("faceleft");
        right = dug.findRegion("faceright");
        up = dug.findRegion("faceup");
        down = dug.findRegion("facedown");

        TextureRegion[] leftwalk = left.split(left.getRegionWidth()/3, left.getRegionHeight()/1)[0];
        TextureRegion[] rightwalk = right.split(right.getRegionWidth()/3, right.getRegionHeight()/1)[0];
        TextureRegion[] upwalk = up.split(up.getRegionWidth()/3, up.getRegionHeight()/1)[0];
        TextureRegion[] downwalk = down.split(down.getRegionWidth()/3, down.getRegionHeight()/1)[0];

        Walkingleft = new Animation(0.5f, leftwalk[0],leftwalk[1],leftwalk[2]);
        WalkingRight = new Animation(0.5f, rightwalk[0],rightwalk[1],rightwalk[2]);
        WalkingUp = new Animation(0.5f, upwalk[0],upwalk[1],upwalk[2]);
        WalkingDown = new Animation(0.5f, downwalk[0],downwalk[1],downwalk[2]);

        stndbyleft = new Animation(0, leftwalk[1]);
        stndbyright = new Animation(0, rightwalk[1]);
        stndbyup = new Animation(0, upwalk[1]);
        stndbydown = new Animation(0, downwalk[1]);

        DugmanPlayer.WIDTH = 1/16f * leftwalk[0].getRegionWidth();
        DugmanPlayer.HEIGHT = 1/16f * leftwalk[0].getRegionHeight();
    }

    public void DrawPlayer(float deltatime, OrthogonalTiledMapRenderer r)
    {
        Animation fm = null;
        boolean loop = true;

        switch (st) {
        case standby: if(speed.y == 0){
        fm = stndbyup;
        }
        else if(-speed.y == 0){
        fm = stndbydown;
        }else if (speed.x == 0) {
        fm = stndbyright;
        }else if (-speed.x == 0) {
        fm = stndbyleft;
        }
            break;
        case walking: if(speed.y>speedmax){
        fm = WalkingUp;
        }else if(-speed.y>speedmax){
        fm = WalkingDown;
        }else if (speed.x>speedmax) {
        fm = WalkingRight;
        }else if (-speed.x>speedmax) {
        fm = Walkingleft;
        }
        break;
        case digging:
            break;
        case dying:
            break;
        default:
            break;
        }


        SpriteBatch batch = r.getSpriteBatch();

        batch.begin();
        batch.draw(fm.getKeyFrame(statetime, loop), position.x, position.y, WIDTH, HEIGHT);
        batch.end();
    }

    public void InputProcess()
    {
        if(Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W)) {
            if(st != state.standby)st = state.walking;
            speed.y += speedmax;
            d = dir.up;
        }
        else if(Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S)) {
            if(st!=state.standby)st = state.walking;
            speed.y = -speedmax;
            d = dir.down;
        }
        else if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)) {
            if(st!=state.standby)st = state.walking;
            speed.y = -speedmax;
            d = dir.left;
        }
        else if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)) {
            if(st!=state.standby)st = state.walking;
            speed.y += speedmax;
            d = dir.right;
        }
        else
        {
            st = state.standby;
            speed.x = 0;
            speed.y = 0;
        }
    }   
}

我对我的角色类所做的是,它将包含我的角色的所有输入,将动画渲染到特定的状态和方向。

4

1 回答 1

1
 else if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)) {

            if(st!=state.standby)st = state.walking;
            speed.y = -speedmax;
            d = dir.left;

        }


        else if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)) {

            if(st!=state.standby)st = state.walking;
            speed.y += speedmax;
            d = dir.right;

        }

你不认为你应该在这段代码中使用 speed.x 而不是 speed.y :)

于 2013-07-25T10:27:38.330 回答