直接上代码吧
1 package com.mygdx.game; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.graphics.g2d.Animation; 5 import com.badlogic.gdx.graphics.g2d.Batch; 6 import com.badlogic.gdx.graphics.g2d.TextureRegion; 7 import com.badlogic.gdx.scenes.scene2d.Actor; 8 import com.badlogic.gdx.scenes.scene2d.InputEvent; 9 import com.badlogic.gdx.scenes.scene2d.InputListener; 10 11 /** 12 * Created by HanHongmin on 14-7-20. 13 */ 14 public class Plane extends Actor { 15 private Animation animation; 16 private float stateTime; 17 18 public Plane(Animation animation){ 19 this.animation = animation; 20 setBounds(getX(), getY(), animation.getKeyFrames()[0].getRegionWidth(), animation.getKeyFrames()[0].getRegionHeight()); 21 22 this.addListener(new InputListener(){ 23 public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){ 24 System.out.println("Touched" + getName()); 25 return true; 26 } 27 }); 28 } 29 30 public void draw(Batch batch, float alpha){ 31 stateTime = stateTime+Gdx.graphics.getDeltaTime(); 32 33 TextureRegion toDraw = animation.getKeyFrame(stateTime); 34 35 batch.draw(toDraw, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), 36 getScaleX(), getScaleY(), getRotation()); 37 } 38 39 40 }
1 package com.mygdx.game; 2 3 import com.badlogic.gdx.ApplicationListener; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.Input; 6 import com.badlogic.gdx.graphics.GL20; 7 import com.badlogic.gdx.graphics.Texture; 8 import com.badlogic.gdx.graphics.g2d.Animation; 9 import com.badlogic.gdx.graphics.g2d.TextureRegion; 10 import com.badlogic.gdx.scenes.scene2d.*; 11 import com.badlogic.gdx.utils.Array; 12 13 public class MyGdxGame implements ApplicationListener { 14 private Plane plane; 15 private Stage stage; 16 private Texture t1; 17 private Texture t2; 18 private Texture t3; 19 20 private float moveSpeed = 200; 21 22 @Override 23 public void create() { 24 stage = new Stage(); 25 26 t1 = new Texture("plane1.png"); 27 t2 = new Texture("plane2.png"); 28 t3 = new Texture("plane3.png"); 29 Array<TextureRegion> frames = new Array(4); 30 frames.add(new TextureRegion(t1)); 31 frames.add(new TextureRegion(t2)); 32 frames.add(new TextureRegion(t3)); 33 frames.add(new TextureRegion(t2)); 34 35 Animation animation = new Animation(0.05f,frames, Animation.PlayMode.LOOP); 36 37 plane = new Plane(animation); 38 39 plane.setCenterPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); 40 plane.setName("飞机1"); 41 42 stage.addActor(plane); 43 44 Gdx.input.setInputProcessor(stage); 45 } 46 47 @Override 48 public void dispose() { 49 t1.dispose(); 50 t2.dispose(); 51 t3.dispose(); 52 stage.dispose(); 53 } 54 55 @Override 56 public void render () { 57 Gdx.gl.glClearColor(0, 0, 0, 1); 58 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 59 float time = Gdx.graphics.getDeltaTime(); 60 if(Gdx.input.isKeyPressed(Input.Keys.W)){ 61 plane.setY(plane.getY()+moveSpeed*time); 62 } 63 if(Gdx.input.isKeyPressed(Input.Keys.A)){ 64 if(plane.getScaleX()==1){ 65 plane.setScaleX(-1); 66 plane.setX(plane.getX()+plane.getWidth()); 67 } 68 69 plane.setX(plane.getX()-moveSpeed*time); 70 } 71 if(Gdx.input.isKeyPressed(Input.Keys.S)){ 72 plane.setY(plane.getY()-moveSpeed*time); 73 } 74 if(Gdx.input.isKeyPressed(Input.Keys.D)){ 75 if(plane.getScaleX()==-1){ 76 plane.setScaleX(1); 77 plane.setX(plane.getX()-plane.getWidth()); 78 } 79 80 plane.setX(plane.getX()+moveSpeed*time); 81 } 82 83 stage.act(Gdx.graphics.getDeltaTime()); 84 stage.draw(); 85 } 86 87 @Override 88 public void resize(int width, int height) { 89 } 90 91 @Override 92 public void pause() { 93 } 94 95 @Override 96 public void resume() { 97 } 98 }
三张图片资源如下:
//==============add 2014-07-22
代码控制图片翻转的地方总觉得不妥,用的方法不对。
2dx里面有个锚点可以控制,翻转旋转都是以锚点为基点的,libgdx里面要手动控制吗?怎么想怎么不对。
看看源码发现有个Origin属性,测试看看。
plane.setOrigin(plane.getWidth()/2,plane.getHeight()/2);
没有问题~·
最后加上了描框,方便测试以及调试碰撞检测等~~
全代码如下:
1 package com.mygdx.game; 2 3 import com.badlogic.gdx.ApplicationListener; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.Input; 6 import com.badlogic.gdx.graphics.Color; 7 import com.badlogic.gdx.graphics.GL20; 8 import com.badlogic.gdx.graphics.Texture; 9 import com.badlogic.gdx.graphics.g2d.Animation; 10 import com.badlogic.gdx.graphics.g2d.TextureRegion; 11 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 12 import com.badlogic.gdx.math.Rectangle; 13 import com.badlogic.gdx.scenes.scene2d.*; 14 import com.badlogic.gdx.utils.Array; 15 16 public class MyGdxGame implements ApplicationListener { 17 private Plane plane; 18 private Stage stage; 19 private Texture t1; 20 private Texture t2; 21 private Texture t3; 22 23 private float moveSpeed = 200; 24 ShapeRenderer debugRenderer; 25 26 @Override 27 public void create() { 28 stage = new Stage(); 29 30 t1 = new Texture("plane1.png"); 31 t2 = new Texture("plane2.png"); 32 t3 = new Texture("plane3.png"); 33 Array<TextureRegion> frames = new Array(4); 34 frames.add(new TextureRegion(t1)); 35 frames.add(new TextureRegion(t2)); 36 frames.add(new TextureRegion(t3)); 37 frames.add(new TextureRegion(t2)); 38 39 Animation animation = new Animation(0.05f,frames, Animation.PlayMode.LOOP); 40 41 plane = new Plane(animation); 42 43 plane.setCenterPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); 44 //plane.setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); 45 plane.setOrigin(plane.getWidth()/2,plane.getHeight()/2); 46 plane.setName("飞机1"); 47 48 stage.addActor(plane); 49 50 Gdx.input.setInputProcessor(stage); 51 52 debugRenderer = new ShapeRenderer(); 53 54 debugRenderer.setProjectionMatrix(stage.getCamera().combined); 55 } 56 57 @Override 58 public void dispose() { 59 t1.dispose(); 60 t2.dispose(); 61 t3.dispose(); 62 stage.dispose(); 63 } 64 65 @Override 66 public void render () { 67 Gdx.gl.glClearColor(0, 0, 0, 1); 68 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 69 float time = Gdx.graphics.getDeltaTime(); 70 if(Gdx.input.isKeyPressed(Input.Keys.W)){ 71 plane.setY(plane.getY()+moveSpeed*time); 72 } 73 if(Gdx.input.isKeyPressed(Input.Keys.A)){ 74 //System.out.println(plane.getRotation()); 75 if(plane.getScaleX()==1){ 76 plane.setScaleX(-1); 77 //plane.setX(plane.getX()+plane.getWidth()); 78 } 79 //plane.setRotation(plane.getRotation()+1); 80 plane.setX(plane.getX()-moveSpeed*time); 81 } 82 if(Gdx.input.isKeyPressed(Input.Keys.S)){ 83 plane.setY(plane.getY()-moveSpeed*time); 84 } 85 if(Gdx.input.isKeyPressed(Input.Keys.D)){ 86 if(plane.getScaleX()==-1){ 87 plane.setScaleX(1); 88 //plane.setX(plane.getX()-plane.getWidth()); 89 } 90 91 plane.setX(plane.getX()+moveSpeed*time); 92 } 93 94 stage.act(Gdx.graphics.getDeltaTime()); 95 stage.draw(); 96 97 drawDebug(); 98 } 99 100 private void drawDebug(){ 101 debugRenderer.begin(ShapeRenderer.ShapeType.Line); 102 103 Rectangle rect = new Rectangle(plane.getX(), plane.getY(), plane.getWidth(), plane.getHeight()); 104 //float x1 = plane.getX() + rect.x; 105 //float y1 = plane.getY() + rect.y; 106 debugRenderer.setColor(new Color(0, 1, 0, 1)); 107 debugRenderer.rect(rect.x, rect.y, rect.width, rect.height); 108 debugRenderer.end(); 109 } 110 111 @Override 112 public void resize(int width, int height) { 113 } 114 115 @Override 116 public void pause() { 117 } 118 119 @Override 120 public void resume() { 121 } 122 }
//================add 2014-07-23显示FPS
1. 在create函数中添加代码
1 Label.LabelStyle labelStyle = new Label.LabelStyle(new BitmapFont(), Color.WHITE); //创建一个Label样式 2 Label label = new Label("FPS:", labelStyle); //创建标签,显示的文字是FPS: 3 label.setName("fpsLabel"); //设置标签名称为fpsLabel 4 label.setY(0); //设置Y为0,即显示在最下面 5 label.setX(0); //设置X值,显示为最后一个字紧靠屏幕最右侧 6 stage.addActor(label); //将标签添加到舞台
2. 在render函数中设置当前FPS值
1 Label label = (Label) stage.getRoot().findActor("fpsLabel"); //获取名为fpsLabel的标签 2 label.setText("FPS:" + Gdx.graphics.getFramesPerSecond());
本例中已可以显示FPS。
通常来讲一个游戏一般有两个camera,一个用于显示游戏类的动画等,另一个用于显示UI。stage中默认是有一个camera的。
如果共同使用一个SpriteBatch:
1 sb.end(); 2 sb.setProjectionMatrix(uiCamera.combined);//往视角里画东西 3 sb.begin(); 4 label.draw(sb,1); 5 sb.end();
用一个SpriteBatch,一个stage应该也是可以的。