http://overlap2d.com/making-physics-based-side-scroller-project-setup-part-1/#Main_Goals
This is Part 1 of a tutorial, where we are going to make a side-scroller game together.
Main Goals
The goal is to make a simple game using libGDX, Box2D and Overlap2D Editor for learning purposes. There will be a level with ground, platforms and a playable character who will be walking around. Main character also will be able to shoot bullets which will break surroundings in order to make his way through. We will use Box2D physics simulated world for all the platforms and surroundings, while player itself will not be a physics object. We will write player pseudo-physics ourselves using ray-casting. We will also setup a basic simple UI, using 9patch images.
Goals/Steps for this tutorial
- Get ready main textures
- Setup Overlap2D project
- Import all requested assets
- Setup physics data in Overlap2D
- Setup libGDX project
- Export Overlap2D data into your project assets folder
- Make short Stage class for things to show
- Run the physics simulation and enjoy
What you will need before you start
- IntelliJ IDEA or Eclipse with Gradle plugin
- Overlap2D Level Editor (request download from main page)
- draw9patch tool, can be found in your Android SDK tools folder (http://developer.android.com/tools/help/draw9patch.html) (optional)
Getting ready main textures
Get ready PNG texture files for your project, mainly platforms to be used in your level and some circle to rotate. One of the platforms we will use as a 9patch image, so we can change its width while it still looks good and soesn’t get stretched. Here is the list of images I have.
You can download them from here to follow along if you do not want to use others. (9patch item will be already created there if you can’t get draw9patch tool).
I decided that inside the game we can use platforms of different sizes, so it will be nice to create a 9patch platform. For that, run your draw9patch tool from Android SDK tools directory, and drag and drop platform1.png there, make selections on it like on screenshot below:
Save the file as paltform1.9.png (I know I misspelled it, but what can you do. Too lazy to go back and fix it now, so yeah, just copy/paste the file name, please). It is important to keep the .9 at the end of file name so that the editor will recognize this image as 9patch (note: this step is already done if you are downloading images I provided).
The assets are now ready for import, so it’s time to create Overlap2D project.
Setting up Overlap2D project
Run Overlap2D.jar and go to File → New Project. For this project I decided to use a bit bigger resolution like 1280×800: this is a common 7 inch tablet resolution and is also seen on some phones. The reason I am not doing 800×480 is because I will also show you how to do a multiresolution project with Overlap2D (in next tutorial of this series). When you click “New Project” a new project dialog is opened, so you can put all the required data there. Here is the screenshot of my configuration:
Then click “Create New Project” button. An empty project will open up. You do not have any images imported yet so it’s perfect time to import your images we prepared earlier. Go toFile → Import to Library (and yes, I do think we should rename this to just “Import”) and then near the import images text field click on “…” button to select the files to import. Navigate to the folder you have them in and import them (duh!). Importing images takes some time, because Overlap2D not just copies them but also packs them into Atlas Pack file for faster use.
If all was successful, then you should see a list of images on the right middle panel. That means all is good and you can start dragging them to your scene.
Let’s also prepare our ground platform. Drag and drop our 9patch paltform1.9 to your scene from right panel, and then from “Basic Properties” panel on top right set its width to 800. It should change in size at the middle, while the left and right parts remain as-is. When done, select that platform, right-click on it, and from drop down menu select “Group Into Composite” option. Later you would be able to get inside that composite item and add things like rock, grass or other stuff into it. But for now we will leave it as-is, and proceed to setting up our physics environment.
Configuring Physics Data
Okay it’s time to setup our meshes. There are several ways to do it. If you want to setup a physics data for an object that is already on the screen, just right-click on it and select “Edit Physics” button from drop down menu, this will bring up the “Edit Physics” dialog. There you can choose if your object is static or dynamic, set its mass, friction, density and so on, and draw the polygon mesh for its fixture shape. You can also right-click on an asset inside the right panel, and select “Edit Physics”. If done so, later every time you drop this asset to the stage you will have physics data copied with it. It is also important to notice that physics data is not saved unless you click “Save Physics Data” inside the edit physics dialog. If you just close it, data is lost.
In this tutorial we need to create physics body for the main big platform, as well as for several others. Right-click on composite item created earlier, and create physics for it by tracing its shape, setting it up as a static body, and setting friction to 1.0.
Note: if you try to just edit physics of 9patch item, it won’t work. You can only setup physics for composite items or simple images for now.
For other platforms just edit physics from the assets panel, with same settings. Take a look at the shapes I made:
For the ball png, I could not use a simple circle shape as we don’t support it yet (sorry about that). Please make sure to leave this one as a “dynamic” body, and add a mass to it, like “1”.
Besides that, set the rest of the settings like restitution, friction and density to “1” as well. This will allow this body to fall down and rotate when hitting things, as well as bounce. You can test it by clicking on “Test” button inside “Physics Editor”.
When you are done setting up physics objects, set up the world properties as well. For that click on an empty space on your stage. That will show you the world properties on top right panel. There, set Gravity Y value to -10.0 and make sure that “Enable Physics” checkbox is checked. Save your project.
Drag and drop some of the items to your stage and create an interesting level where your future character will be able to move. Here is what I managed to get:
I put this rotated panel there so a ball will fall on it to showcase dynamic items. Your Overlap2D project setup is ready, let’s get to the coding!
Setting up your libGDX application
First get the libGDX setup app from here: http://libgdx.badlogicgames.com/download.html (Click “Download Setup App”).
After you run it, you should enter some settings, here is the one I used (note: I did not create Android project, because I did not have latest SDK at that moment).
This will create a Gradle project in the specified location. I used IntelliJ as IDE so my examples are going to be about doing it with that IDE, but if you use Eclipse it should not be hard as well. So for IntelliJ, run it and then choose “Import Project”. Then select “Import Gradle Project”, on next screen make sure “Use default Gradle wrapper” is selected and hit “Finish”.
This will create your project, but you still need to run the Gradle script to configure it. In IntelliJ got to View → Tool Windows → Gradle.
This will open up a panel on the right; find a “Refresh” icon there and click it. After some crunching your project will be configured and ready to run.
Then in the same right panel in “Desktop” section find the “Run” button and run your project (am I oversimplifying this? Honestly I am just making a long text to get the the end of this image on the right, so things look pretty. Don’t judge).
Okay, you probably got an empty libGDX app running now, with red background and a crazy face on the bottom left. That means you are doing good.
Make sure to get rid of the unnecessary stuff by removing badlogic.jpg from assets folder, then going to TestPlatformer class and deleting sprite batch and the image. Now if you did all right, run the project and you will see a black screen.
Setting up your project to work with Overlap2D runtime
First of all go to github and locate Overlap2D runtime project: https://github.com/UnderwaterApps/overlap2d-runtime-libgdx
Next download it as ZIP or clone it. Make sure you put the overlap2d-runtime-libgdx-master directory in the same parent directory as your project is.
Now we need to configure Gradle to see Overlap2D runtime. To do that, first open settings.grade in the root of your libGDX project. And add this lines there:
Now open build.gradle file and locate part with project(“:core”) { , change it so it looks like this:
Now, when you presh the “Refresh all gradle projects” button again, your project will have Overlap2DRuntime as a dependency.
Exporting Overlap2D data to your project assets folder
In order to render your Overlap2D scene inside the empty libGDX project you just built, you will need to export all data to your libGDX project assets folder. To do so simply go to your Overlap2D project and choose File → Export Settings. This will open up “Export Settings” dialog box; click on “…” button to set the path to your libGDX assets folder which is inside “core” folder. Then click on “Save Settings and Export” button.
Now when you go to your IntelliJ you will see that assets folder is full with things generated by Overlap2D like the Atlas file with textures, MainScene.dt file with your scene content, and project.dt file with information about resolutions. Now that you have all data ready, only coding is left.
Setup window size
We need to set our window size to the size of or game, which is 1280×800. For that, open DesktopLauncher.java and add config.width, config.height configurations to it:
Creating GameStage
In order to render Overlap2D scene, you need a stage. Create a GameStage class and extend it from Overlap2DStage. Overlap2DStage will take care of most of the things, like setting up a default resource loader that will load things from your assets folder as if they were exported from Overlap2D directly, like we did. It will also setup all the physics world configurations, and do other required things (I encourage you to check its source to see what is done).
In GameStage constructor we need to do just 3 things.
- Initialize scene loader
- load MainScene as an actor
- put that actor on stage to render
Here is how it’s done:
The last thing to do is to render our stage. This is done by creating its instance in our TestPlatformer class create method. And then later in render method calling stage.act() and stage.draw().
That’s it! Go ahead and run the project, if all works well the Overlap2D scene will load, the ball will fall down under the gravity and bounce off things.
Final result screenshot
What’s next
In next tutorial we will add a character to control in this simulated physics environment.
Let me know if you completed the tutorial, show off your screenshots and suggest your improvements in Overlap2D forums!
相关推荐
虚拟虚拟卷轴 快速滚动任意数量的数据| |赞助商目录安装npm install --save vue-virtual-scroller :warning: 现在, vue-virtual-scroller使用在显示时自动刷新自身,以防止显示故障。 这意味着您需要包括vue-...
1. **基本游戏架构**:2D-Side-Scroller的核心是游戏循环,它负责处理输入、更新游戏状态、渲染画面并控制帧率。Java中的`while`或`for`循环常用于实现这一机制。 2. **图形库的使用**:Java提供了多种图形库,如...
多人无限 2-D-Side-Scroller : Horae 的重装步兵密苏里州立大学 2015 年Spring软件工程小组项目Horae 的重装步兵是什么? 2D Infinite Sidescroller 多人网页浏览器游戏......真是一口。 古代神话主题,特别关注季节...
React整页滚动条 用于全页滚动的React模块 安装 npm install --save react-full-page-scroller 演示版 用法 import React , { Component } from 'react' ... < h1> 1 < / h1 > < / div > < h1
ngx-virtual-scroller 虚拟滚动显示虚拟的“无限”列表。 支持水平/垂直,可变高度和多列。 从angular2-virtual-scroll重命名为ngx-virtual-scroller 。 请更新您的package.json 关于 此模块显示一小部分记录,恰好...
安装npm install react-infinite-scroller --saveyarn add react-infinite-scroller如何使用import InfiniteScroll from 'react-infinite-scroller' ;窗口滚动事件< InfiniteScroll pageStart = { 0 } loadMore =...
vue-scroller移动端下拉加载组件 vue-scroller移动端下拉加载组件 vue-scroller移动端下拉加载组件
本项目是一款基于微信小程序的coolui-scroller下拉刷新和上拉加载功能组件源码,包含581个文件,涵盖112个JavaScript文件、74个JSON配置文件、58个PNG图片、57个wxss样式表、57个wxml模板文件、50个JPG图片、39个GIF...
elp-cascader Cascader based on 'element-ui' and 'vue-virtual-scroller'@vueblocks/elp-cascader基于element-ui和vue-virtual-scroller的级联选择器,用虚拟列表的方式逐级渲染列表。适用于数据量较大的场景。...
安装$ npm install priority - nav - scroller -- save - dev用法导入JS 该脚本是ES6(ES2015)模块,但编译后的版本作为“ src / scripts / priority-nav-scroller-umd.js”包含在构建中。 如果您的构建过程可以...
npm install --save custom-react-scroller 截图示例 道具 rightIcon :根据需要渲染正确的图标组件。 (可选的) leftIcon :根据需要渲染左侧图标组件。 (可选的) 用法 import React from 'react' import ...
Vue Virtual Scroller是一款高效的数据列表滚动解决方案,专为Vue.js框架设计。它通过虚拟化技术实现了即使在数据量巨大的情况下,也能保持页面滚动的流畅性。这个组件的关键在于,它只渲染视口内可视的元素,而不是...
知识点1:vue-scroller 的滚动位置记录 vue-scroller 是一个基于 Vue.js 的滚动组件,提供了滚动位置记录的功能。通过使用 vue-scroller,可以记录用户的滚动位置,并在返回列表页时自动滚动到之前的位置。 知识点...
插件名称:Chayon-Shaah-Background-Parallax-Scroller 插件作者:Chayon Shaah 插件Verson:1.0 网址: : 说明:这是一个不错的轻量级固定背景视差滚动器jquery插件,由孟加拉国Chayon Shaah Mymensingh开发...
1 下载 npm i vue-scroller 2 引入 import Vue from 'vue' import VueScroller from 'vue-scroller' Vue.use(VueScroller) 3 使用 1)添加上拉加载方法和下拉刷新方法 2)加载完成调用 相应的方法结束动画,不然的...
"MODE-2-Vertical-Scroller-master"可能是一个GitHub项目的名称,其中"master"表示主分支。该项目可能包含源代码、示例、文档和其他资源,帮助用户理解和实现垂直滚动条。常见的文件可能有`.py`(Python源代码)、`...
《Side-Scroller: 探索JavaScript实现的横向卷轴游戏技术》 “Side-Scroller”是一个典型的横向卷轴游戏项目,这类游戏以其独特的视觉风格和操作方式深受玩家喜爱,如经典的《超级马里奥兄弟》。在这个项目中,我们...
通过下载`view-pager-with-custom-scroller-master`这个压缩包,你可以看到实际的代码实现。在这个项目中,开发者可能已经创建了一个自定义的`Scroller`类和`PageTransformer`,并在`MainActivity`中设置了它们。...
import ReactPageScroller from 'react-page-scroller'; 示例在demo / src中。 特性 财产 类型 描述 默认值 animationTimer 数字 动画持续时间(以毫秒为单位) 1000 animationTimerBuffer 数字 动画缓冲时间 ...
安装yarn add react-custom-scroller好处极其轻巧(压缩后不到2KB)。 它使用本机滚动事件,因此所有事件都可以正常工作(鼠标滚轮,空格,向下翻页,向上翻页,箭头等)。 没有其他第三方依赖。 表现极好!用法...