有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在游戏中添加按钮,使两者都可见?

作为练习,我做了一个现成的蛇游戏(full thing here,我在here中遇到问题的部分),我尝试添加按钮来控制蛇的方向。因为它是简单的游戏顺时针转动蛇每当你点击屏幕

我已经创建了按钮并在R布局中绘制了它们。活动与游戏。我的问题是我不能让游戏和按钮同时显示。我要么设置ContentView(R.layout.activity_game)以获得白色背景上的按钮,要么设置ContentView(snakeView)以获得实际的游戏(或者两者兼而有之,但在这种情况下,代码后面出现的就是显示的那个)

关于如何做到这一点,我的最佳猜测是使用addContentView()添加R.layout。在snakeView的基础上进行活动,但要使背景透明,但我不知道如何做到这一点。 我还尝试将snakeView添加为ImageView,但根据我所能理解的,这是不可能的

以下是按钮的xml:

    <?xml version="1.0" encoding="utf-8"?>

<安卓x.constraintlayout.widget.ConstraintLayout

xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/frameLayout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".GameActivity">

<Button
    安卓:id="@+id/button8"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginStart="170dp"
    安卓:layout_marginLeft="170dp"
    安卓:layout_marginTop="542dp"
    安卓:layout_marginEnd="170dp"
    安卓:layout_marginRight="170dp"
    安卓:alpha="255"
    安卓:onClick="upButton"
    安卓:text="UP"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    安卓:id="@+id/button9"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="590dp"
    安卓:layout_marginEnd="82dp"
    安卓:layout_marginRight="82dp"
    安卓:layout_marginBottom="109dp"
    安卓:alpha="255"
    安卓:onClick="rightButton"
    安卓:text="RIGHT"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    安卓:id="@+id/button10"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginStart="170dp"
    安卓:layout_marginLeft="170dp"
    安卓:layout_marginEnd="170dp"
    安卓:layout_marginRight="170dp"
    安卓:layout_marginBottom="61dp"
    安卓:alpha="255"
    安卓:onClick="downButton"
    安卓:text="DOWN"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    安卓:id="@+id/button11"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginStart="82dp"
    安卓:layout_marginLeft="82dp"
    安卓:layout_marginTop="590dp"
    安卓:layout_marginBottom="109dp"
    安卓:alpha="255"
    安卓:onClick="leftButton"
    安卓:text="LEFT"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</安卓x.constraintlayout.widget.ConstraintLayout>

这是我添加到游戏活动中的额外代码,占据了第77-80行

public void upButton(View v) { directionOfTravel = 0; }
public void rightButton(View v) { directionOfTravel = 1; }
public void downButton(View v) { directionOfTravel = 2; }
public void leftButton(View v) { directionOfTravel = 3; }

提前感谢您的帮助

编辑:我曾尝试将安卓: alpha = "0"添加到XML的顶部,但这只会使整个内容变得透明。有没有办法只选择背景而不使用按钮


共 (0) 个答案