我的第一个 Android 游戏不响应触摸输入
我正在创建一个非常简单的游戏,因为我是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将侦听器注册到 SurfaceView
尝试将侦听器附加到视图的布局,如下所示
Instead of registering your listener to surfaceview
Try attaching the listener to the layout for the view something like this