То, что у меня есть в настоящее время - игра, в которой пользователь нажимает спрайт и при нажатии ее добавит к их счету, а затем сделает так, что спрайт исчезнет. Проблема, с которой я сталкиваюсь, заключается в том, что я не могу понять, как заставить спрайты снова появляться после их нажатия, поскольку они полностью удаляются из списка массивов, я бы прочитал их в списке массивов или сделаю это в другой путь?Восстановить список массивов после опустошения
Это код спрайта:
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 and Height of the Sprite image
private int bmp_width;
private int bmp_height;
// Needed for 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 for sprite spawn
x = gameView.getWidth();
x = random.nextInt(x);
y = gameView.getHeight();
y = random.nextInt(y);
}
//update the position of the 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 to wrap around
//increment x whilst not off screen
if (x >= (gameView.getWidth() - 40)){ //if gone off the right sides of screen
xSpeed = (xSpeed * -1);
}
if (x <= -10)
{
xSpeed = (xSpeed * -1);
}
if (y >= (gameView.getHeight() - 40)){//if gone off the bottom of screen
ySpeed = (ySpeed * -1);
}
if (y <= 0)//if gone off the top of the screen
{
ySpeed = (ySpeed * -1);
}
xSpeed = (xSpeed * -1);
}
/* Checks if the Sprite was 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
}
и это мой код на самом деле отображения элементов и списка массива:
public void surfaceCreated(SurfaceHolder holder) {
// We can now safely setup the game start the game loop.
ResetGame();//Set up a new game up - could be called by a 'play again option'
gameLoopThread = new GameLoopThread(this.getHolder(), this);
gameLoopThread.running = true;
gameLoopThread.start();
mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true);
for (int sp =0; spritesArrayList.size() < spNumber; sp++) {
spritesArrayList.add(sprite = new Sprite(this));
}
}
Я не знаю, почему она не будет работать
Где вы определяете/инициализируете 'spritesArrayList'? – MikeKeepsOnShine
Это просто: private ArrayList spritesArrayList; int spNumber = 5; @mikekeepsonshine –
harry
, пожалуйста, отправьте код, где 'spritesArrayList' инициализируется и работает. – Kishore