http://overlap2d.com/making-physics-based-side-scroller-part-4-screen-sizes-and-resolutions/
Today we are going to do a bit boring, but important screen size and resolution management using Overlap2D inner workings on a bit low level. Previously our Test Plarformer was running on fixed size 1280×800 which is a nice big size for a phone or tablet. I thought in this tutorial we will add 2 additional sizes 800×480 and 480×320 (we can’t add a bigger size, because well, we should have thought about it earlier when creating the initial project, and made a bigger images)
Before you go any further, please update your overlap2d-runtime to the latest.
If you are just starting, go to Part 1 first.
Agenda
In this tutorial we are going to:
- Create 2 new Overlap2D resolutions
- Fix our 9patch button pixel problem for 480×320
- Create a PlatformerResourceManager class
- Find closes resolution match from available sizes for current screen size
- Fix Problems with different speeds on different resolutions
- Test with different sizes and enjoy
Creating new resolutions in Overlap2D
This one is easy so let’s get over with this quick. Run your Overlap2D and open the project you were working on. In the right top corner click “Create New” button, which will open up a dialog to create new resolution. New resolution creation will calculate relative sizes of texture and create a new atlas pack with images of new sizes packed in it. Usually you want to have more then one size of textures because smaller devices need smaller textures and have low memory, this means if you load into memory smaller png files for smaller screens, your game will be more optimized.
When dialog is opened you have to put inside new width and new height for your resolution, and change a base side for your resolution. When you have a platformer, you want more of the world from left and right be available for players with wider screens. while not compromising on the top and bottom parts (so players with all screen sizes will see same height, but different width) This means our height is our base, we are going to base all resolutions on height.
Create 2 resolutions this way both based on height, one – 800×4800 and other 480×320. It is up to you what to put in the “name” field. But I usually put the exact sizes with “x” in between for easier readability. It’s kind of an ID for your resolution. When done from the top right drop down you can switch between availible resolutions, and see how your game will look on them. Should look nice and scaled.
Problem with resizing 9patch buttons.
Remember we had 2 screens, one for game and other for UI? switch to our UI screen and then open up the 480×320 resolution. First thing you notice is that our button that was looking good before is kinda messed up now (If you used same images as I, and same numbers, the left side of button will be kind of gradient-ish) The explanation here is following:
As you know 9patches work by specifying the “repeat” part on them with a black pixel line on sides. That is done using draw9patch tool provided with android SDK. Because our button is so small, the repeat area is small too. When resizing Overlap2D tries to do it’s best to recalculate the 9patch areas, but sometimes when the difference is half of pixel, it has to decide to choose a whole pixel or no pixel. so on low resolutions you might get a black 9patch line to be a pixel left then it was needed. This has to be fixed by hand.
Find your overlap2D project files, and head tot he assets/480×380/images folder, this is where you will find our 9patch button image, if you look at it close our black line is slightly longer to the left by one pixel on both bottom and top. Open this image with draw9patch tool (which is in your android sdk tools folder) drag and drop your image there, and fix the line size to be only over the area that needs to be repeated (or just delete one pixel on the left using shift +click on it) When it looks good, save it, and close draw9patch tool. With your image being okay, you need Overla2pD to repack it. To do this, open your project, open 480×320 resolution, and while being on it, click Repack button on the top right side of the screen. It will take some time, and your button should look all good now.
So, that’s a neat trick you need to know if you plan to use overlap2d and 9patch UI items for production projects.
Do not forget to press Ctrl+E to export your new assets to your Java project assets folder.
Creating new resource manager
The Overlap2D resource manager was a good home to us, but from now on we need more flexibility. Run your Eclipse or Intellij, open up the project and create new class called PlatformerResourcemanager. Extend it from our good old ResourceManager, so it will have all of old functionality.
Several things we need to do now, Have a method that will load all the stuff we need, and also figure out what resolution fits our needs. Our screen size is decided either from size of your device screen, size of your computer screen app is full screenned, or size of the window if it is windowed. In our case size of the window is written in DesktopLauncher.java you can always figure out that size by calling libgdx method Gdx.graphics.getWidth() or getHeight(). Our mission is to decide which one of our 3 pre-created resolutions are best fit to the current screen size, for example if screen size is 750×460 then the closest match is 800×480, we need close match so stretching of screen will be minimal and we will have maximum quality for minimum memory.
Find closest resolution match from available sizes
Here is the main concepts you need to know. List of possible resolutions is in the file project.dt create by the editor. You load it by calling loadProjectVO() method of the ResourceManager, it then will return you ProjectInfoVO type of object that has originalResolution in it which is your (orig) and it also has ArrayList of resolutions for other resolutions you created (in our case 2). Each resolution is a ResolutionEntryVO which has name, width, height and base. We are going to compare resolutions only by height, compare them to our current screen height, and find the one that has the minimum delta difference. And remember it somewhere.
Here is the full code for entire class:
Now when our class is ready we need to use it properly.
First of all we need to change our TestPlatformer class, create method to this:
Next step modify our Menu and Game stages to get the RM instead of creating a new one, and use it properly. The usage is slightly different, because game stage uses physics and menu stage does not.
for Game stage, we need to change constructor to start like this before loading the scene:
And the MenuStage is even easier:
Don’t forget to modify constructor parameterss and add some class variables if needed. (If something is off, compare with the source code on the github:
https://github.com/azakhary/platformer_tutorial
This should be enough to get this going, now, if you go to your DesktopLauncher.java and change window size to something like 800×480 and then run the project, it should run using smaller textures, and look awesome. The only problem you notice is that things kinda go faster, sheep runs faster, jumps higher, platforms go further e.g. This is all because we had some numbers before that we used as is, without taking into account our stage size.
Scaling other variables according to resolution
We have several places we hard-coded numbers before, which needs to be fixed. This is Player controller that has fixed numbers for gravity, jump speed and max speed of movement. Then it is MovingPlatform class that has platform margin and speed both hardcoded and also loaded from custom vars. All this variables are number of pixels in one way or another, and when you work with other resolutions, you stage is different size, so number of pixels changes too. Overla2D although knows for how much. It keeps track of current multiplier for each resolution in any composite item. There are two variables, mulX and mulY each is saying how much the resolution is bigger on each axis. All you have to do is multiply your hard-coded values with this numbers. Gravity for example is a value of pixels on Y scale so it has to be multiplier by mulX and move speed of player is a value on X scale so it has to be modified by mulY. usually mulX and mulY are the same number, but when you have different aspect ratios they start to differ. mulX or mulY values are “1” when your loaded resolution is “orig”. so plan your inital numbers according to the orig resolution.
Here are the code snippets:
Run game now, and things should move on the same speeds and margins irrelevant of your screen size.
Other thing to try is to set your screen size to 900×480 for example. This will be a wide screen for 800×480 resolution. Our button will be still on the left. But you can see that more of the world is visible from left/right sides. That is because of this line we have in our PlatformerResourceManager:
And this line in GameStage (but not MenuStage, compare the difference to understand it better)
And that is it for today!
Enjoy running this on different resolutions/window sizes/devices. Next time we will get back to improving game again, but I thought this size/resolution thingie was important and might have been confusing!
As always, compare source codes with mine on github or ask in forums.
相关推荐
虚拟虚拟卷轴 快速滚动任意数量的数据| |赞助商目录安装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 多人网页浏览器游戏......真是一口。 古代神话主题,特别关注季节...
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...
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”包含在构建中。 如果您的构建过程可以...
Vue Virtual Scroller是一款高效的数据列表滚动解决方案,专为Vue.js框架设计。它通过虚拟化技术实现了即使在数据量巨大的情况下,也能保持页面滚动的流畅性。这个组件的关键在于,它只渲染视口内可视的元素,而不是...
npm install --save custom-react-scroller 截图示例 道具 rightIcon :根据需要渲染正确的图标组件。 (可选的) leftIcon :根据需要渲染左侧图标组件。 (可选的) 用法 import React from 'react' import ...
知识点4:使用 vm 实例访问 Vue 实例 在 beforeRouteEnter 钩子中,无法直接使用 this 来访问 Vue 实例。因此,需要使用 vm 实例来访问 Vue 实例。vm 实例是 Vue 实例的一个代理,可以用来访问 Vue 实例的方法和...
插件名称:Chayon-Shaah-Background-Parallax-Scroller 插件作者:Chayon Shaah 插件Verson:1.0 网址: : 说明:这是一个不错的轻量级固定背景视差滚动器jquery插件,由孟加拉国Chayon Shaah Mymensingh开发...
"MODE-2-Vertical-Scroller-master"可能是一个GitHub项目的名称,其中"master"表示主分支。该项目可能包含源代码、示例、文档和其他资源,帮助用户理解和实现垂直滚动条。常见的文件可能有`.py`(Python源代码)、`...
npm i vue-scroller 2 引入 import Vue from 'vue' import VueScroller from 'vue-scroller' Vue.use(VueScroller) 3 使用 1)添加上拉加载方法和下拉刷新方法 2)加载完成调用 相应的方法结束动画,不然的话会一直...
《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)。 它使用本机滚动事件,因此所有事件都可以正常工作(鼠标滚轮,空格,向下翻页,向上翻页,箭头等)。 没有其他第三方依赖。 表现极好!用法...