http://overlap2d.com/making-physics-based-side-scroller-part-3-adding-ui-and-moving-platforms/
Good coding day, awesome ones. Today we are going to do the 3d part of the tutorial on side-scroller. It is going to be a short one, but still important.
Agenda
In this tutorial we are going to:
- Add new scene (stage) to host game UI
- put a restart level button on it and make it work
- handle inputs on both stages.
- Add moving platforms and camera
Adding UI scene
First, just a quick disclaimer why your UI needs to be in other scene.
- Because we will move camera around in game, and we do not want our UI to move in opposite direction, or artificially move it with it.
- Because we do not want UI to be affected with lighting system if we use any.
Now that this is clear, let’s go to File > Scene > Create new scene (in overlap2d) and create new scene. I called it UIScene.
This will create an empty scene, with the same resolution as your previous one was. Our current UI is going to be pretty simple, just a button in left top corner that will restart the game.
Creating restart button
For that we will need a 9patch as a background and a ttf font for text. If you were following the tutorial from the beginning, you remember that I shared some images that you imported. There was a 9patch texture called Goldprogress.9.png (I do not even remember why it was named that way…. welp what can you do). It was supposed to be a button background, so go ahead and drag it to the screen, click on it to select it, and in the basic properties area, set it’s width to 348 pixels.
Next we need to use a text label, so we need to import a font first. I went to dafont.com and got VCR OSD Mono (which was 100% free to use). And you can obviously get any. Import it using File > Import to Library > Import fonts. When done, click on the UI tab in right middle panel, and drag and drop a Label to the scene. Set the size to 72, Align center, and put it in the middle of background image (9patch). Select label and the background together, right click and choose Convert to Button. This will create a “half made” composite item. If you double click on it and get inside you will see it has 3 layers: Default, normal and pressed. Both images are currently on default layer. The pressed and normal layers are to host pressed and normal states of button.
Our text has nothing to do with button pressed state, so first create another layer and name it “text” put it on top of all layers. Then select your label, press Ctrl+X to cut it out, select text layer you created earlier, and Ctrl+V to paste label to that layer in same place.
Now for the states. Cut and paste your background image to the normal layer, then copy it, and paste to the pressed layer, while selected click on the color picker in the basic properties, and select a dark gray tint to kinda darken it a bit. (or you can use another image for that state if you so desire). At the end you should have a composite with empty default layer (you can’t delete it) with normal and pressed backgrounds on their layers, and a text layer with label on it. You can exit your newly created button composite by double clicking on empty space. position your button in top left location of the screen, and give it an ID: restartBtn.
Save and export your project, we are done here. If al is good there should be a UIScene.dt file be added in your assets folder of your libGDX project.
Creating new stage for UI scene
In TestPlatformer java we previously had variable “stage” let’s change it to gameStage for better naming, as we are going to have another stage with name menuStage (add it as well). The important trick here is to remember that gameStage when created called for initSceneLoader method of Overlap2DStage, which create basically a default ResourceManager for you, you do not want to do it again and load all resources for each stage separately for menu stage, because it was already done in game stage. In order to re-use resource manager, we are going to pass it as a parameter to MenuStage constructor. In fact we are going to even pass the entire GameStage to MenuStage, as menu stage would need to call some of it’s methods that we are going to write soon. Bare with me, it’s going to be explained better ahead. For now, let’s just create a new class called MenuStage and extend it from a simple libGDX stage ( we do not need any physics or lights here)
Let me go through this lines of code in more details. First we create a new instance of SceneLoader to load our UI scene. If used with empty constructor, it would have generated a new resource manager instance for us, but because we already have one in game stage that is already have all the resources loaded in memory, we are going to simply reuse it. It is in essentials.rm
Then we just load the UIScene and add it as actor, as usual.
For the button, first we create the button instance (the iScript) that is self initializing from the restartBtn composite item. (Basically it will create an iScript and attach it to the button). This line will make our button clickable, it will respond to click by changing to pressed state, and we will also have a method to add listeners to it. So we can now go ahead and add a click listener. From there we just call a restart method of game stage (that we haven’t made yet) to restart the game.
Now, next step us to change our TestPlatformer.java so it will have instances of both stages, will draw them both in sequential order, and most importantly will deal well with inputs for both. you see by default the game stage was listening for inputs, now we need to do it with both scenes. For that you can use a nice class that libGDX has, it is called InputMultiplexer. Here is the new code of our TestPlatformer.java
And finally let’s add the restart method to our GameStage so that the game will actually restart when the button is pressed. This time we’ll keep it simple and just reset the position of the player to initial one. Add this method to GameStage
Then in player controller, add private Vector2 initialCoordinates, and set it to player initial coordinates in init method like so:
Finally create the reset method:
Run the game, and it should work nicely, walk around and press restart, the player should move where it started, and your button should have 2 states when pressed and not, and it should also move with camera no matter where the player is.
Moving Platforms
Now that we are done with UI, let’s also have some moving platforms. for that place some platforms on the screen on overlap2d, then add several custom variables to them using basic properties panel. I named them platfromMargin and platfromSpeed. First one is to specify how far platform will go up or down from initial position. and the other to specify speed in pixels per second. I used values like 100 pixel margin and 50 pixel per second for speed. so it will take 8 seconds for platform to complete entire cycle.
When this is done and exported, create a class script to handle the logic for each platform:
The code is pretty straight forward, we take the configuration values from custom variables, and then move the platform in act method. We change position by changing physics body position first, and then setting actor position to body position while converting between physics and stage coordinates using PhysicsBodyLoader.SCALE value. The actor does not automatically follow physics body, because it is a static body not a dynamic body.
Now in order to attach this script to each platform, add this code to the end of GameStage constructor:
Here we just iterate through the items, of root composite, see if any of them has the platformSpeed custom variable and is a composite item, and if we find any, add a MovingPlatform scrip to that item. That’s it. Now when you run the game some platforms will move, and you can even jump on them.
Now let’s also do some improvements to player controller.
Moving on angled slopes
I am going to post full code on github, but let’s quickly go through some improvements you can do to your player controller. First, we want to move on angled platforms, and right now, if you try, player will fall directly through them. This is happening because we are ray casting from the bottom of the player, and when player moves say right, the ray size is zero as there is no vertical speed, and because ray is now under the platform, no collision is detected. To fix this, we need to cast the ray from the middle of the player.
Here is the new check for collisions method:
I have also modified it so we can double jump in the air, by adding jumpingCount variable and doing this when jumping:
The jump counter is re-set when we hit the ground in collider.
Moving Camera with player
Finally, let’s move the camera with the player by adding act method to GameStage:
That’s it for this tutorial, I think we’ve done a lot! And if something does not work you can:
相关推荐
虚拟虚拟卷轴 快速滚动任意数量的数据| |赞助商目录安装npm install --save vue-virtual-scroller :warning: 现在, vue-virtual-scroller使用在显示时自动刷新自身,以防止显示故障。 这意味着您需要包括vue-...
《2D-Side-Scroller:重温2D横向卷轴太空射击游戏的编程之旅》 在游戏开发领域,2D-Side-Scroller是一种经典的类型,它带我们回到了早期电子游戏的黄金时代。由Michael van Dyk创作的“2D-Side-Scroller”是一款以...
多人无限 2-D-Side-Scroller : Horae 的重装步兵密苏里州立大学 2015 年Spring软件工程小组项目Horae 的重装步兵是什么? 2D Infinite Sidescroller 多人网页浏览器游戏......真是一口。 古代神话主题,特别关注季节...
elp-cascader Cascader based on 'element-ui' and 'vue-virtual-scroller'@vueblocks/elp-cascader基于element-ui和vue-virtual-scroller的级联选择器,用虚拟列表的方式逐级渲染列表。适用于数据量较大的场景。...
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 =...
npm install --save react-full-page-scroller 演示版 用法 import React , { Component } from 'react' import MyComponent from 'react-full-page-scroller' import 'react-full-page-scroller/dist/index.css' ...
vue-scroller移动端下拉加载组件 vue-scroller移动端下拉加载组件 vue-scroller移动端下拉加载组件
本项目是一款基于微信小程序的coolui-scroller下拉刷新和上拉加载功能组件源码,包含581个文件,涵盖112个JavaScript文件、74个JSON配置文件、58个PNG图片、57个wxss样式表、57个wxml模板文件、50个JPG图片、39个GIF...
安装$ npm install priority - nav - scroller -- save - dev用法导入JS 该脚本是ES6(ES2015)模块,但编译后的版本作为“ src / scripts / priority-nav-scroller-umd.js”包含在构建中。 如果您的构建过程可以...
Vue Virtual Scroller是一款高效的数据列表滚动解决方案,专为Vue.js框架设计。它通过虚拟化技术实现了即使在数据量巨大的情况下,也能保持页面滚动的流畅性。这个组件的关键在于,它只渲染视口内可视的元素,而不是...
npm install --save custom-react-scroller 截图示例 道具 rightIcon :根据需要渲染正确的图标组件。 (可选的) leftIcon :根据需要渲染左侧图标组件。 (可选的) 用法 import React from 'react' import ...
知识点3:使用 sessionStorage 存储滚动位置 在示例代码中,使用了 sessionStorage 来存储滚动位置。sessionStorage 是一个浏览器存储机制,可以用来存储临时数据。在本示例中,使用 sessionStorage 来存储滚动位置...
插件名称:Chayon-Shaah-Background-Parallax-Scroller 插件作者:Chayon Shaah 插件Verson:1.0 网址: : 说明:这是一个不错的轻量级固定背景视差滚动器jquery插件,由孟加拉国Chayon Shaah Mymensingh开发...
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)。 它使用本机滚动事件,因此所有事件都可以正常工作(鼠标滚轮,空格,向下翻页,向上翻页,箭头等)。 没有其他第三方依赖。 表现极好!用法...