(LibGDX)DeltaTime与精灵运动并不完全相同

时间:2017-04-30 01:44:40

标签: java android libgdx tile 2d-games

我有几个精灵跟随TileMap中的某个路径,我将精灵的运动基于DeltaTime。由于DeltaTime变化,它会导致一些精灵与其他精灵重叠/过去。请注意,精灵必须在TileMap中向下一个单元格,如果它到达某个图块,则向相反方向向下。这是精灵运动的代码:



package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Vector2;


public class CentipedeBody1 extends Sprite {

    enum State{
        LEFT,RIGHT,DOWN
    }

    State currentState,previousState ;

    public static final float DOWN_MOVEMENT=7f;
    public float downMovCounter;
    public float speed;
    public float stateTime;
    TextureRegion image;
    Vector2 position,size;
    Animation<TextureRegion> animation;
    TextureRegion currentFrame;
    TextureRegion[]frames;
    Texture tilesImage = new Texture(Gdx.files.internal("tile.png"));
    TextureRegion[][] splitTiles = TextureRegion.split(tilesImage, 8, 8);
    public CentipedeBody1(TextureRegion image, Vector2 position, Vector2 size) {
        super(new TextureRegion(image));
        setPosition(position.x,position.y);
        setSize(size.x,size.y);
        currentState=State.LEFT;
        previousState=State.LEFT;
        speed=8f;

   
    }

    public void update(TiledMap map) {

        float delta=Gdx.graphics.getDeltaTime();
 

        if(currentState ==State.LEFT){
          setPosition(getX()-speed*delta,getY());

            if(getX()<0) {
                previousState=currentState;
                currentState = State.DOWN;
                setFlip(true,false);
            }
        }

        if(currentState ==State.RIGHT){
            setPosition(getX()+speed*delta,getY());
            if(getX()> 19) {
                previousState=currentState;
                currentState = State.DOWN;
                setFlip(false,false);

            }
        }

        if(currentState ==State.DOWN){
            setPosition(getX(),getY()-1);
        
           
                downMovCounter=0;
                currentState =previousState==State.LEFT?
              State.RIGHT:State.LEFT;
           

        }



        TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(2);
        TiledMapTileLayer.Cell cell = new TiledMapTileLayer.Cell();


        if(cur.getCell(Math.round(getX()),Math.round(getY())).getTile().getProperties().containsKey("mushroom"))
        {


            if(getCurrentState()==State.LEFT)
            {
                setFlip(true,false);

            }
            else if(getCurrentState()==State.RIGHT)
            {
                setFlip(false,false);
            }

            previousState=currentState;
            currentState = State.DOWN;



        }
            }
&#13;
&#13;
&#13;

0 个答案:

没有答案