Java 编程帮助和 Android 开发

发布于 2024-11-26 01:29:16 字数 659 浏览 2 评论 0原文

当用户触摸屏幕时,我试图在另一个图像上绘制一个图像。但图像仅在用户触摸时出现,然后在松开屏幕时消失。

这是我的 if 方法:

int RED = 0;
int GREEN = 1;
Graphics g = game.getGraphics();
int len = touchEvents.size();

if (RED == 0) {
    ready = Assets.readybtntwo;
}

for(int i = 0; i < len; i++) {
    TouchEvent event = touchEvents.get(i);

    if(event.type == TouchEvent.TOUCH_UP){
        updateWaiting(touchEvents);
        RED +=1;

        if (RED == 0)
            ready = Assets.readybtntwo;

        if(RED == 1)
            ready = Assets.readybtngreentwo;
    }

    g.drawPixmap(ready, 0, 0);
}

抱歉,我正在使用从 android games 开始的书构建的框架。但这并不重要,我希望图像永远保留并终止 if 循环。

I am trying to draw an image over another image when a user touches the screen. But the image only appears when the user touches and then disappears when they let go of the screen.

This is my if method:

int RED = 0;
int GREEN = 1;
Graphics g = game.getGraphics();
int len = touchEvents.size();

if (RED == 0) {
    ready = Assets.readybtntwo;
}

for(int i = 0; i < len; i++) {
    TouchEvent event = touchEvents.get(i);

    if(event.type == TouchEvent.TOUCH_UP){
        updateWaiting(touchEvents);
        RED +=1;

        if (RED == 0)
            ready = Assets.readybtntwo;

        if(RED == 1)
            ready = Assets.readybtngreentwo;
    }

    g.drawPixmap(ready, 0, 0);
}

Sorry I'm using a framework built from the book beginning android games. But it shouldn't matter, I want the image to stay forever and terminate the if loop.

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

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

发布评论

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

评论(1

地狱即天堂 2024-12-03 01:29:16

你这里有一个逻辑错误。如果上述代码全部位于触摸事件处理程序内,则看起来变量 RED 是您的方法的本地变量。这意味着当用户每次触摸屏幕时它都会重置为 0,然后再次变为 1。这可能不是你想要的。

只渲染它的原因是 g.drawPixmap 要么将元素提交到绘制队列,要么立即渲染它。这种绘制按钮的方法仅在发生触摸事件时才会被绘制!

相反,您可以将布尔值 drawGreenReadyButton 值作为类成员,即

private boolean drawGreenReadyButton = false;

然后您可以将该内部 if 语句更改为以下内容:

if(event.type == TouchEvent.TOUCH_UP){
    updateWaiting(touchEvents);
    drawGreenReadyButton = true;
}

在主渲染循环内而不是在触摸事件处理程序中放置:

if(drawGreenReadyButton) {
    g.drawPixmap(Assets.readybtngreentwo, 0, 0);
} else {
    g.drawPixmap(Assets.readybtntwo, 0, 0);
}

另外,请考虑使用 TouchEvent .TOUCH_DOWN 而不是 TouchEvent.TOUCH_UP,这样按钮在触摸屏幕时就会显示为绿色,而不是在抬起手指时。

You have a logic error here. It looks like the variable RED is local to your method if the above code is all inside the touch event handler. This means that when the user touches the screen each time it's reset to 0 and then becomes 1 again. This probably isn't what you want.

The reason that it's only rendered is that g.drawPixmap will either be submitting an element to a draw queue or rendering it immediately. This method to draw the button is only ever being drawn when you have a touch event!

Instead, you could have a boolean drawGreenReadyButton value as a class member, i.e.

private boolean drawGreenReadyButton = false;

Then you can change that inner if statement to the following:

if(event.type == TouchEvent.TOUCH_UP){
    updateWaiting(touchEvents);
    drawGreenReadyButton = true;
}

And inside your main rendering loop rather than in the touch event handler put:

if(drawGreenReadyButton) {
    g.drawPixmap(Assets.readybtngreentwo, 0, 0);
} else {
    g.drawPixmap(Assets.readybtntwo, 0, 0);
}

Also, consider using TouchEvent.TOUCH_DOWN rather than TouchEvent.TOUCH_UP, so that the button is shown green as soon as they touch the screen, not when they lift their finger up.

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