mardi 21 mars 2017

Boolean not changing as expected

So this is the class for the game I'm making that generates obstacles, each time an obstacle goes off screen it removes the obstacle and adds to the score. This works fine, however the Boolean (plusScore) doesn't change to true when expected.

public void update() {
            //
            if(obstacles.get(obstacles.size() - 1).getRectangle().top >= Constants.SCREEN_HEIGHT) {
                int xStart = (int)(Math.random()*(Constants.SCREEN_WIDTH - playerGap));
                obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
                obstacles.remove(obstacles.size() - 1);
                score += 10;
                plusScore = true; //Problem here
                tempTime = (int)System.currentTimeMillis();
            }
            if (tempTime + 5 <= System.currentTimeMillis()) {
                plusScore = false; //Unsure if working as relying on above
            }
        }

This (below) is where I'm calling for the Boolean, I included the first part of the method as I wasn't sure if there could be possible conflict.

public void draw(Canvas canvas) {
   for(Obstacle ob : obstacles)
       ob.draw(canvas);
   Paint paint = new Paint();
   paint.setTextSize(100);
   paint.setColor(Color.BLACK);
   canvas.drawText(" " + score, 50, 50 + paint.descent() - paint.ascent(), paint);

   if(plusScore) { //This is what the Boolean should effect
       Paint paint2 = new Paint();
       paint2.setTextSize(100);
       paint2.setColor(Color.GREEN);
       drawPlusScore(canvas, paint2, "+10!");
   }
}

[Just to clarify the draw method works fine when there is no if statement.] If the issue is obvious I apologise, I'm following a tutorial for the first time and decided to try something for myself.

Aucun commentaire:

Enregistrer un commentaire