简体   繁体   English

libgdx中的线程和移动精灵的一般问题

[英]threading in libgdx and general questions to move sprites

I am reassembling my game i wrote for android. 我正在重新组装我为Android写的游戏。 It was written with simple canvas to learn the basics. 它是用简单的画布编写的,用于学习基础知识。 To get it to a higher level i am going to write it with the help of libgdx . 为了达到更高的水平,我将在libgdx的帮助下编写它。 To do so i thought about a stage where i add my Actors (Monsters, skills if they are active, character and maybe particle) to. 为了做到这一点,我想到了一个stage ,我添加我的Actors (怪物,技能,如果他们是活跃的,角色和可能是粒子)。 I read that this is a good way to assamble a 2D Game. 我读到这是一个讨论2D游戏的好方法。

Some more informations about it. 关于它的一些更多信息。 It's a 2D RPG based on an square system. 这是一个基于方形系统的2D RPG。 Every step is one move to the next square.(maybe tile based lateron. At the moment the background is a single picture) 每一步都是移动到下一个广场。(也许是后面的瓷砖。目前背景是单张图片)

In the old version i used threads for every Monster so they can handle and react as they "wish". 在旧版本中,我为每个Monster使用了线程,因此他们可以按照“希望”处理和做出反应。 Is this also possible in libgdx or do i need to handle it in a different way? 这在libgdx中是否也可以,或者我是否需要以不同的方式处理它?

If i want to move the Character from one to the next square i had a calculated time where i moved the sprite from one square to the next by changing it's pixelposition. 如果我想将角色从一个移动到下一个方格,我有一个计算的时间,我通过改变它的像素位置将精灵从一个方格移动到下一个方格。 In libgdx i am going to have a camera with the squares as init. 在libgdx中,我将使用方块作为init的相机。 So (40f,30f) for example. 所以(40f,30f)例如。 Does it work to move the character within an thread as i did in the old game? 像我在旧游戏中那样在thread移动角色是否有效? So i knew that it does take 500ms from one square to the next so every 50ms the ppuX need to be +0.1f. 所以我知道从一个广场到下一个广场需要500毫秒,所以每50毫秒ppuX需要+ 0.1f。 I also updated the sprite depending on that. 我也根据它更新了精灵。 So if it moves faster the sprite gets faster changed. 因此,如果它移动得更快,精灵变得更快。

I also programmed the skills in that way. 我也用这种方式编写了技能。 For example a fireball is a picture that gets moved by a thread till it hit something. 例如,火球是一个被线程移动直到它碰到某个东西的图片。

See the picture of the "prototype". 看到“原型”的图片。 用帆布原型的图片

It's probably possible, but assigning a thread per entity is a very bad idea - so bad, in fact, that it has a corresponding TheDailyWTF entry: 这可能是可能的,但是为每个实体分配一个线程是一个非常糟糕的主意 - 事实上它很糟糕,它有一个相应的TheDailyWTF条目:

http://thedailywtf.com/Articles/Sprite_Threading.aspx http://thedailywtf.com/Articles/Sprite_Threading.aspx

Not only are you consuming resources unnecessarily in this approach, you're also endangering your sanity in the long run. 在这种方法中,您不仅不必要地消耗资源,而且从长远来看,您也会危及您的理智。 I believe the linked entry, along with the comments, exhaustively elaborates on why that is the case. 我相信链接条目以及评论,详尽阐述了为什么会这样。

Generally, libgdx supports the "main-loop" model, via the ApplicationListener class (specifically the render() method). 通常,libgdx通过ApplicationListener类(特别是render()方法)支持“主循环”模型。 There is a number of tutorials to get you started, including one for Android . 有很多教程可以帮助您入门, 其中包括一个适用于Android 的教程

I know the approach may seem more limiting at first, but it allows for much more reliable code. 我知道这种方法起初可能看起来更有限,但它允许更可靠的代码。

In LibGdx, the Game class's render() method will be called continuously. 在LibGdx中,将继续调用Game类的render()方法。 In this method you can do 4 things. 在这种方法中,你可以做4件事。

1) Process Input 1)过程输入
2) Update sprites/actors 2)更新精灵/演员
3) Check collision 3)检查碰撞
4) render the world 4)渲染世界

You don't need to create a separate thread for moving game objects. 您无需为移动游戏对象创建单独的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM