java - Repopulate an array list once emptied -
what have game user presses sprite , when pressed add score , make sprite disappear. issue i'm having can't figure out how make sprites reappear once pressed being removed array list completely, readd these array list or in different way?
this sprite code:
package cct.mad.lab; import java.util.random; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.os.vibrator; public class sprite { //x,y position of sprite - initial position (0,50) private gameview gameview; private bitmap spritebmp; //width , height of sprite image private int bmp_width; private int bmp_height; // needed new random coordinates. private random random = new random(); private int x = random.nextint(200)-1; private int y = random.nextint(200)-1; int xspeed = (random.nextint(30)-15); int yspeed = (random.nextint(30)-15); public sprite(gameview gameview) { this.gameview=gameview; spritebmp = bitmapfactory.decoderesource(gameview.getresources(), r.drawable.spritehead); this.bmp_width = spritebmp.getwidth(); this.bmp_height= spritebmp.getheight(); //random y coordinate sprite spawn x = gameview.getwidth(); x = random.nextint(x); y = gameview.getheight(); y = random.nextint(y); } //update position of sprite public void update() { x = x + xspeed; y = y + yspeed; wraparound(); //adjust motion of sprite. } public void draw(canvas canvas) { //draw sprite image canvas.drawbitmap(spritebmp, x , y, null); } //y -= gameview.getheight();//reset y public void wraparound(){ //code wrap around //increment x whilst not off screen if (x >= (gameview.getwidth() - 40)){ //if gone off right sides of screen xspeed = (xspeed * -1); } if (x <= -10) { xspeed = (xspeed * -1); } if (y >= (gameview.getheight() - 40)){//if gone off bottom of screen yspeed = (yspeed * -1); } if (y <= 0)//if gone off top of screen { yspeed = (yspeed * -1); } xspeed = (xspeed * -1); } /* checks if sprite touched. */ public boolean wasittouched(float ex, float ey) { boolean touched = false; if ((x <= ex) && (ex < x + bmp_width) && (y <= ey) && (ey < y + bmp_height)) { touched = true; } return touched; }//end of wasittouched }
and code displaying items , array list:
public void surfacecreated(surfaceholder holder) { // can safely setup game start game loop. resetgame();//set new game - called 'play again option' gameloopthread = new gameloopthread(this.getholder(), this); gameloopthread.running = true; gameloopthread.start(); mbackgroundimage = bitmap.createscaledbitmap(mbackgroundimage, getwidth(), getheight(), true); (int sp =0; spritesarraylist.size() < spnumber; sp++) { spritesarraylist.add(sprite = new sprite(this)); } }
i'm not sure why won't work
when have add/remove items object, use hashmap
.
in case, hashmap<string,sprite>
, can add/remove items identifying them through the hash key.
e.g.
//hashmap<string,sprite> hashsprite //add hashsprite.put("sprite1",new sprite(this)); //remove hashsprite.remove("sprite1")
i know it's not real answer, suggestion appreciated!
Comments
Post a Comment