卡住视图滚动以跟随精灵

发布于 2024-12-20 04:08:42 字数 1295 浏览 4 评论 0原文

我对编程非常陌生,目前正在尝试为 Android 制作一个简单的游戏。我遇到的问题是视图滚动的方式。按照我的逻辑,我相信精灵应该留在屏幕的中央,但事实并非如此。视图确实跟随精灵,但速度不一样(如果这是有道理的话)。从观点来看有点落后。它并不跳跃,所以我知道它运行得并不慢。但令我惊讶的是,我将滚动矩形设置为等于玩家 x 位置 - 显示宽度,然后将 y 轴设置为相同。所以我希望你们能帮助我找出为什么精灵没有停留在视图的中心。任何帮助将不胜感激。

谢谢。

private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with

Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();             
displayHeight = display.getHeight();   

displayRect = new Rect(0, 0, displayWidth, displayHeight);
scrollRect = new Rect(0, 0, displayWidth, displayHeight);

//设置滚动矩形的新左上角

    newScrollRectX = ((int)player.getXLocation() - (displayWidth/2));
    newScrollRectY = ((int)player.getYLocation() - (displayHeight/2));

//这是在我的onDraw方法中,因此它在绘制玩家之前更新

    scrollRect.set(newScrollRectX, newScrollRectY,
    newScrollRectX + displayWidth, newScrollRectY + displayHeight);

//bmLargeImage是1440X1440背景

    canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);

canvas.drawBitmap(player.getBitmap(),(float)player.getX(player.getSpeedX()),     (float)player.getY(player.getSpeedY()), null);

I'm very new to programming and am currently trying to make a simple game for android. The problem I'm having is the way the view is scrolling. By my logic, I believe the sprite should stay in the center of the screen, but it doesn't. The view does follow the sprite, but not at the same speed if that makes sense. As in the view kind of lags behind. It's not jumpy so I know it's not running slowly. What surprises me though is I'm setting my scrolling rectangle equal to player x location - display width, and then same for the y axis. So What I'm hoping you guys can help me out with is figure out why the sprite isn't staying in the center of the view. Any help will be greatly appreciated.

Thanks.

private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with

Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();             
displayHeight = display.getHeight();   

displayRect = new Rect(0, 0, displayWidth, displayHeight);
scrollRect = new Rect(0, 0, displayWidth, displayHeight);

//Setting the new upper left corner of the scrolling rectangle

    newScrollRectX = ((int)player.getXLocation() - (displayWidth/2));
    newScrollRectY = ((int)player.getYLocation() - (displayHeight/2));

//This is in my onDraw method, so it updates right before the player is drawn

    scrollRect.set(newScrollRectX, newScrollRectY,
    newScrollRectX + displayWidth, newScrollRectY + displayHeight);

//bmLargeImage is a 1440X1440 background

    canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);

canvas.drawBitmap(player.getBitmap(),(float)player.getX(player.getSpeedX()),     (float)player.getY(player.getSpeedY()), null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-12-27 04:08:42

我的第一个猜测是您正在使用整数计算,而应该使用浮点计算。

int test = 10 / 3; // == 3
float test2 = 10 / 3.0; // == 3.33333...

这可以解释“跳跃”。


请注意,我不确定是否所有 Android 手机都有 FPU。您可能必须使用定点计算来保证正确的性能。

My first guess would be you are using integer calculations and should use floating point calculations instead.

int test = 10 / 3; // == 3
float test2 = 10 / 3.0; // == 3.33333...

This would explain the 'jumping'.


Beware, I am not certain whether all android phones have FPUs. You might have to use fixed point calculations instead to guarantee proper performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文