首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在游戏过程中重新定位移动动画?

如何在游戏过程中重新定位移动动画?
EN

Stack Overflow用户
提问于 2017-06-08 21:33:02
回答 0查看 48关注 0票数 0

我正在使用Android Studio IDE和libGDX框架来开发一个游戏。它是Pac-Man游戏的克隆,具有类似于flappy bird的游戏玩法。概念是,pac人在木板上移动,同时避开来自右侧的幽灵,这些幽灵直接向左侧移动(而不是追逐玩家的位置)。我不确定我如何为鬼魂动画创建一个'for循环‘,我希望鬼魂在几秒钟后一致地重新定位并从右侧重新出现,除了它们最初完全离开屏幕之外。

其中一个幽灵的班级。

代码语言:javascript
复制
public class Blinky {

private Vector3 position; //x y and z axis
private Rectangle bounds;
private Texture texture1;
private Animation blinkyAnimLeft;

public Blinky(int x, int y) {

    position = new Vector3(x, y, 0);
    Texture texture1 = new Texture("blinkyLeft.png");
    blinkyAnimLeft = new Animation(new TextureRegion(texture1), 2, 0.5f);
    //bounds = new Rectangle(x,y,texture1.getWidth() / 2, texture1.getHeight());
}

public void update(float dt) {

    blinkyAnimLeft.update(dt);
   //bounds.setPosition(position.x, position.y);
}

public Vector3 getPosition() {
   return position;
}

public TextureRegion getTexture() {
    return blinkyAnimLeft.getFrame();
}

//public Rectangle getBounds() {
    return bounds;
}

  public void dispose() {
    texture1.dispose();
  }
}

在GamePlayState类中初始化的Ghost和Player

代码语言:javascript
复制
public class GamePlayState extends State  {
//Variables
private float timePassed = 0;
private Texture background;
public static final int WALK = 1;
public static final double GHOST_WALK = 0.5;
private static final int PLANKS_SPACING = 125;   //gap betwen the planks
private static final int PLANK_COUNT = 4;
private Array<Obstacle> planks;
private Player player;
private Blinky blinky;
private Inky   inky;
private Texture missile;

public GamePlayState(GameStateManager gsm) {
    super(gsm);
    player = new Player(50, 100);
    blinky = new Blinky(400, 220);
    inky = new Inky(400, 240);
    //   missile = new Texture("missile.png");

    background = new Texture("black.jpg");
    cam.setToOrtho(false, PacMan.WIDTH/2, PacMan.HEIGHT/2);

    planks = new Array<Obstacle>();

    for (int i = 1; i<= PLANK_COUNT; i++) {
        planks.add(new Obstacle(i * (PLANKS_SPACING + Obstacle.PLANK_WIDTH)));
    }
}

@Override
public void handleInput() {

}

@Override
public void update(float dt) {
    handleInput();
    player.update(dt);
    blinky.update(dt);
    inky.update(dt);
    cam.position.x = player.getPosition().x + 80;    //update the position of the camera with the bird

    //update when pacman cam viewport has passed plank
    //make cam follow the player
    for (Obstacle plank: planks) {

        if (cam.position.x - (cam.viewportWidth/1) > plank.getPosTopPlank().x + plank.getTopPlank().getWidth()) {

            plank.respositionPlanks(plank.getPosTopPlank().x + ((Obstacle.PLANK_WIDTH + PLANKS_SPACING * PLANK_COUNT )));
        }
        if (plank.collision (player.getBounds()))
        gsm.set(new GamePlayState(gsm));
    }
    cam.update();
    }

@Override
public void render(SpriteBatch sb) {

    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(background, cam.position.x - (cam.viewportWidth/2), 0);
    timePassed += Gdx.graphics.getDeltaTime();

    //Moving Inky
    sb.draw(inky.getTexture(), inky.getPosition().x, inky.getPosition().y);
    inky.getPosition().x -= GHOST_WALK;

    //Moving Blinky
    sb.draw(blinky.getTexture(), blinky.getPosition().x, blinky.getPosition().y);
    blinky.getPosition().x -= GHOST_WALK;
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44437188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档