我的第一个 Android 游戏不响应触摸输入

发布于 2025-01-06 13:46:35 字数 4050 浏览 1 评论 0原文

我正在创建一个非常简单的游戏,因为我是在 android 平台上开发的新手,但一般来说不是编程。无论如何,当我通过模拟器运行应用程序时,或者有时通过实际设备运行应用程序时,我看到的只是屏幕上绘制的两个桨,包括球,当我触摸屏幕或用鼠标模拟触摸时, onTouch 事件不会被触发。我不知道为什么,任何人都可以指出我正确的方向,我是在 Android 设备上开发的新手。

代码:

package main.game;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.*;
public class DrawingArea extends SurfaceView
{
    private Ball ball = null;
    private Paddle player = null;
    private Paddle computer = null;
    private Vector2 playerPos = Vector2.Zero;
    private boolean startGame = false;
    private boolean initiated = false;
    private Paint paint;
    public DrawingArea(Context context, Paint paint) {
        super(context);
        setOnTouchListener(new Touch());
        setWillNotDraw(false);
        this.paint = paint;


    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        if (initiated == false)
        {
            ball = new Ball(getWidth() / 2.0f, getHeight() / 2.0f, 32, paint);
            player = new Paddle(playerPos.getX(), playerPos.getY(), 32, 128, paint);
            Paint p = new Paint();
            p.set(paint);
            p.setColor(Color.GREEN);
            computer = new Paddle(getWidth() - 32, 0, 32, 128, p);
            GameLoop gloop = new GameLoop(this);
            gloop.start();
            UpdateLoop uloop = new UpdateLoop(this);
            uloop.start();
            initiated = true;
        }
        ball.draw(canvas);
        player.draw(canvas);
        computer.draw(canvas);

    }
    class GameLoop extends Thread
    {
        private DrawingArea area;
        private boolean running = true;
        public GameLoop(DrawingArea area)
        {
            this.area = area;
        }
        @Override
        public void run() {
            int frame = 0;
            while(running)
            {

                this.area.invalidate();
                if(frame == Integer.MAX_VALUE) { frame = 0;}
                frame++;
            }


        }
        public void closeThread()
        {
            running = false;
        }

    }
    class Touch implements View.OnTouchListener
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            startGame = true;
            playerPos = new Vector2(0, event.getY());
            if (playerPos.getY() < (player.getY() + 256))
            {
                player.translateY(-0.5f);
            }
            else if (playerPos.getY() > (player.getY() + 256))
            {
                player.translateY(0.5f);
            }
            return true;
        }

    }
    class UpdateLoop extends Thread
    {
        private DrawingArea area;
        private boolean running = true;
        private boolean forward = true;
        public UpdateLoop(DrawingArea area)
        {
            this.area = area;
        }
        @Override
        public void run() {
            int frame = 0;
            while(running)
            {


                if (startGame == true)
                ball.translate(0.5f, 0.5f, 45.0f, forward);
                if (ball.getX() < 0 || ball.getY() < 0 || ball.getX() > area.getWidth()|| ball.getY() > area.getHeight()|| ball.Intersects(computer) || ball.Intersects(player))
                {
                    if(forward == true)
                    forward = false;
                    else 
                    forward = true;
                }
                if(frame == Integer.MAX_VALUE) { frame = 0;}
                frame++;
            }
        }
    }

}

更新:

              DrawingArea area = new DrawingArea(this, paint);
            setContentView(R.layout.main);
            LinearLayout main = (LinearLayout)findViewById(R.id.main);
            main.addView(area, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            main.setOnTouchListener(this);

I am creating a very simple game, because I am new to developing on the android platform, but not programming, in general. Anyway, when I run the app via the emulator, or sometimes through an actual device, all I see are the two paddles being drawn, including ball, on the screen, and when I touch the screen, or simulate touching with a mouse, the event onTouch, doesn't get fired. I do not know why, can anyone point me to the right direction, I am new to developing on android devices.

Code:

package main.game;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.*;
public class DrawingArea extends SurfaceView
{
    private Ball ball = null;
    private Paddle player = null;
    private Paddle computer = null;
    private Vector2 playerPos = Vector2.Zero;
    private boolean startGame = false;
    private boolean initiated = false;
    private Paint paint;
    public DrawingArea(Context context, Paint paint) {
        super(context);
        setOnTouchListener(new Touch());
        setWillNotDraw(false);
        this.paint = paint;


    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        if (initiated == false)
        {
            ball = new Ball(getWidth() / 2.0f, getHeight() / 2.0f, 32, paint);
            player = new Paddle(playerPos.getX(), playerPos.getY(), 32, 128, paint);
            Paint p = new Paint();
            p.set(paint);
            p.setColor(Color.GREEN);
            computer = new Paddle(getWidth() - 32, 0, 32, 128, p);
            GameLoop gloop = new GameLoop(this);
            gloop.start();
            UpdateLoop uloop = new UpdateLoop(this);
            uloop.start();
            initiated = true;
        }
        ball.draw(canvas);
        player.draw(canvas);
        computer.draw(canvas);

    }
    class GameLoop extends Thread
    {
        private DrawingArea area;
        private boolean running = true;
        public GameLoop(DrawingArea area)
        {
            this.area = area;
        }
        @Override
        public void run() {
            int frame = 0;
            while(running)
            {

                this.area.invalidate();
                if(frame == Integer.MAX_VALUE) { frame = 0;}
                frame++;
            }


        }
        public void closeThread()
        {
            running = false;
        }

    }
    class Touch implements View.OnTouchListener
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            startGame = true;
            playerPos = new Vector2(0, event.getY());
            if (playerPos.getY() < (player.getY() + 256))
            {
                player.translateY(-0.5f);
            }
            else if (playerPos.getY() > (player.getY() + 256))
            {
                player.translateY(0.5f);
            }
            return true;
        }

    }
    class UpdateLoop extends Thread
    {
        private DrawingArea area;
        private boolean running = true;
        private boolean forward = true;
        public UpdateLoop(DrawingArea area)
        {
            this.area = area;
        }
        @Override
        public void run() {
            int frame = 0;
            while(running)
            {


                if (startGame == true)
                ball.translate(0.5f, 0.5f, 45.0f, forward);
                if (ball.getX() < 0 || ball.getY() < 0 || ball.getX() > area.getWidth()|| ball.getY() > area.getHeight()|| ball.Intersects(computer) || ball.Intersects(player))
                {
                    if(forward == true)
                    forward = false;
                    else 
                    forward = true;
                }
                if(frame == Integer.MAX_VALUE) { frame = 0;}
                frame++;
            }
        }
    }

}

Update:

              DrawingArea area = new DrawingArea(this, paint);
            setContentView(R.layout.main);
            LinearLayout main = (LinearLayout)findViewById(R.id.main);
            main.addView(area, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            main.setOnTouchListener(this);

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

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

发布评论

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

评论(1

回心转意 2025-01-13 13:46:35

不要将侦听器注册到 SurfaceView

setOnTouchListener(new Touch());

尝试将侦听器附加到视图的布局,如下所示

   @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    //the whole screen becomes sensitive to touch
    mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
    mLinearLayoutMain.setOnTouchListener(this);
}

Instead of registering your listener to surfaceview

setOnTouchListener(new Touch());

Try attaching the listener to the layout for the view something like this

   @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    //the whole screen becomes sensitive to touch
    mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
    mLinearLayoutMain.setOnTouchListener(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文