2 Replies - 1546 Views - Last Post: 02 November 2012 - 05:06 AM

#1 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

question about launching a projectile

Posted 27 October 2012 - 05:39 AM

I have a surface view class which draws my bitmaps, and I am having a bit of trouble getting an object to be launched from the center bottom of the screen to where the user clicks on the screen, here is a code snippet below
The onTouch method that gets the coordinates from user touch on screen
public boolean onTouch(View arg0, MotionEvent event) {

		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			x = event.getX();
			y = event.getY();
			sX = event.getX();
			sY = event.getY();
			fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
			break;
		case MotionEvent.ACTION_UP:
			fX = event.getX();
			fY = event.getY();
			dX = fX - sX;
			dY = fY - sY;
			sclX = dX / 30;
			sclY = dY / 30;
			//x = y = 0;
			break;
		}
		return true;
	}

the code that draws and updates the bitmaps on screen
public void run() {
			while (running) {
				if (!sh.getSurface().isValid())
					continue;

				canvas = sh.lockCanvas();
				canvas.drawRGB(02, 02, 150);
				if (x != 0 && y != 0)
					canvas.drawBitmap(cross, x - (cross.getWidth() / 2), y- (cross.getHeight() / 2), null);

				canvas.drawBitmap(sling,(canvas.getWidth() / 2) - (sling.getHeight() / 2),canvas.getHeight() - sling.getHeight(), null);
				if (fX != 0 && fY != 0) {
					canvas.drawBitmap(rock,	(canvas.getWidth()/2) + aniX,(canvas.getHeight()- sling.getHeight()) + aniY,null);
					//canvas.drawBitmap(cross, fX - (cross.getWidth() / 2), fY- (cross.getHeight() / 2), null);
				}

				aniX = aniX - sclX;
				aniY = aniY - sclY;
				
				sh.unlockCanvasAndPost(canvas);
			}

		}


Is This A Good Question/Topic? 0
  • +

Replies To: question about launching a projectile

#2 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question about launching a projectile

Posted 28 October 2012 - 01:10 PM

Got it solved, was a small error in calculation logics
public boolean onTouch(View arg0, MotionEvent event) {
		
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Log.d("ANIX", "X"+aniX + "Y"+ aniY);

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			x = event.getX();
			y = event.getY();
			fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
			break;
		case MotionEvent.ACTION_UP:
			fX = event.getX() - (cross.getWidth() / 2);
			fY = event.getY() - (cross.getHeight() / 2);
			dX = fX - sX;
			dY = fY - sY;
			sclX = dX / 30;
			sclY = dY / 30;
			break;
		case MotionEvent.ACTION_MOVE:
			x = event.getX();
			y = event.getY();
			break;
		}
		return true;
	}

Was This Post Helpful? 1
  • +
  • -

#3 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: question about launching a projectile

Posted 02 November 2012 - 05:06 AM

Interestingly enough, although Y works, getting the X-axis to work is quite harder. I still cannot figure out the X-axis now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1