Jump to content

EDIT: nevermind, I got the project finished.

 

So I have Android lessons at school and we had to make this tutorial:

http://www.javacodegeeks.com/2011/07/android-game-development-displaying.html

And it's 'sequel':

http://www.javacodegeeks.com/2011/07/android-game-development-moving-images.html

 

Well I did that and it all worked.. But now we have to make it so 5 sprites appear on the screen and you have to be able to move them all separately.

(And some other stuff, like having them be able to collide into each other, but I think I would be able to do that myself).

 

Anyways, I think this is how the spirte gets added to the screen or how you would call that..

 

 

The first piece of code was I think for initializing the sprite.. And the second for updating.. I have no idea how I would make it so I have multiple spirtes.. Any help is appreciated 

package com.example.opdracht1;import android.graphics.Bitmap;import android.graphics.Canvas;import android.view.MotionEvent;public class Droid { private Bitmap bitmap; // the actual bitmap private int x;   // the X coordinate private int y;   // the Y coordinate private boolean touched; // if droid is touched/picked up  private Speed speed; public Droid(Bitmap bitmap, int x, int y) {  this.bitmap = bitmap;  this.x = x;  this.y = y;  speed = new Speed(1, 0.5f); } public Bitmap getBitmap() {  return bitmap; } public void setBitmap(Bitmap bitmap) {  this.bitmap = bitmap; } public int getX() {  return x; } public void setX(int x) {  this.x = x; } public int getY() {  return y; } public void setY(int y) {  this.y = y; } public boolean isTouched() {  return touched; } public void setTouched(boolean touched) {  this.touched = touched; } public void draw(Canvas canvas) {  canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null); } public void handleActionDown(int eventX, int eventY) {	  if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {	   if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {	    // droid touched	    setTouched(true);	   } else {	    setTouched(false);	   }	  } else {	   setTouched(false);	  }	 } public void update() {		if (!touched) {			x += (speed.getXv() * speed.getxDirection());			y += (speed.getYv() * speed.getyDirection());		}	} public Speed getSpeed() {	 return speed;}}
public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) {   // delegating event handling to the droid   droid.handleActionDown((int)event.getX(), (int)event.getY());   // check if in the lower part of the screen we exit   if (event.getY() > getHeight() - 50) {    thread.setRunning(false);    ((Activity)getContext()).finish();   } else {    Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());   }  } if (event.getAction() == MotionEvent.ACTION_MOVE) {   // the gestures   if (droid.isTouched()) {    // the droid was picked up and is being dragged    droid.setX((int)event.getX());    droid.setY((int)event.getY());   }  } if (event.getAction() == MotionEvent.ACTION_UP) {   // touch was released   if (droid.isTouched()) {    droid.setTouched(false);   }  }  return true; } @[member=OverRide] protected void onDraw(Canvas canvas) {  // fills the canvas with black  canvas.drawColor(Color.BLACK);  droid.draw(canvas); } public void update() {		// check collision with right wall if heading right		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT				&& droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {			droid.getSpeed().toggleXDirection();		}		// check collision with left wall if heading left		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT				&& droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {			droid.getSpeed().toggleXDirection();		}		// check collision with bottom wall if heading down		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN				&& droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {			droid.getSpeed().toggleYDirection();		}		// check collision with top wall if heading up		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP				&& droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {			droid.getSpeed().toggleYDirection();		}		// Update the lone droid		droid.update();	}

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/307304-android-programming-help/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×