libGDX游戏开发之NPC敌人事件(六)
libGDX系列
,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm…国内用libgdx比较少,多数情况需要去官网和google找资料,相互学习的可以加我联系方式。
libGDX游戏开发之NPC敌人事件(六)
前面的文章提到在地图中添加敌人,这只时候不参与事件点击等一些操作的场景,不适合RPG游戏,因为RPG游戏会点击怪物或者点击NPC。
通过阅读代码我发现文理Texture
、精灵Sprite
等无法添加鼠标点击等事件。前面提到的舞台Stage
标签可以添加事件。通过阅读源码发现他们是继承Actor
实现的。那么我们的NPC、敌人也能通过继承它实现。
编写一个NPC、敌人Actor
NPC
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Disposable;/*** @author lingkang* @date 2021/10/10 15:01* @description*/
public class NPCActor extends Actor implements Disposable {private Texture npc;private float npcX = 200, npcY = 400; // NPC的位置public NPCActor(SpriteBatch batch) {// 任意找一张图片,我这里选择70*105 最好是2的幂大小npc = new Texture(Gdx.files.internal("npc.png"));// 设置npc演员的大小setWidth(70);setHeight(105);}/*** 重写演员的绘制方法*/@Overridepublic void draw(Batch batch, float parentAlpha) {// 将地图上的位置映射到相机上Vector3 project = ActorEventApplicationAdapter.camera.project(new Vector3(npcX, npcY, 0));batch.draw(npc, project.x, project.y,// 将纹理设置绘制起点和大小0, 0, 70, 105);// 设置npc演员的位置setPosition(project.x, project.y);}@Overridepublic void dispose() {// 防止内存溢出npc.dispose();}
}
编写相机渲染
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.ScalingViewport;/*** @author lingkang* @date 2021/10/10 15:12* @description*/
public class ActorEventApplicationAdapter extends ApplicationAdapter {// 精灵批处理private SpriteBatch batch;// 地图相关private TmxMapLoader maploader;private TiledMap map;// 公开这个相机public static OrthographicCamera camera;// 相机private OrthogonalTiledMapRenderer renderer;private float mapX = 128f, mapY = 128f; // 地图的位置,用于相机观察这个位置private float moveSpeed = 4f; // 相机移动的速度// 舞台public Stage stage;// 玩家private TextureRegion player;@Overridepublic void create() {batch = new SpriteBatch();// 加载地图和地图渲染maploader = new TmxMapLoader();// 地图是 块=20 像素大小:1280x960 随意找个地图,不加也行map = maploader.load("worldmap/worldmap.tmx");renderer = new OrthogonalTiledMapRenderer(map, 1);// 将地图单元设置为 1camera = new OrthographicCamera();// 相机可看到的宽高camera.setToOrtho(false, 800, 600);camera.position.x = mapX;camera.position.y = mapY;camera.update();// 玩家 大小 256*256 随意找一张图片player = new TextureRegion(new Texture("worldmap/badlogic.jpg"), 256, 256);ScalingViewport scalingViewport = new ScalingViewport(Scaling.stretch,Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),new OrthographicCamera()); // 舞台所在的相机与我们的相机不是同一个// 必须保持舞台上的精灵批处理是同一个stage = new Stage(scalingViewport, batch);NPCActor npcActor = new NPCActor(batch);// 添加npc点击事件npcActor.addListener(new InputListener() {@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {// 注意,这里的X,Y是NPC大小内的位置,不是相机的位置System.out.println(String.format("NPC被点击!NPC身体上的位置:%f,%f ",x, y));return false;}});stage.addActor(npcActor);Gdx.input.setInputProcessor(stage); // 必须添加输入事件}@Overridepublic void render() {// 清除相机内容ScreenUtils.clear(225f, 225f, 225f, 0.4f);// 用户的输入userInput();// 更新相机camera.position.x = mapX;camera.position.y = mapY;camera.update();// 一定要记得更新这个相机的视图// 摄影机查看并渲染内容renderer.setView(camera);renderer.render();// 绘制玩家, -128 是因为绘制的原点是中央batch.begin();batch.draw(player, Gdx.graphics.getWidth() / 2 - 128, Gdx.graphics.getHeight() / 2 - 128);batch.end();stage.draw();stage.act();// 演员事件}/*** 轮询,用户输入*/private void userInput() {if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A)) {if (mapX < 64) {mapX = 64; // 不给玩家走出屏幕外} elsemapX -= moveSpeed;}if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D)) {if (mapX > 1280 - 64) {mapX = 1280 - 64;// 不给玩家走出屏幕外} elsemapX += moveSpeed;}if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W)) {if (mapY > 960 - 64) {mapY = 960 - 64;// 不给玩家走出屏幕外} elsemapY += moveSpeed;}if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S)) {if (mapY < 64) {mapY = 64;// 不给玩家走出屏幕外} elsemapY -= moveSpeed;}}
}
编写启动入口
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import top.lingkang.enemy.ActorEventApplicationAdapter;/*** @author lingkang* @date 2021/10/10 15:21* @description*/
public class ActorEventApp {public static void main(String[] arg) {LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();config.width = 1280;config.height = 960;new LwjglApplication(new ActorEventApplicationAdapter(), config);}
}
效果:
控制台输出我们的点击事件:
后记
类似地,我们将上面的NPC改为敌人,鼠标选择后,按技能键一顿输出就是RPG。
打赏
觉得内容不错就赏作者一杯咖啡吧!(恰饭)
后续我会出更多libgdx文章:寻路、寻路算法、打怪,以RPG的视角。
libGDX游戏开发之NPC敌人事件(六)相关推荐
- libGDX游戏开发之Box2D(十四)
libGDX游戏开发之Box2D(十四) libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm-国内用libgdx比较少,多数情况需要去官网和goo ...
- [Unity3D]Unity3D游戏开发之NPC对话系统的简单实现
大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 相信每一个喜欢RPG游戏的人,都会被PRG游戏中的剧情所打动吧.那么作为一部RPG,其剧情主 ...
- [整理]Unity3D游戏开发之Lua
原文1:[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上) 各位朋友,大家好,我是秦元培,欢迎大家关注我的博客,我地博客地址是blog.csdn.net/qinyuanpei.如果 ...
- 游戏开发之U3D插件EasyTouch5.x使用
游戏开发之U3D插件EasyTouch5.x使用 本篇只讲EasyTouch5.x的使用,不讲源码.本来想把EasyTouch5.x的使用手册翻译一遍,想来也是没那个时间. 第一 ...
- JAVA游戏开发之FPS精准控制
目录 JAVA游戏开发之FPS精准控制... 1 1 概述... 2 1.1 编写目的... 3 2 FPS精准控制... 3 2.1 FPS描述. ...
- 开发中的“软”与“硬”:高画质移动游戏开发之道
摘要:游戏的效果不仅与游戏引擎的渲染相关,与硬件优化也有千丝万缕的联系.一款基于芯片优化的移动游戏界面,甚至可以堪比视频游戏的视觉效果.高通半导体事业部资深经理刘晓光从软硬件两个层面分享了移动游戏开发 ...
- DirectX游戏开发之3D角色动起(下)
DirectX游戏开发之3D角色动起(下) 直接先上图吧! 动作idle 动作attack 动作walk 动作run 看,多动作的模型搞下来了.原则上只要在此基础上略做修改就可以实现3d游戏的基本制作 ...
- 游戏开发之U3D实现技能图标冷却的效果
游戏开发之U3D实现技能图标冷却的效果 --本次使用的NGUI插件. 制作思路:NGUI插件里面有个UISprite组件,在它上面有个属性叫Type,改变相应的Type类型有不同的作用,本次使 ...
- DirectX游戏开发之2D文字的实现
DirectX游戏开发之2D文字的实现 文字是承载信息最有效的载体之一,各种游戏程序都离不开文字的显示.对于3D文字来说,2D文字有其渲染的高效性和实现的简洁性. 2D文字的显示无论在写2D游戏还是在 ...
- 微信小游戏开发之CocosCreator多分辨率场景适配方案
主题 Cocos Creator不同手机分辨率的背景图像和场景内容适配 特别说明 CocosCreator微信小游戏开发系列文章,是我在逐步开发过程中,基于官方文档之上,记录一些重点内容,以及对官方文 ...
最新文章
- CImg库中部分函数的作用和用法
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...
- 博客园计划增加的功能
- 【工作秘籍】Facebook内部高效工作大揭秘
- CodeForces - 287C Lucky Permutation(构造)
- 贾跃亭债务小组:美法院支持贾跃亭个人破产重组继续进行
- Android Studio和Kotlin入门
- WinHTTP Web Proxy Auto-Discovery Service
- linux多播 多个接收方,在同一端口上接收多个多播源 – C,Linux
- 超级有用的git reset --hard和git revert命令
- 【神器】yololib 飘云修改版 -- 给iOS APP 添加导入表注入--你懂的
- android 串口 汉字乱码,Arduino软串口出现乱码
- 软件工程 学习笔记 知识梳理
- 打包jar包时文件读取和第三方jar包的问题
- amazing,盗走女友微信后,竟在微信号里看见了……
- Dell PowerEdge全系服务器RAID卡驱动程序 下载地址
- c语言while break用法举例,c语言中continue和break的用法
- html设置字体为5号,以下HTML代码中,哪一个是将词语“Run Away Bride”显示为Verdana字体并且字号为5号的正确代码?...
- 企业容器云管理平台选型指南
- L75 Middle 颜色分类 三路快排