如何在屏幕上显示按钮?

发布于 2024-12-19 13:43:03 字数 552 浏览 4 评论 0原文

所以我正在为 Android 制作一款游戏,基本上涉及一艘太空飞船在屏幕上左右移动(锁定在纵向模式),试图躲避从屏幕顶部飞向它的物体。我有一个 GraphicsView 类(它扩展了 View),用于处理所有绘图等。我试图在按钮上显示两个按钮,一个将船移动到左侧,一个将船移动到右侧。我的代码遍布各处:

Button mLeft;

mLeft = new Button (context);

mLeft.setX (0);
mLeft.setY (mDisplay.getHeight () - 64);
mLeft.setHeight (64);
mLeft.setWidth (64);
mLeft.setBackgroundColor (80000000);
mLeft.setText ("<");

每当我尝试调用上面的 mLeft 的任何方法时,我的程序都会崩溃。我尝试将它们全部注释掉,然后将它们单独添加回来,看看它们中的任何一个是否不会崩溃,但它们都会崩溃。我可以在程序不崩溃的情况下执行 mLeft.draw (画布),但屏幕上没有显示任何内容。

对此的任何帮助将不胜感激,谢谢。

So I am making a game for Android that basically involves a space ship moving left and right on the screen (locked in portrait mode) that is trying to dodge objects flying at it from the top of the screen. I have a class GraphicsView (it extends View) that deals with all of the drawing and whatnot. I am trying to display two buttons on the button, one to move the ship to the left and one to move the ship to the right. I have this code strewn throughout:

Button mLeft;

mLeft = new Button (context);

mLeft.setX (0);
mLeft.setY (mDisplay.getHeight () - 64);
mLeft.setHeight (64);
mLeft.setWidth (64);
mLeft.setBackgroundColor (80000000);
mLeft.setText ("<");

Whenever I try to call any of these methods of mLeft above, my program crashes. I have tried commenting all of them out, then adding them back individually to see if any one of them won't crash, but ll of them do. I am able to do mLeft.draw (canvas) without the program crashing, but nothing shows up on the screen.

Any help on this would be very appreciated, thank you.

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

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

发布评论

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

评论(2

往日情怀 2024-12-26 13:43:03

您添加了 mLeft.setOnClickListener() 吗?另外,对于onDraw(),您是否在方法末尾添加了invalidate()

did you add mLeft.setOnClickListener() . Also, for the onDraw(), did you add invalidate() at the end of the method?

掩耳倾听 2024-12-26 13:43:03

这可能是因为 setText("<")。尝试将其更改为其他内容。

It could be because of setText("<"). Try changing that to something else.

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