在我自上而下的游戏中,我有一个敌人探测器来探测敌人是否在附近.我的问题是我如何创建动态我的仪表栏的过渡?我是这个框架的新手.谢谢和进步
当玩家检测到附近的敌人时,仪表栏将像下面的图像屏幕一样动画
没有敌人
我的代码:
//detector
meter_bar = new Texture("meter_bar.png");
myTextureRegion = new TextureRegion(meter_bar);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
actormeter_bar = new Image(myTexRegionDrawable);
actormeter_bar.setPosition(200,1005);
stage.addActor(actormeter_bar);
meter = new Texture("meter.png");
myTextureRegion = new TextureRegion(meter);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
actormetter = new Image(myTexRegionDrawable);
actormetter.setPosition(190,990);
stage.addActor( actormetter);
球员运动(前锋):
//right_control
right_paw = new Texture(Gdx.files.internal("right_paw.png"));
myTextureRegion = new TextureRegion(right_paw);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
moveForward = new ImageButton(myTexRegionDrawable); //Set the button up
moveForward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
//the hover
moveForward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw-hover.png"))));
moveForward.setPosition(520,110);
stage.addActor(moveForward); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
moveForward.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Right Button Pressed");
progressKnobX = progressKnobX + 4;
actorprogressknob.setX(progressKnobX); // x-position to move to
if(progressKnobX > 418 ) progressKnobX= 418 ;
music.play();
motionState=MotionState.NONE;
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
motionState=MotionState.UP;
return true;
}
});
stage.addActor(moveForward);
敌人精灵运动:
zombie = new Texture(Gdx.files.internal("enemy/ZombieSprite.png"));
zombiesprite = new Sprite(zombie);
zombieenemy = new Rectangle();
zombieenemy.setWidth(zombiesprite.getWidth());
zombieenemy = zombiesprite.getBoundingRectangle();
zombieenemy.x = zombieX;
zombieenemy.y = zombieY;
TextureRegion[][] zom = TextureRegion.split(zombie, zombie.getWidth() / Zombie_FRAME_COLS, zombie.getHeight() / Zombie_FRAME_ROWS);
TextureRegion[] zwalkFrames = new TextureRegion[Zombie_FRAME_COLS * Zombie_FRAME_ROWS];
index = 0;
for (int i = 0; i < Zombie_FRAME_ROWS; i++) {
for (int j = 0; j < Zombie_FRAME_COLS; j++) {
zwalkFrames[index++] = zom[i][j];
}
}
zombiewalkAnimation = new Animation<TextureRegion>(0.120f, zwalkFrames);
MoveForward按钮功能:
//button functions
MotionState motionState=MotionState.NONE;
enum MotionState {
NONE {
@Override
public boolean update(Rectangle player) {
return true;
}
},
UP {
@Override
public boolean update(Rectangle player) {
player.y += 300 * Gdx.graphics.getDeltaTime();
return false;
}
},
DOWN{
@Override
public boolean update(Rectangle player) {
player.y -= 300 * Gdx.graphics.getDeltaTime();
return false;
}
},
LEFT{
@Override
public boolean update(Rectangle player) {
player.x -= 100 * Gdx.graphics.getDeltaTime();
return false;
}
},
RIGHT{
@Override
public boolean update(Rectangle player) {
player.x += 100 * Gdx.graphics.getDeltaTime();
return false;
}
};
public abstract boolean update(Rectangle player);
}
给予
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) motionState = MotionState.DOWN;
if(Gdx.input.isKeyPressed(Input.Keys.UP)) motionState=MotionState.UP;
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) motionState=MotionState.LEFT;
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) motionState=MotionState.RIGHT;
if(motionState.update(player)) motionState=MotionState.NONE;
if(player.y> 1910){
zombieenemy.y -= 60 * Gdx.graphics.getDeltaTime();
}
解决方法:
在我回答你的初步问题之前,我想建议你学会使用Libgdx extension Ashley,因为事情的样子非常混乱,你很难维持.
我的答案:
持有一个ArrayList<>或类似的数据模型与你内部的所有游戏敌人,然后我们将遍历每一个并获得它的位置并计算与玩家的距离.
Enemy closestEnemy = null;
float closestEnemyDist = -1;
for(Enemy enemy : ListOfEnemies){
/* here we find the distance to the player */
float distToPlayer = Math.sqrt((enemy.y-player.x)^2 + (enemy.y-player.y)^2);
/* initiate closestEnemyDist for the first time */
if(closestEnemyDist == -1){
closestEnemyDist = distToPlayer;
}
/* we find the closest distance to the player */
float minDist = Math.min(closestEnemyDist, distToPlayer);
/* we make sure it is the closest and save the closest enemy as well */
if(minDist <= closestEnemyDist){
closestEnemyDist = minDist;
closestEnemy = enemy;
}
}
这给你最近的敌人和它的距离,你可以制定它的方法或只是你想要的距离,你可以在每次游戏更新时运行此代码.
距离计:
在这里我们要做一个小数学,让我们说你的仪表显示百分比……从0%到100%,你必须选择你的仪表认为0%的最大距离(意味着敌人称之为最大距离),对于你来说很容易理解这个想象,距离是100,看看这个方法.
public int calcMeter(float closestEnemyDist, float maxDistance) {
int meter = 0;
/* calculate only if enemy is in range */
if(closestEnemyDist < maxDistance){
/* using rounding and multiply by 100f because we wanted
* values from 0 - 100, you can can modify this to work just
* with float from 0 - 1f
*/
meter = Math.round(
/*
* here we flip the percentage so if the enemy is very
* close then your meter will be higher
*/
(1f - (closestEnemyDist / maxDistance)) * 100f
);
}
return meter;
}
希望这对你有所帮助,祝你好运:)