6、Actor,Stage的学习

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

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

时间: 2024-08-26 01:25:04

6、Actor,Stage的学习的相关文章

Actor模式初步入门

     从实习到现在,一直在做Unity相关的业务,不知不觉中感觉已经不在关注服务器相关的技术了.一次偶然的机会再腾讯的gad平台上观看了云风在15年在腾讯做的skynet讲座(http://gad.qq.com/content/coursedetail/467),skynet是用c写的核心,lua做上层业务,基于actor模型的服务器框架,哈哈,这次学习actor模式的学习也是因此而起. Actor模型概念      Actor模型为并行而生,简单说是未解决高并发的一种编程思路.在Actor

Scala笔记整理(九):Actor和AKKA

[TOC] 概述 ? Scala的Actor有点类似于Java中的多线程编程.但是不同的是,Scala的Actor提供的模型与多线程有所不同.Scala的Actor尽可能地避免锁和共享状态,从而避免多线程并发时出现资源争用的情况,进而提升多线程编程的性能. Spark中使用的分布式多线程框架,是Akka,是Scala的一种多线程的类库.Akka也实现了类似Scala Actor的模型,其核心概念同样也是Actor.Scala Actor模型已经在2.1.0的时候还在用,但是在2.1.1的时候已经

HDU-SupportOrNot训练实录

菜鸡队训练实录. 现场赛记录: 2016:[名称:奖项/排名] ZJPSC:Gold/1 CCPC中南邀请赛:Gold/1 ICPC Dalian:Gold/24 ICPC Beijing:??? CCPC Final:??? ICPC China-Final:??? To do List: 所有人需要提高效率 减小罚时 三人组队训练时必须用指定Ubuntu电脑敲题,其他两台电脑只能读题.读代码 为提升代码能力,poursoul和_ilovelife尽量做到每天solo一套简单GYM,也可以视情

libgdx自制简易Flappy Bird

Flappy Bird,好吧,无需多说.今天年初不知咋的,一下子就火了,而且直接跃居榜首,在ios和android平台都是如此,实在难以理解.传说其作者每天收入能达到5w刀,着实碉堡了... 好吧,咱没创意,不过山寨一个还是可以的,话说!!! 好了,不罗嗦了,直接代码了. 我使用libgdx框架(你要说是引擎也行)实现的,版本为0.9.9.就设计了一个开始画面和一个游戏画面. 游戏入口和主类: package com.fxb.flappy; import com.badlogic.gdx.Gam

android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动

http://www.cnblogs.com/htynkn/archive/2012/01/13/libgdx_13.html 本文紧跟上文,地址:android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用 地图我们创建好了接下来就是主角的出现.其实上文介绍了如何TiledMap和Stage的结合,角色的处理就简单了. 可以继承Actor类创建主角类,我就偷个懒,用Image代替. 编辑我们的TMX文件,添加一个对象层. 在主角要出现的地方加个形状 取名为play1 我们的

5、Stage,Image,ImageButton,Label的初步学习

昨天到现在就学习这几个,感觉快没有动力了,加油,一定要坚持 废话不多说了 1.Stage,舞台类 Stage(); [构造] Stage(Viewport viewport); [构造] Stage(Viewport viewport, Batch batch); [构造] 这里我只用了第一个和第二个 new Stage();就是不适用任何缩放模式吧,偶是这么理解的 new Stage(Viewport  viewport); 这需要创建以下这2个对象的实例,把这个2个实例的其中一个作为参数传递

Spark学习笔记1:Application,Driver,Job,Task,Stage理解

看了spark的原始论文和相关资料,对spark中的一些经常用到的术语学习了一下,记录下. 1,Application application(应用)其实就是用spark-submit提交到spark的程序.比方说spark examples中的计算pi的SparkPi.一个application通常包含三部分:从数据源(比方说HDFS)取数据形成RDD,通过RDD的transformation和action进行计算,将结果输出到console或者外部存储(比方说collect收集输出到cons

2014.8.12-AKKA和Actor model 分布式开发环境学习小结

学习使用AKKA 断断续续有一年了.目前还是习惯用java来写akka下面的程序.对于原生的scala还是没有时间和兴趣去学习它. 毕竟学习一门语言需要兴趣和时间的. AKKA学习资源还是不算丰富. 看过最多的就是官方的编程手册,还有就是AKKA Essentials 这两本.  自己动手写的程序还不算多,也放在github上面. 另外,在akka编译配置.升级版本上,以及部署多台服务器组建akka cluster 方面花费了不少时间.因为项目需要,上周重新在办公室用两台mac台式机和一台thi

libgdx学习记录5——演员Actor

Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用.Actor能够设置大小,位置,旋转和动画等. 我们自定义的Actor一般需要继承于Actor,并且重写其中的act和draw方法. 自定义的actor是一个图片. 1 class MyActor extends Actor{ 2 TextureRegion region; 3 4 public MyActor(){ 5 Texture texture = new Texture( Gdx.files.internal(