3

I am designing my first 2-D game for android, In game iam moving a charater by 20 pixel each time whenever a loop is executing, by using a simple expression x=x+20; But the problem iam facing when i test my game on a phone with reso 320*480 with screen size 3 inch, the character seems to move fast, but while testing it on tablet character move slowly. So i want to know how to move a character, so it will appear to move with same speed on both low end devices and high end devices, please tell me the logic, it will be more helpful if you explain the logic with some code.

4

2 回答 2

2

你的问题是你一直使用 20 作为你的移动速度。您需要生成相对于比例因子的移动速度。我不知道您如何以不同的方式处理纵横比,但例如我会假设您正在使用信箱。

假设您正在使用信箱,您需要设置相对于您想要的分辨率的比例因子,在您的情况下为 320px。因此,您应该执行以下操作:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;

double scaleFactor = height / 320.0; /*// 480.0 if you're in landscape */

然后对于您的运动,您可以执行以下操作:

x += 20 * scaleFactor; /*// 20 is your move speed, the scaleFactor will make sure it scales across device sizes

注意:根据您处理水平和垂直比例的方式,您可能需要同时考虑这两个因素,以保持跨设备的垂直和水平移动速度均匀,但这应该是一个好的开始。

于 2013-08-13T20:04:39.337 回答
1

您可以获取设备的宽度,然后将其除以某个值(例如除以 10 表示 10% 或除以 100 表示 100%),然后根据屏幕大小进行移动。角色仍然会以不同的方式移动,但不会像设定的数量那样不同。要获得屏幕的宽度,您可以这样做:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

然后执行以下操作:

x=x+(width*.01);

添加 1% 的屏幕

于 2013-08-13T19:21:10.253 回答