wrior
01-30-2011, 07:47 PM
Hi everyone, i'm having a problem when overriding the onTouchEvent method in my custom surface view class in android. See I am trying to make a game in android but have run into a major problem. As far as im concerned the code below should keep the xChange variable equal to either 1 or -1 as long as the left or right side of the screen is touched and should set it back to zero when i take my finger off the screen.
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(event.getX()<(getWidth()/3)){
xChange = -1;
}else if(event.getX()>((getWidth()/3)*2)){
xChange = 1;
}
}
if(event.getAction()==MotionEvent.ACTION_UP){
xChange=0;
}
return super.onTouchEvent(event);
}
but the problem is when i take my finger of the screen the variable stays equal to either -1 or 1 causing my block object to keep moving either left or right. Heres the Full code for my Game View Class:
package net.wiorsoft.blocks;
import java.util.Iterator;
import java.util.Vector;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
private GameLoop thread;
Paint bPaint = new Paint();
int xChange = 0;
int colChange = 0;
int width,height;
Vector<Block> blocks = new Vector<Block>(12,3);
public GameView(Context context) {
super(context);
getHolder().addCallback(this);
thread = new GameLoop(getHolder(), this);
setFocusable(true);
bPaint.setColor(Color.BLACK);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
this.width=width/9;
this.height=height/18;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry=true;
while(retry){
try {
thread.join();
retry=false;
}catch(InterruptedException e){
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(event.getX()<(getWidth()/3)){
xChange = -1;
}else if(event.getX()>((getWidth()/3)*2)){
xChange = 1;
}
}
if(event.getAction()==MotionEvent.ACTION_UP){
xChange=0;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas){
}
public void newBlock(int color){
blocks.add(new Block(getWidth()/2,0,color,width,height));
}
public void update(){
for(Iterator<Block> ite = blocks.iterator(); ite.hasNext();){
Block temp = ite.next();
temp.updatePos(xChange);
}
}
public void render(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for(Iterator<Block> ite = blocks.iterator(); ite.hasNext();){
ite.next().draw(canvas, bPaint);
}
}
public class Block{
Rect rect = new Rect();
int xPos,yPos,colour,width,height;
public Block(int xPos,int yPos, int col,int blockWidth,int blockHeight){
this.xPos = xPos;
this.yPos = yPos;
this.colour = col;
this.width = blockWidth;
this.height = blockHeight;
rect.set(xPos, yPos, xPos+width, yPos+height);
}
public void updatePos(int xChange){
xPos += xChange;
if(yPos<(getHeight()-height)){
yPos = yPos+1;
}
rect.set(xPos, yPos, xPos+width, yPos+height);
}
public void draw(Canvas canvas,Paint paint){
paint.setColor(colour);
paint.setStyle(Style.FILL);
canvas.drawRect(rect, paint);
}
}
}
And For My GameLoop Class:
package net.wiorsoft.blocks;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
public class GameLoop extends Thread {
private static final String TAG = "GameLoop";
private SurfaceHolder gViewHolder;
private GameView gView;
private boolean running;
private long time;
int fps = 30;
int targetFPS;
public void setRunning(boolean running){
this.running=running;
}
public GameLoop(SurfaceHolder sHolder, GameView gView){
super();
this.gViewHolder = sHolder;
this.gView = gView;
targetFPS=1000 / fps;
}
public void run(){
Canvas canvas;
gView.newBlock(Color.RED);
while(running){
do{
time = System.currentTimeMillis();
canvas=null;
try{
canvas = this.gViewHolder.lockCanvas();
gView.update();
gView.render(canvas);
}finally{
if(canvas!=null){
this.gViewHolder.unlockCanvasAndPost(canvas);
}
}
}while(((System.currentTimeMillis()-time)>=targetFPS));
}
}
}
Thanks in advance to anyone who can help
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(event.getX()<(getWidth()/3)){
xChange = -1;
}else if(event.getX()>((getWidth()/3)*2)){
xChange = 1;
}
}
if(event.getAction()==MotionEvent.ACTION_UP){
xChange=0;
}
return super.onTouchEvent(event);
}
but the problem is when i take my finger of the screen the variable stays equal to either -1 or 1 causing my block object to keep moving either left or right. Heres the Full code for my Game View Class:
package net.wiorsoft.blocks;
import java.util.Iterator;
import java.util.Vector;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
private GameLoop thread;
Paint bPaint = new Paint();
int xChange = 0;
int colChange = 0;
int width,height;
Vector<Block> blocks = new Vector<Block>(12,3);
public GameView(Context context) {
super(context);
getHolder().addCallback(this);
thread = new GameLoop(getHolder(), this);
setFocusable(true);
bPaint.setColor(Color.BLACK);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
this.width=width/9;
this.height=height/18;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry=true;
while(retry){
try {
thread.join();
retry=false;
}catch(InterruptedException e){
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(event.getX()<(getWidth()/3)){
xChange = -1;
}else if(event.getX()>((getWidth()/3)*2)){
xChange = 1;
}
}
if(event.getAction()==MotionEvent.ACTION_UP){
xChange=0;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas){
}
public void newBlock(int color){
blocks.add(new Block(getWidth()/2,0,color,width,height));
}
public void update(){
for(Iterator<Block> ite = blocks.iterator(); ite.hasNext();){
Block temp = ite.next();
temp.updatePos(xChange);
}
}
public void render(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for(Iterator<Block> ite = blocks.iterator(); ite.hasNext();){
ite.next().draw(canvas, bPaint);
}
}
public class Block{
Rect rect = new Rect();
int xPos,yPos,colour,width,height;
public Block(int xPos,int yPos, int col,int blockWidth,int blockHeight){
this.xPos = xPos;
this.yPos = yPos;
this.colour = col;
this.width = blockWidth;
this.height = blockHeight;
rect.set(xPos, yPos, xPos+width, yPos+height);
}
public void updatePos(int xChange){
xPos += xChange;
if(yPos<(getHeight()-height)){
yPos = yPos+1;
}
rect.set(xPos, yPos, xPos+width, yPos+height);
}
public void draw(Canvas canvas,Paint paint){
paint.setColor(colour);
paint.setStyle(Style.FILL);
canvas.drawRect(rect, paint);
}
}
}
And For My GameLoop Class:
package net.wiorsoft.blocks;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
public class GameLoop extends Thread {
private static final String TAG = "GameLoop";
private SurfaceHolder gViewHolder;
private GameView gView;
private boolean running;
private long time;
int fps = 30;
int targetFPS;
public void setRunning(boolean running){
this.running=running;
}
public GameLoop(SurfaceHolder sHolder, GameView gView){
super();
this.gViewHolder = sHolder;
this.gView = gView;
targetFPS=1000 / fps;
}
public void run(){
Canvas canvas;
gView.newBlock(Color.RED);
while(running){
do{
time = System.currentTimeMillis();
canvas=null;
try{
canvas = this.gViewHolder.lockCanvas();
gView.update();
gView.render(canvas);
}finally{
if(canvas!=null){
this.gViewHolder.unlockCanvasAndPost(canvas);
}
}
}while(((System.currentTimeMillis()-time)>=targetFPS));
}
}
}
Thanks in advance to anyone who can help