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

时间:2012-02-16 22:56:42

标签: java android events touch

我正在创建一个非常简单的游戏,因为我不熟悉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);

1 个答案:

答案 0 :(得分:0)

而不是将你的听众注册到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);
}