I have just started developing in Android and I am trying to make a very basic game where you have to move a bat along the bottom of the screen and catch items while avoiding bombs.
The problem I am having is I want the bat to move along the bottom of the screen when you hold your finger on the left or the right side of the screen.
Currently I can get the bat to move a few pixels across when the user touches the screen but I can not get it to keep moving until the user removes his finger from the screen.
Here is my (very) basic code so far :
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;
}
}
The catcher.moveLeft() method is as follows:
public void moveLeft()
{
posX -= 5;
}
Any help with this matter would be greatly appreciated! :)
Aucun commentaire:
Enregistrer un commentaire