英文:
Set offset for BitmapDrawable while repeating in the x and y direction?
问题
我正在制作一个2D平台游戏。我已经为平台创建了一个纹理,旨在重复使用,以填充整个平台,而不会超出边界。我的第一次尝试是手动从位图中绘制所有像素,但这会在移动平台时导致背景闪烁穿过(移动和绘制线程是分开的,因此移动可以以特定速度运行,而FPS则不需要受影响)。我发现这个技术效果更好:
// 初始化
bitmap = new BitmapDrawable(res, Texture.PLATFORM.getBitmap());
bitmap.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
// 绘图循环
int x = getX() + (isStill() ? 0 : (int)MainActivity.offsetX);
int y = getY() + (isStill() ? 0 : (int)MainActivity.offsetY);
bitmap.setBounds(x, y, x + getWidth(), y + getHeight());
bitmap.draw(canvas);
然而,位图似乎保持静态,而平台则充当查看位图的“视窗”。我能想到的唯一解决方法是在某种程度上“偏移”静态位图:
bitmap.offset(x, y);
显然,那不是一个函数。当查看文档时,我找不到符合我要求的函数。
总之,BitmapDrawable导致背景不随平台移动,使其看起来非常奇怪。
提前谢谢!
英文:
I'm making a 2D platformer game. I have created a texture for the platform, that is meant to be repeated over and over to fill the entire platform, without going over. My first attempt was to draw all the pixels from the bitmap manually, but this caused the background to flicker through while moving the platform (the movement and drawing threads are seperate, so the movement can run at a specific speed, while the FPS doesn't need to suffer). I found this technique worked better:
// Init
bitmap = new BitmapDrawable(res, Texture.PLATFORM.getBitmap());
bitmap.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
// Drawing loop
int x = getX() + (isStill() ? 0 : (int)MainActivity.offsetX);
int y = getY() + (isStill() ? 0 : (int)MainActivity.offsetY);
bitmap.setBounds(x, y, x + getWidth(), y + getHeight());
bitmap.draw(canvas);
However, the bitmap appears to be staying static while the platform is acting as a "view hole" to see through to the bitmap. The only work around I can think of is to somehow "offset" the static bitmap:
bitmap.offset(x, y);
Obviously, that isn't a function. I couldn't find one that would do what I want when looking through the docs.
To summon things up, the BitmapDrawable is causing the background to not move with the platform, making it look super weird.
Thanks in advance!
答案1
得分: 0
尝试在你的代码中使用以下提示:(我假设游戏在水平方向上向前移动)
- GUY 只应该在垂直方向上移动(使用适当的触摸输入),而不应该在前后方向上移动,因为你希望焦点(或者交替地是摄像机)仅集中在GUY上。我注意到在你的视频中,当GUY从墙的初始较高位置移动到稍低的位置时,墙也在向上移动,请纠正这一点,因为GUY应该向下移动(尝试实现重力效果)。
- WALL 只应该在前方(主要)和后方(较少)移动。WALL 不应该上下移动。不要对它应用重力效果。你可以至少创建两个WALL的
BitmapDrawable
实例来显示在屏幕上。它们将会被顺序地重复使用(例如:如果第一个完全移出了屏幕,使用setBounds()
方法在所需位置重新显示它),然后对其他实例也是同样如此,整个游戏都要这样。 - 目前的蓝色背景,如果它是更大地图的一部分,那么它需要适当地偏移。
- 我在撰写本文时想到的一个障碍是将WALL向下移动,直到它移出屏幕,这会导致GUY的死亡。
- 在我使用词语移动的那些地方,你需要使用
setBounds(a, b, c, d)
方法进行必要的基于位置的更改,因为我没有找到其他更新BitmapDrawable
实例位置的方法。我认为,你需要使用类似libGdx的游戏框架来获得像setOffset(x, y)
这样的豪华方法,或类似的方法。
抱歉,我只能为你提供这些想法,而没有具体的代码,因为我没有在这样的项目中工作的经验。希望这些对你有所帮助。
英文:
Try these tips in your code:(I assumed the game moves forward in the horizontal direction)
- The GUY should only move up and down(with the appropriate touch input) and not forward and backward as you want the focus(or camera alternatively) solely on the GUY.I noticed that the WALL was moving up in your video when the GUY moved from initial higher position of the wall to little bit lower position later, rectify this because the GUY should move down(try to implement Gravity effect).
- The WALL should only move forward(mostly) and backward(less often I guess).The WALL shouldn't move up and down normally. Do not apply Gravity effect to it. You can create at least 2
BitmapDrawable
instance of WALL for a screen. They are going to be reused sequencially(for eg: If the 1st one goes totally outside of the screen, reshow it in the desired position usingsetBounds()
method) and continue same for others the whole game. - The currently BLUE BACKGROUND, if it is a part of a larger map, then it needs to be appropriately offsetted.
- One of the obstacles that I can think of at the time of writing this is to move the WALL down until it goes out of the screen which results in the death of the GUY.
- At those places, where I have used the word move, you need to use the
setBounds(a, b, c, d)
method to make necessary position based changes as I didn't find other way to update the position of aBitmapDrawable
instance. I think, you need to use game framework like libGdx to get method of luxury likesetOffset(x, y)
or of similar sort.
Sorry that I could only present you the ideas without specific code as I do not have past experience working in a project like this. Hope, it helps you in anyway possible.
专注分享java语言的经验与见解,让所有开发者获益!
评论