Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Disable one while loop from another while loop

android java while loops while()

Best Answer Plecostomus129, 16 May 2014 - 02:12 PM

Ok, I figured it out. I just needed to change the while loop integers to a different number. I have no idea how this works, but it does.

while (touch.y > 50) {
			try {
				Thread.sleep(1);
				touch.y--;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		while (touch.y < 500) {
			try {
				Thread.sleep(1);
				touch.y++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 Plecostomus129

Plecostomus129

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 14 May 2014 - 02:20 PM

I am creating an android application, and I need to use a 2 while loops to make a ball go up, but come down once it reaches a number. Right now, when the screen is touched, this is the code it runs:

while (touch.y > 50) {
			try {
				Thread.sleep(5);
				touch.y--;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		while (touch.y <=50) {
			try {
				Thread.sleep(5);
				touch.y++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}

I want the ball's y-axis integer (touch.y) to go up after it gets down to 50, but when I run the code, the first while loop runs perfectly, but the second one is cancelled out by the first one. Is there any simple way to cancel out one while loop from another to restrict interference? Here is my full source code, but I think that I only need help with the while loops

import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;

public class MainActivity extends Activity implements OnTouchListener {
	DrawView drawView;
	
	Point touch = new Point(79,492);
	String inputAction = "";
	
	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		drawView = new DrawView(this);
		setContentView(drawView);
		
		drawView.setOnTouchListener(this);
	}
	
	@Override public void onResume() {
		super.onResume();
		drawView.resume();
	}
	
	@Override public void onPause() {
		super.onPause();
		drawView.pause();
	}
	
	@Override public boolean onTouch(View v, MotionEvent event) {
		switch(event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			inputAction = "DOWN";
			break;
		case MotionEvent.ACTION_MOVE:
			inputAction = "MOVE";
			break;
		case MotionEvent.ACTION_UP:
			inputAction = "UP";
			break;
		}
		
		while (touch.y > 50) {
			try {
				Thread.sleep(5);
				touch.y--;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		while (touch.y <=50) {
			try {
				Thread.sleep(5);
				touch.y++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		
		return true;
	}
	
	public class DrawView extends SurfaceView implements Runnable {
		
		Thread gameloop = null;
		
		SurfaceHolder surface = null;
		
		volatile boolean running = false;
		
		AssetManager assets = null;
		
		public DrawView(Context context) {
			super(context);
			
			surface = getHolder();
			
			assets = context.getAssets();
		}
		
		public void resume() {
			running = true;
			gameloop = new Thread(this);
			gameloop.start();
		}
		
		public void pause() {
			running = false;
			while (true) {
				try {
					gameloop.join();
				}
				catch (InterruptedException e) { }
			}
		}
		
		@Override public void run() {
			
			while (running) {
				if (!surface.getSurface().isValid()) continue;
				
				Canvas canvas = surface.lockCanvas();
				canvas.drawColor(Color.BLACK);
				
				Paint paint = new Paint();
				paint.setColor(Color.WHITE);
					canvas.drawCircle(touch.x, touch.y, 50, paint);
				
				surface.unlockCanvasAndPost(canvas);
				
				try {
					Thread.sleep(20);
				}
				catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

Note: This file is for android. It needs to be run in an android device to work, and needs to be compiled into an APK



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 14 May 2014 - 03:29 PM

There's many things to be said.

 

First I wouldn't do the loops on the event handler. In fact, I would flag the event for not to work if it's still processing a previous touch.

Then secondly, the second loop isn't interrupted, the thing is that it starts at it's peak value (50)  then adds one (51) and get's out of the loop.



#3 Plecostomus129

Plecostomus129

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 16 May 2014 - 02:12 PM   Best Answer

Ok, I figured it out. I just needed to change the while loop integers to a different number. I have no idea how this works, but it does.

while (touch.y > 50) {
			try {
				Thread.sleep(1);
				touch.y--;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		while (touch.y < 500) {
			try {
				Thread.sleep(1);
				touch.y++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download