I'm building a game for my Android app studio class, and the game's objective is to pop the bubbles. I currently have 7 bubble textures installed in the game and set up under the Bubble.java file (See Below) in the Gameview.java file I have the game generating the bubbles and then moving them down. I followed a tutorial to get the game to this point and have been trying to modify it to generate the other 6 bubbles, but the most I can get it to do is have the bubbles blink each frame through the different textures as they move down the screen. All the bubbles are the same X & Y of 200
I removed all imports for simplicity of the files.
public class Bubble {
Bitmap bubble[] = new Bitmap[7];
int bubbleFrame = 0;
int bubbleX, bubbleY, bubbleV;
Random random;
public Bubble(Context context){
bubble[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble0);
bubble[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble1);
bubble[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble2);
bubble[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble_green);
bubble[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble_blue);
bubble[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble_yellow);
bubble[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.bubble_red);
random = new Random();
resetPosition();
}
public Bitmap getBubble(int bubbleFrame){
return bubble[bubbleFrame];
}
public int getBubbleWidth(){
return bubble[0].getWidth();
}
public int getBubbleHeight(){
return bubble[0].getHeight();
}
public void resetPosition() {
bubbleX = random.nextInt(GameView.dWidth - getBubbleWidth());
bubbleY = -200 + random.nextInt(600) * -1;
bubbleV = 35 + random.nextInt(16);
}
}
Here is my Gameview file as well.
public class GameView extends View {
Bitmap background, ground, player;
Rect rectBackground, rectGround;
Context context;
Handler handler;
final long UPDATE_MILLIS = 30;
Runnable runnable;
Paint textPaint = new Paint();
Paint healthPaint = new Paint();
float TEXT_SIZE = 120;
int points = 0;
int life = 30;
static int dWidth, dHeight;
Random random;
float playerX, playerY;
float oldX;
float oldPlayerX;
ArrayList<Bubble> bubbles;
ArrayList<Pop> pops;
public GameView(Context context){
super(context);
this.context = context;
background = BitmapFactory.decodeResource(getResources(), R.drawable.background0);
ground = BitmapFactory.decodeResource(getResources(), R.drawable.ground);
player = BitmapFactory.decodeResource(getResources(), R.drawable.player);
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
dWidth = size.x;
dHeight = size.y;
rectBackground = new Rect(0,0,dWidth,dHeight);
rectGround = new Rect(0,dHeight - ground.getHeight(), dWidth, dHeight);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
invalidate();
}
};
textPaint.setColor(Color.rgb(255,165,0));
textPaint.setTextSize(TEXT_SIZE);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setTypeface(ResourcesCompat.getFont(context, R.font.bubblegum));
healthPaint.setColor(Color.GREEN);
random = new Random();
playerX = dWidth / 2 - player.getWidth() / 2;
playerY = dHeight - ground.getHeight() - player.getHeight();
bubbles = new ArrayList<>();
pops = new ArrayList<>();
// Add First Bubble to game
Bubble bubble = new Bubble(context);
bubbles.add(bubble);
}
// Draw Game
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(background, null, rectBackground, null);
canvas.drawBitmap(ground, null, rectGround, null);
canvas.drawBitmap(player, playerX, playerY, null);
// Draw Bubbles
for (int i=0; i<bubbles.size(); i++){
canvas.drawBitmap(bubbles.get(i).getBubble(bubbles.get(i).bubbleFrame), bubbles.get(i).bubbleX, bubbles.get(i).bubbleY, null);
bubbles.get(i).bubbleY += bubbles.get(i).bubbleV;
// Bubble Collision Detection with Ground - Remove Lives.
if (bubbles.get(i).bubbleY + bubbles.get(i).getBubbleHeight() >= dHeight - ground.getHeight()){
//life --;
Pop pop = new Pop(context);
pop.popX = bubbles.get(i).bubbleX;
pop.popY = bubbles.get(i).bubbleY;
pops.add(pop);
bubbles.get(i).resetPosition();
//Check for Life 0
if (life == 0){
Intent intent = new Intent(context, GameOver.class);
intent.putExtra("points", points);
context.startActivity(intent);
((Activity) context).finish();
}
}
}
// Player Collision Detection with Bubble - Add Points.
for (int i=0; i < bubbles.size(); i++){
if (bubbles.get(i).bubbleX + bubbles.get(i).getBubbleWidth() >= playerX
&& bubbles.get(i).bubbleX <= playerX + player.getWidth()
&& bubbles.get(i).bubbleY + bubbles.get(i).getBubbleWidth() >= playerY
&& bubbles.get(i).bubbleY + bubbles.get(i).getBubbleWidth() <= playerY + player.getHeight()){
points += 10;
// Add more bubbles based on Points value
if (points % 400 == 0 || points == 50 || points == 100 || points == 200){
Bubble bubble = new Bubble(context);
bubbles.add(bubble);
}
Pop pop = new Pop(context);
pop.popX = bubbles.get(i).bubbleX;
pop.popY = bubbles.get(i).bubbleY;
pops.add(pop);
bubbles.get(i).resetPosition();
}
}
// Remove Pop image after bubble is popped.
for (int i=0; i<pops.size(); i++){
canvas.drawBitmap(pops.get(i).getPop(pops.get(i).popFrame), pops.get(i).popX,
pops.get(i).popY, null);
pops.get(i).popFrame++;
if (pops.get(i).popFrame > 0){
pops.remove(i);
}
}
// Player health bar
if (life == 2){
healthPaint.setColor(Color.YELLOW);
} else if (life == 1) {
healthPaint.setColor(Color.RED);
}
canvas.drawRect(dWidth-200, 30, dWidth-200+60*life, 80, healthPaint);
canvas.drawText("" + points, 20, TEXT_SIZE, textPaint);
handler.postDelayed(runnable, UPDATE_MILLIS);
}
// Player movement through touch input.
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
if (touchY >= playerY){
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN){
oldX = event.getX();
oldPlayerX = playerX;
}
if (action == MotionEvent.ACTION_MOVE){
float shift = oldX - touchX;
float newPlayerX = oldPlayerX - shift;
if (newPlayerX <= 0)
playerX = 0;
else if (newPlayerX >= dWidth - player.getWidth())
playerX = dWidth - player.getWidth();
else
playerX = newPlayerX;
}
}
return true;
}
}