我刚刚开始在Android中开发,我正在尝试制作一个非常基本的游戏,你必须在屏幕底部移动一个蝙蝠并捕捉物品,同时避免炸弹.
我遇到的问题是,当您将手指放在屏幕的左侧或右侧时,我希望球棒沿着屏幕底部移动.
目前,当用户触摸屏幕时,我可以让蝙蝠移动几个像素,但是在用户将手指从屏幕上移开之前我无法继续移动.
到目前为止,这是我的(非常)基本代码:
package com.mattdrewery.supercatch;
import android.view.View;
import android.view.MotionEvent;
import android.content.Context;
import android.graphics.Canvas;
public class GameView extends View
{
private Catcher catcher;
public GameView(Context context)
{
super(context);
setFocusable(true);
// Create the catcher
catcher = new Catcher(context, R.drawable.catcher, 240, 250);
}
@Override
protected void onDraw(Canvas canvas)
{
// Draw the catcher to the canvas
canvas.drawBitmap(catcher.getImage(), catcher.getPosX(), catcher.getPosY(), null);
// Redraw the screen
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
// Get the action from the touch screen
int eventAction = event.getAction();
int X = (int) event.getX();
int Y = (int) event.getY();
// If the user presses on the screen....
if (eventAction == MotionEvent.ACTION_DOWN)
{
catcher.moveLeft();
}
// Redraw the screen
invalidate();
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
catcher.moveLeft()方法如下:
public void moveLeft()
{
posX -= 5;
}
Run Code Online (Sandbox Code Playgroud)
任何有关此事的帮助将不胜感激!:)
我认为这可能会奏效:
boolean actionUpFlag = false;
if (eventAction == MotionEvent.ACTION_DOWN)
{
actionUpFlag = true;
}
else if (eventAction == MotionEvent.ACTION_UP)
{
actionUpFlag = false;
}
while (actionUpFlag)
{
catcher.moveLeft();
}
Run Code Online (Sandbox Code Playgroud)
这是你想要的 ?
| 归档时间: |
|
| 查看次数: |
9233 次 |
| 最近记录: |