0

I have a sprite represented as a rectangle for manipulating and information, with a texture rendered on top. Here's the code that I have:

int carrotScore;
    boolean onCarrot = true;
    Rectangle carrot;

    carrotScore = 0;

carrot = new Rectangle();
        carrot.height = 100;
        carrot.width = 100;
        carrot.x = 50;
        carrot.y = 50;

if(carrot.overlaps(farmer) && onCarrot!= false){
                onCarrot = false;
                carrotScore += 1;               
            }

if(carrotScore == 2){
                game.setScreen(new Level2Complete(game));
            }

if(onCarrot = true){
                game.batch.draw(carrotTexture, 50, 50, 100, 100);
            }

However when i run it and the sprites overlap the previous graphic still shows and doesn't disapear, whilst it changes the screen to level2Complete, so still counting? Any ideas why is it still counting the carrot and why isn't the graphic disappearing?

1 Answer 1

2

The line

if(onCarrot = true)

Should be

if(onCarrot == true)

You are assigning onCarrot = true instead of checking whether it is true.

5
  • Ok thats an improvement but the graphic still isn't being stopped it's staying there, is it because the grapgic is'nt being changed so it's not refreshing? Sep 29, 2014 at 21:05
  • I am not quite sure how your game works, so it is hard to tell what might be wrong. Are you clearing the screen before you render a new frame? Unless the Level2Complete screen (whatever that is) draws on top of the carrot, the carrot will stay there. Clearing the screen is usually accomplished by filling the screen with a color or a background image or such.
    – Atuos
    Sep 29, 2014 at 21:11
  • I have a tile map drawn first then the carrot and other sprites on top then the level2conplete is drawn over everything as I want to see the stuff behind as well like the tile map etc Sep 29, 2014 at 21:15
  • I can only speculate what might be wrong, as there is not enough code to see what is happening. Check if the game.batch.draw(carrotTexture, 50, 50, 100, 100) method is being called, for example by printing something there.
    – Atuos
    Sep 29, 2014 at 21:23
  • Try to debug your code. Set a breakpoint to the if (carrot.overlaps(farmer) && onCarrot), then go on and look, if this statement is true some time. Then go on an look, what happens if you reach the line if (onCarrot == true). Also make sure, you clear the screen before drawing the next frame!
    – Robert P
    Sep 30, 2014 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.