码迷,mamicode.com
首页 > 其他好文 > 详细

libgdx Actor

时间:2014-07-21 23:30:20      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   width   

直接上代码吧

 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 }

 

三张图片资源如下:

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

libgdx Actor,布布扣,bubuko.com

libgdx Actor

标签:style   blog   http   color   os   width   

原文地址:http://www.cnblogs.com/hanhongmin/p/3858835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!