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

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

转载于:https://www.cnblogs.com/gorden178/p/4346312.html

6、Actor,Stage的学习相关推荐

  1. 【MATLAB强化学习工具箱】学习笔记--actor网络和critic网络的结果放在哪里?

    原算例见 [MATLAB强化学习工具箱]学习笔记--在Simulink环境中训练智能体Create Simulink Environment and Train Agent_bear_miao的博客- ...

  2. 【英语学习】【WOTD】veritable 释义/词源/示例

    文章目录 Podcast veritable *adj.* [VAIR-uh-tuh-bul] Definition Did You Know? Examples Podcast veritable ...

  3. Few-Shot Classification of Aerial Scene Images via Meta-Learning(基于元学习的航拍场景图像小样本分类)

    Abstract: 基于卷积神经网络(CNN)的方法近年来在航空场景分类领域占据主导地位.虽然取得了显著的成功,但基于cnn的方法存在过多的参数,并依赖于大量的训练数据.在本工作中,我们将小样本学习引 ...

  4. 分布式强化学习之D4PG

    D4PG全称Distributed Distributional Deterministic Policy Gradient,是总所周知的DDPG的分布式版本.因此学习D4PG之前,需要了解DDPG. ...

  5. 深度强化学习:入门(Deep Reinforcement Learning: Scratching the surface)

    原文链接:https://blog.csdn.net/qq_32690999/article/details/78594220 本博客是对学习李宏毅教授在youtube上传的课程视频<Deep ...

  6. critic法计算_对于强化学习算法中的AC算法(Actor-Critic算法) 的一些理解

    AC算法(Actor-Critic算法)最早是由<Neuronlike Adaptive Elements That Can Solve Difficult Learning Control P ...

  7. 《EasyRL》强化学习笔记

    文章目录 概览 基本概念 系统构成 Agent与环境 策略 奖赏函数 值函数 环境模型 强化学习分类 强化学习与监督学习|非监督学习的区别 例子:playing pong 新技术:深度强化学习 马尔科 ...

  8. 多Agent 深度强化学习综述

    多Agent 深度强化学习综述 人工智能技术与咨询 来源:<自动化学报>,作者梁星星等 摘 要 近年来,深度强化学习(Deep reinforcement learning,DRL) 在诸 ...

  9. CVPR 2021 《Domain-robust VQA with diverse datasets and methods but no target labels》论文笔记

    目录 简介 动机 方法 实验 简介 论文链接 动机 由于cv中的方法通常会过拟合数据集,所以很多学者尝试让这些方法对"域变化"鲁棒.但是,这些domain adaptation方法 ...

最新文章

  1. 一文读懂支持向量机SVM(附实现代码、公式)
  2. 智能车竞赛技术报告 | 智能车视觉 - 新余学院 - 开放艺术队
  3. python中dpi_python matplotlib 绘图 和 dpi对应关系详解
  4. python可以做什么游戏-用python做游戏的细节详解
  5. Window下PHP 安装redis扩展
  6. 程序员们,节日快乐!
  7. 计算机网络实验报告3-tcp,计算机网络实验报告3-TCP.doc
  8. 整个行业都缺Web前端工程师,你还在问Web前端工作好找吗?
  9. return 0可以不加吗_拼多多开店可以不交保证金? 拼多多0元开店是真的吗?
  10. CENTOS6.5 TFS部署
  11. jquery跨域调用webService
  12. 『001』如何在自己的网页里引入一个聊天机器人(。・∀・)ノ
  13. Qt6.2.1在线安装教程
  14. Java excel添加水印
  15. InputStream的available()方法(读文件)
  16. 大主宰PHP文章,大主宰:沈苍生让李玄通放弃洛璃?牧尘受到眷顾!北溟再出手相助...
  17. 基于无监督深度学习的单目深度和自身运动轨迹估计的深度神经模型
  18. STM32F407 ETR 计数程序
  19. rtsp摘要认证协议流程
  20. 安装程序时提示installshield wizard错误,错误代码-6003

热门文章

  1. 7-35 情人节 (15 分)
  2. c++ double 只输出五位_C/C++编程笔记:C语言入门知识点(一),请收藏C语言最全笔记...
  3. mysql是开放源代码_但这可能是很因难的,因为MySQL是开放源代码的,所以任何人...
  4. java纪元时间_Java实现纪元秒和本地日期时间互换的方法【经典实例】
  5. Java EE中的重新验证(java.util.regex.Pattern)
  6. Gym 101147J Whistle's New Car(dfs)
  7. JavaScript的函数声明与函数表达式的区别
  8. HeadFirst之设计模式 觀察者模式
  9. 诗与远方:无题(七十八)- 望天而作
  10. JavaScript语言基础(二)