Java 编程帮助和 Android 开发
当用户触摸屏幕时,我试图在另一个图像上绘制一个图像。但图像仅在用户触摸时出现,然后在松开屏幕时消失。
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你这里有一个逻辑错误。如果上述代码全部位于触摸事件处理程序内,则看起来变量 RED 是您的方法的本地变量。这意味着当用户每次触摸屏幕时它都会重置为 0,然后再次变为 1。这可能不是你想要的。
只渲染它的原因是 g.drawPixmap 要么将元素提交到绘制队列,要么立即渲染它。这种绘制按钮的方法仅在发生触摸事件时才会被绘制!
相反,您可以将布尔值 drawGreenReadyButton 值作为类成员,即
然后您可以将该内部 if 语句更改为以下内容:
在主渲染循环内而不是在触摸事件处理程序中放置:
另外,请考虑使用 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.
Then you can change that inner if statement to the following:
And inside your main rendering loop rather than in the touch event handler put:
Also, consider using
TouchEvent.TOUCH_DOWN
rather thanTouchEvent.TOUCH_UP
, so that the button is shown green as soon as they touch the screen, not when they lift their finger up.