Android使用libgdx实现模拟方向键控制角色移动的方法
本文实例讲述了Android使用libgdx实现模拟方向键控制角色移动的方法。分享给大家供大家参考,具体如下:
packagecom.demo; importandroid.os.Bundle; importcom.badlogic.gdx.backends.android.AndroidApplication; //Libgdx的Texture与Sprite使用 publicclassLibgdxActivityextendsAndroidApplication{ publicvoidonCreate(Bundlebundle){ super.onCreate(bundle); //初始化游戏屏幕,并设置是否支持GLES2.0,如果您对向下兼容没什么需要选择true即可(2.1以上),否则选择false。 //initialize(newFirstGame(),true); initialize(newBox2DDemo(),true); } }
packagecom.demo; importcom.badlogic.gdx.Gdx; importcom.badlogic.gdx.graphics.Texture; importcom.badlogic.gdx.graphics.g2d.SpriteBatch; importcom.badlogic.gdx.scenes.scene2d.Actor; publicclassFirstActorextendsActor{ privateTexturetexture; @Override publicvoiddraw(SpriteBatchbatch,floatarg1){ batch.draw(texture,this.x,this.y); } @Override publicActorhit(floatarg0,floatarg1){ if(x>0&&y>0&&this.height>y&&this.width>x){ returnthis; }else{ returnnull; } } @Override publicbooleantouchDown(floatarg0,floatarg1,intarg2){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicvoidtouchDragged(floatarg0,floatarg1,intarg2){ //TODOAuto-generatedmethodstub } @Override publicvoidtouchUp(floatarg0,floatarg1,intarg2){ //TODOAuto-generatedmethodstub } publicFirstActor(Stringname){ super(name); texture=newTexture(Gdx.files.internal("bt.png")); this.height=texture.getHeight(); this.width=texture.getWidth(); } }
packagecom.demo; importandroid.util.Log; importcom.badlogic.gdx.ApplicationListener; importcom.badlogic.gdx.Gdx; importcom.badlogic.gdx.graphics.GL10; importcom.badlogic.gdx.graphics.Texture; importcom.badlogic.gdx.graphics.g2d.NinePatch; importcom.badlogic.gdx.scenes.scene2d.Actor; importcom.badlogic.gdx.scenes.scene2d.Stage; importcom.badlogic.gdx.scenes.scene2d.ui.Button; importcom.badlogic.gdx.scenes.scene2d.ui.ClickListener; importcom.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle; classFirstGameimplementsApplicationListener,ClickListener{ privatestaticStringUP="up"; privatestaticStringDOWN="down"; privatestaticStringLEFT="left"; privatestaticStringRIGHT="right"; //舞台 privateStagestage; //演员 privateActorfirstActor; privateTexturetexture; privateButtonbuttonUp,buttonDown,buttonLeft,buttonRight; privateNinePatchpatch1,patch2; @Override publicvoidcreate(){ stage=newStage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),true); firstActor=newFirstActor("renwu"); buttonUp=initButton(UP,40,80); buttonDown=initButton(DOWN,40,0); buttonLeft=initButton(LEFT,0,40); buttonRight=initButton(RIGHT,80,40); buttonUp.setClickListener(this); buttonDown.setClickListener(this); buttonLeft.setClickListener(this); buttonRight.setClickListener(this); stage.addActor(firstActor); stage.addActor(buttonUp); stage.addActor(buttonDown); stage.addActor(buttonLeft); stage.addActor(buttonRight); Gdx.input.setInputProcessor(stage); } @Override publicvoidrender(){ //清屏 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stage.act(Gdx.graphics.getDeltaTime()); stage.draw(); } @Override publicvoiddispose(){ //释放占用的资源 stage.dispose(); } @Override publicvoidresume(){ } @Override publicvoidpause(){ } @Override publicvoidresize(intwidth,intheight){ } publicButtoninitButton(Stringname,intx,inty){ if(name.equals(UP)){ texture=newTexture(Gdx.files.internal("up_alt.png")); }elseif(name.equals(DOWN)){ texture=newTexture(Gdx.files.internal("down_alt.png")); }elseif(name.equals(LEFT)){ texture=newTexture(Gdx.files.internal("back_alt.png")); }elseif(name.equals(RIGHT)){ texture=newTexture(Gdx.files.internal("forward_alt.png")); } patch1=newNinePatch(texture,0,0,0,0); Buttonbutton=newButton(newButtonStyle(patch1,patch1,patch1,0f,0f,0f,0f,null,null),name); button.x=x; button.y=y; button.width=32; button.height=32; returnbutton; } @Override publicvoidclick(Actorbutton){ if(button.equals(buttonUp)){ Actoractor=button.parent.findActor("renwu");; actor.y+=10; Log.i("touch","up"); }elseif(button.equals(buttonDown)){ Actoractor=button.parent.findActor("renwu");; actor.y-=10; Log.i("touch","down"); }elseif(button.equals(buttonLeft)){ Actoractor=button.parent.findActor("renwu");; actor.x-=10; Log.i("touch","left"); }elseif(button.equals(buttonRight)){ Actoractor=button.parent.findActor("renwu");; actor.x+=10; Log.i("touch","right"); } } }
希望本文所述对大家Android程序设计有所帮助。