libgdx

LibGDX重建Flappy Bird——(1) 项目创建与导入

走远了吗. 提交于 2019-11-29 09:28:26
为了节省时间和篇幅,有关LibGDX的基础内容在本文将不会介绍。如果有需要可以查看 土豆教程 。 我们在本项目中使用的是旧版libgdx项目生成工具gdx-setup-ui.jar,虽然该工具是旧版,但是对于简单的项目来说该工具还是非常方便的。下面我们将通过该工具创建项目。 首先双击打开该工具,如图所示: 点击创建按钮,并输入相应的项目名称: 其中Name表示项目名称,package表示包名,game class表示游戏主类名称。这里为了方便我们只选中了两个平台的项目:android和desktop。我们发现上述窗口的第二窗格的Required下出现了一行红色的LibGDX字样,这表明我们还没有选中要使用的LibGDX源压缩文件。下面步骤我们将解决该错误。 点击 按钮,弹出文件选择对话框,如下所示: 选中libgdx-1.2.0.zip文件并点击打开按钮。 然后我们发现LibGDX字样变为绿色了,这表明现在已经准备完毕我们可以生成项目了: 点击 按钮弹出下面窗口: 点击Launch!按钮,稍等一会,发现窗口显示All done!字样表明项目创建成功。 关闭LibGDX Project Setup工具,开始导入项目。 接下来,我们开始导入项目,对于旧版工具生成的项目导入方法非常 简单。首先打开Eclipse,选择File->improt弹出下面导入对话框: 选择General-

LibGDX重建Flappy Bird——(2) 创建游戏框架

故事扮演 提交于 2019-11-29 09:28:17
我们都知道Libgdx只是一个2D游戏框架,并不是游戏引擎,著名的 AndEngine 2D游戏引擎就是基于Libgdx开发的,并且Flappy Bird原版就是使用AndEngine引擎开发的。所以我们必须先创建自己游戏引擎。 创建UML类图 首先我们需要分析Flappy Bird的项目架构。我们使用类图帮助我们可视化项目结构并理解项目构成。下面是该类图: 上图中FlappyBirdMain是项目主类,并且必须继承于LibGDX的ApplicationListener接口。主类包含了一个Assets类引用,Assets类被用于访问和组织游戏资源。主类还包含了WorldController和WorldRenderer两个类的句柄。 WorldController类包含了应用的初始化和所有游戏逻辑。它管理所有游戏对象并控制他们的行为。 WorldRenderer类被用于渲染每个游戏对象和需要的GUI信息。 倒数第二行的三个类继承于AbstractGameObject抽 象类。他们共用一组接口,公共接口对所有游戏对象具有相同的操 作功能,并能将对象渲染到场景。 下面是Flappy Bird的三个游戏对象: Bird:玩家控制的角色 Pipe:我们看到的绿色管道。 Land: 水平移动的地面。 实现Constants类 package com.art.zok.flappybird

LibGDX重建Flappy Bird——(3) 打包资源

空扰寡人 提交于 2019-11-29 09:27:50
修改Android启动图标和名称 首先,我们需要替换Android项目的默认启动图标。FlappyBird-android项目中存在一个名为res的特殊目录。在该目录中包含了Android项目专用的资源文件。 展开res目录可以看到四个以drawable为前缀的文件夹: drawable-ldpi(低分辨率显示屏) drawable-mdpi(中分辨率显示屏) drawable-hdpi(高分辨率显示屏) drawable-xdpi(极高分辨率显示屏) 这些文件被用于Android应用支持不同尺寸显示屏的设备并且Android根据显示屏的屏幕密度决定使用内容。为了简单起见,我们忽略设备分辨率的区别,这里我们创建一个公用的drawable文件夹。这样,无论在什么分辨率的设备上Android都会使用该文件夹内的资源。 下面截图是本例Android项目的默认启动图标ic_launcher.png,也就是我们当前使用的Android应用启动图标: 现在删除上述四个文件和所有名为 ic_launcher.png 图标文件。 下面图标是我们将使用的启动图标: 将上述图标文件命名为ic_launcher.png并拷贝到drawable文件夹内。因为我们并没有改变启动图标的命名,因此项目将无需再更改什么便能正常启动。相反,如果你重命名了启动图标的名称,则不要忘记修改AndroidManifest

LibGDX重建Flappy Bird——(6) 碰撞检测及细节处理

不打扰是莪最后的温柔 提交于 2019-11-29 09:27:31
本章源码链接: Libgdx重建FlappyBird 密码:twy2 上一章完整的介绍了BOX2D的物理仿真创建过程,在本章我们将继续完成BOX2D的剩余内容——碰撞检测。因为BOX2D帮我们完成了所有物理模拟过程,包括碰撞检测,这极大的降低我们的项目难度,我们不需要理解碰撞检测如何运行,甚至不需要知道任何碰撞检测算法,就能够完成碰撞检测并通知我们。下面就让我们为FlappyBird添加碰撞检测的回调函数和相应的逻辑代码。 碰撞检测 打开WorldController类,并为其添加下面代码: ... public class WorldController extends InputAdapter implements Disposable { ... private void initWorld() { if(world != null) world.dispose(); world = new World(new Vector2(0, -110.8f), false); // 为world添加碰撞检测监听器 world.setContactListener(new BirdContactListener()); } ... private void collisionDetection(AbstractGameObject a, AbstractGameObject b) {

How can I do something every second? [LibGDX]

做~自己de王妃 提交于 2019-11-29 09:25:19
问题 Lets say I want to make a loop or something that prints out, for example, "Mario" every second. How can I do this? Can't seem to find any good tutorials that teach this anywhere =P 回答1: You can use java.util.Timer. new Timer().scheduleAtFixedRate(task, after, interval); task is the method you want to execute, after is the amount of time till the first execution and interval is the time between executions of aforementioned task. 回答2: As @BennX said you can sum up the delta time you have in

Button ClickListener is not working in LibGDX game

↘锁芯ラ 提交于 2019-11-29 09:14:51
I am developing an Android game using LibGDX. There are 4 buttons in a menu screen, but the ClickListener of these buttons is not working. // retrieve the custom skin for our 2D widgets Skin skin = super.getSkin(); // create the table actor and add it to the stage table = new Table( skin ); table.width = stage.width(); table.height = stage.height(); stage.addActor( table ); // retrieve the table's layout TableLayout layout = table.getTableLayout(); // register the button "start game" TextButton startGameButton = new TextButton( "Start game", skin ); startGameButton.addListener( new

Circle-Rectangle collision side detection in libgdx

风格不统一 提交于 2019-11-29 08:48:57
问题 I have spent hours looking for the solution to this: I am developing a little top-down game with libgdx (maybe it matters what engine i am using). Now i have to implement the collision detection between my character (circle) and the wall (rectangle). I want the character to slide along the wall on collision, if sliding is possible. Let me explain: If i am moving 45 degrees right up i can collide with the down, the left or the corner of a wall. If i collide with the left i want to stop x

No Sound in Android Application with Libgdx

一个人想着一个人 提交于 2019-11-29 08:08:06
I am creating an Android App with Libgdx. The sound works pretty well, if I run the program in the desktop-version and on the emulator as well. When I am testing the application on my android phone (Galaxy S3) there is no sound. Everything else works pretty fine on my device except the sound which is completely is missing. Do anyone know what the issue here could be? Ask me for code or logs I’d like to post it for you! Regards The following example shows the way i play the sound: import com.badlogic.gdx.audio.Sound; public class LevelDesigner { public LevelDesigner() { Sound mp3Sound = Gdx

Loading nine-patch image as a Libgdx Scene2d Button background looks awful

你。 提交于 2019-11-29 07:49:23
I'm trying to use a Nine Patch as a background for a Libgdx Scene2d UI button. It is loading, buts it is really ugly. I can see the "meta-data" pixels, and its being stretched as if it were just a regular image (the text on the button is "Continue"): I'm loading the .9.png files directly into a (libgdx) NinePatchDrawable via a (libgdx) NinePatch like this: this.dialogButtonUp = new NinePatchDrawable( new NinePatch(new Texture(Gdx.files.internal("data/button-round.9.png")))); this.dialogButtonDown = new NinePatchDrawable( new NinePatch(new Texture(Gdx.files.internal("data/button-round-down.9

slow model batch rendering in libGDX

两盒软妹~` 提交于 2019-11-29 07:05:37
问题 I've got a question whether there is a special mode for grouping models in libGDX. I created a simple class that implements ApplicationListener which shows my problem. I am using the nightly build of libGDX. I read two different models which use the same texture. The application renders respectively 250 models of every type. This is how the part of rendering code looks like: mModelBatch.begin(camera); for(int y=0; y<50; y++) { for(int x=-5; x<5; x++) { ModelInstance instance; if(x%2 == 0)