首页 > 代码库 > libgdx Actor
libgdx Actor
直接上代码吧
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 @Override23 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 @Override48 public void dispose() {49 t1.dispose();50 t2.dispose();51 t3.dispose();52 stage.dispose();53 }54 55 @Override56 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 @Override88 public void resize(int width, int height) {89 }90 91 @Override92 public void pause() {93 }94 95 @Override96 public void resume() {97 }98 }
三张图片资源如下:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。