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

6、Actor,Stage的学习

时间:2015-03-18 10:22:00      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

今天总得东西比较多吧。到现在才把学习的结合起来用,其实,啥也没有做。不废话了

1、Actor,演员类

2、Stage,舞台类

  今天2个结合起来,看看效果,今天实现的是魂斗罗的界面,由于多学的比较少,所以比嘛,比较粗糙吧。

  思路吗,先从欢迎页面,点击2个人物,进入战斗界面,战斗界面通过2个按钮控制人物的左右移动,地图也随之移动。

  这个用到了之前的知识,正好加强一下吧

  此次用到了,Animation,TextureRegion,Texture,ImageButton,Stage,Actor

  对于字体样式的就没有使用到了

  废话不多说,上码

   MapActor主要是用来管理地图的移动,人物的移动

  1 package com.Actor;
  2 
  3 import com.badlogic.gdx.Gdx;
  4 import com.badlogic.gdx.graphics.Texture;
  5 import com.badlogic.gdx.graphics.g2d.Animation;
  6 import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
  7 import com.badlogic.gdx.graphics.g2d.Batch;
  8 import com.badlogic.gdx.graphics.g2d.TextureRegion;
  9 import com.badlogic.gdx.scenes.scene2d.Actor;
 10 import com.badlogic.gdx.scenes.scene2d.InputEvent;
 11 import com.badlogic.gdx.scenes.scene2d.InputListener;
 12 import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
 13 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
 14 import com.badlogic.gdx.utils.Array;
 15 
 16 public class MapActor extends Actor {
 17     /** 地图 */
 18     Texture T_map;
 19     /** 左边按钮 */
 20     ImageButton IB_left;
 21     /** 右边按钮 */
 22     ImageButton IB_right;
 23     /** 地图坐标 */
 24     float mapX;
 25     /** 人物坐标 */
 26     float heroX;
 27     /** 当前状态 */
 28     STATE currentState;
 29     /** 左行动画 */
 30     Animation A_left;
 31     /** 右行动画 */
 32     Animation A_right;
 33     /** 动画时间 */
 34     float animtionTime;
 35     int a = 4;
 36 
 37     enum STATE {
 38         RIGHT, LEFT, LEFTNONE, RIGHTNONE
 39     }
 40 
 41     public MapActor() {
 42         init();
 43     }
 44 
 45     private void init() {
 46         initMap();
 47         initIB_left();
 48         initIB_right();
 49         initAnimation();
 50     }
 51 
 52     /**
 53      * 左边按钮
 54      * 
 55      * @author 铁弟<br>
 56      *         QQ:395698388<br>
 57      *         时间:2015-3-16 <br>
 58      */
 59     private void initIB_left() {
 60         Texture t = new Texture(Gdx.files.internal("img/bt.png"));
 61         TextureRegion[][] tr = TextureRegion.split(t, t.getWidth() / 2, t.getHeight());
 62         IB_left = new ImageButton(new TextureRegionDrawable(tr[0][1]), new TextureRegionDrawable(tr[0][0]));
 63         IB_left.setPosition(0, 30);
 64         IB_left.addListener(new InputListener() {
 65 
 66             @Override
 67             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
 68                 currentState = STATE.LEFT;
 69                 return true;
 70             }
 71 
 72             @Override
 73             public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
 74                 currentState = STATE.LEFTNONE;
 75                 super.touchUp(event, x, y, pointer, button);
 76             }
 77 
 78         });
 79     }
 80 
 81     /**
 82      * 右边按钮
 83      * 
 84      * @author 铁弟<br>
 85      *         QQ:395698388<br>
 86      *         时间:2015-3-16 <br>
 87      */
 88     private void initIB_right() {
 89         Texture t = new Texture(Gdx.files.internal("img/bt.png"));
 90         TextureRegion[][] tr = TextureRegion.split(t, t.getWidth() / 2, t.getHeight());
 91         tr[0][0].flip(true, false);
 92         tr[0][1].flip(true, false);
 93         IB_right = new ImageButton(new TextureRegionDrawable(tr[0][1]), new TextureRegionDrawable(tr[0][0]));
 94         IB_right.setPosition(Gdx.graphics.getWidth() - IB_right.getWidth(), 30);
 95         IB_right.addListener(new InputListener() {
 96 
 97             @Override
 98             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
 99                 currentState = STATE.RIGHT;
100                 return true;
101             }
102 
103             @Override
104             public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
105                 currentState = STATE.RIGHTNONE;
106                 super.touchUp(event, x, y, pointer, button);
107             }
108 
109         });
110     }
111 
112     private void initMap() {
113         T_map = new Texture("img/map_1.png");
114     }
115 
116     public ImageButton getIB_left() {
117         return IB_left;
118     }
119 
120     public ImageButton getIB_right() {
121         return IB_right;
122     }
123 
124     /**
125      * 初始化动画
126      * 
127      * @author 铁弟<br>
128      *         QQ:395698388<br>
129      *         时间:2015-3-16 <br>
130      */
131     private void initAnimation() {
132         Texture T_anim = new Texture("img/hdl.png");
133         A_left = initA(T_anim, T_anim.getWidth() / 3, T_anim.getHeight(), true);
134         A_right = initA(T_anim, T_anim.getWidth() / 3, T_anim.getHeight(), false);
135     }
136 
137     private Animation initA(Texture T_anim, int splitWidth, int splitHeight, boolean isLeft) {
138         TextureRegion[][] TR_anim = TextureRegion.split(T_anim, splitWidth, splitHeight);
139         Array<TextureRegion> a = new Array<TextureRegion>();
140         for (TextureRegion[] tr : TR_anim) {
141             for (TextureRegion t : tr) {
142                 t.flip(!isLeft, false);
143                 a.add(t);
144             }
145         }
146         Animation aa = new Animation(0.1f, a);
147         aa.setPlayMode(PlayMode.LOOP);
148         return aa;
149     }
150 
151     /**
152      * 修改当前帧
153      * 
154      * @author 铁弟<br>
155      *         QQ:395698388<br>
156      *         时间:2015-3-16 <br>
157      * @param name
158      * @param time
159      */
160     private TextureRegion changeCurrentTR(STATE name, float time) {
161         TextureRegion TR_current;
162         if (name == STATE.LEFT) {// 左行
163             TR_current = A_left.getKeyFrame(time, true);
164         } else if (name == STATE.LEFTNONE) {// 左停
165             TR_current = A_left.getKeyFrame(0, true);
166         } else if (name == STATE.RIGHT) {// 右行
167             TR_current = A_right.getKeyFrame(time, true);
168         } else if (name == STATE.RIGHTNONE) {// 右停
169             TR_current = A_right.getKeyFrame(0, true);
170         } else {
171             TR_current = A_left.getKeyFrame(0, true);
172         }
173         changeCurrentX(name, TR_current);
174         return TR_current;
175     }
176 
177     /**
178      * 检测是否越过坐标
179      * 
180      * @author 铁弟<br>
181      *         QQ:395698388<br>
182      *         时间:2015-3-16 <br>
183      * @param name
184      */
185     private void changeCurrentX(STATE name, TextureRegion TR_current) {
186         if (name == STATE.LEFT) {// 左行
187             // 判断地图是否越界了
188             if (mapX >= 0) {
189                 mapX = 0;
190                 heroX -= a;
191                 if (heroX <= 0) {
192                     heroX = 0;
193                 }
194             } else {
195                 if (heroX > (Gdx.graphics.getWidth() - 55) / 2) {
196                     heroX -= a;
197                 } else {
198                     heroX = (Gdx.graphics.getWidth() - 55) / 2;
199                     mapX += a;
200                 }
201             }
202         } else if (name == STATE.RIGHT) {// 右行
203             // 判断地图是否越界了
204             if (T_map.getWidth() + mapX <= Gdx.graphics.getWidth()) {
205                 mapX = -(T_map.getWidth() - Gdx.graphics.getWidth());
206                 heroX += a;
207                 if (heroX > (Gdx.graphics.getWidth() - 55)) {
208                     heroX = Gdx.graphics.getWidth() - 55;
209                 }
210             } else {
211                 if (heroX < (Gdx.graphics.getWidth() - 55) / 2) {
212                     heroX += a;
213                 } else {
214                     heroX = (Gdx.graphics.getWidth() - 55) / 2;
215                     mapX -= a;
216                 }
217             }
218         }
219     }
220 
221     @Override
222     public void draw(Batch batch, float parentAlpha) {
223         // 绘制地图
224         batch.draw(T_map, mapX, (Gdx.graphics.getHeight() - T_map.getHeight()) / 2);
225         animtionTime += Gdx.graphics.getDeltaTime();
226         // 绘制人物
227         batch.draw(changeCurrentTR(currentState, animtionTime), heroX, (T_map.getHeight() - 55) / 2 + (Gdx.graphics.getHeight() - T_map.getHeight()) / 2, 55, 55);
228     }
229 }

  

    关于舞台的切换

    这里我使用了接口回调,主要是让MyGdxGame知道是哪个舞台发起的切换指令

    舞台的切换我这里通过boolean变量在控制的

 1 package com.mygdx.game;
 2 
 3 import com.Stage.GameStage;
 4 import com.Stage.IndexStage;
 5 import com.badlogic.gdx.ApplicationAdapter;
 6 import com.badlogic.gdx.Gdx;
 7 import com.badlogic.gdx.graphics.GL20;
 8 import com.badlogic.gdx.scenes.scene2d.Stage;
 9 
10 public class MyGdxGame extends ApplicationAdapter implements MyChangeListener {
11     /** index舞台 */
12     IndexStage S_index;
13     /** 游戏舞台 */
14     GameStage S_game;
15     /** 主页是否已经显示 */
16     boolean isShowIndex = true;
17     /** 是否显示游戏页 */
18     boolean isShowGame = false;
19 
20     @Override
21     public void create() {
22         S_index = new IndexStage(this);
23         Gdx.input.setInputProcessor(S_index);
24     }
25 
26     @Override
27     public void resize(int width, int height) {
28         super.resize(width, height);
29     }
30 
31     @Override
32     public void pause() {
33         super.pause();
34     }
35 
36     @Override
37     public void resume() {
38         super.resume();
39     }
40 
41     @Override
42     public void dispose() {
43         super.dispose();
44     }
45 
46     float time = 0;
47 
48     @Override
49     public void render() {
50         Gdx.gl.glClearColor(0, 0, 0, 1);
51         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
52         stage();
53     }
54 
55     private void stage() {
56         if (isShowIndex) {
57             S_index.act();
58             S_index.draw();
59         } else {
60             if (isShowGame) {
61                 S_game.act();
62                 S_game.draw();
63             }
64         }
65     }
66 
67     @Override
68     public void changeType(Stage stage) {
69         if (stage instanceof IndexStage) {
70             isShowIndex = false;
71             isShowGame = true;
72             S_game = null;
73             S_game = new GameStage(this);
74             Gdx.input.setInputProcessor(S_game);
75         } else if (stage instanceof GameStage) {
76             isShowIndex = true;
77             isShowGame = false;
78             Gdx.input.setInputProcessor(S_index);
79         }
80     }
81 }

 

  主要类就这2个,代码就放在了http://www.cnblogs.com/gorden178/p/4340006.html

 

6、Actor,Stage的学习

标签:

原文地址:http://www.cnblogs.com/gorden178/p/4346312.html

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