在我自上而下的游戏中,我有一个敌人探测器来探测敌人是否在附近.我的问题是我如何创建动态我的仪表栏的过渡?我是这个框架的新手.谢谢和进步
当玩家检测到附近的敌人时,仪表栏将像下面的图像屏幕一样动画
没有敌人
我的代码:
//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 functionsMotionState 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;}
希望这对你有所帮助,祝你好运:)
总结以上是内存溢出为你收集整理的android – 如何在libgdx中制作敌人仪表探测器?全部内容,希望文章能够帮你解决android – 如何在libgdx中制作敌人仪表探测器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)