This site requires JavaScript, please enable it in your browser!
Greenfoot back
DOGGY
DOGGY wrote ...

2012/11/4

Display winner image problem!

DOGGY DOGGY

2012/11/4

#
Hello everybody, Im almost done creating my scenario only im struggling with the last part of it, the winner image. I want a image to pop up after one of the two players reached 5 points first. I already started writing this code a little bit but i cant complete it myself. This is what i have so far: public void endGame) { Counter counter1 = (Counter) getWorld().getObjects(Counter1.class).get(0); Counter counter2 = (Counter) getWorld().getObjects(Counter2.class).get(0); if (counter1.getValue() >= 5) { Greenfoot.stop(); // code when player 1 reached 5 first } if (counter2.getValue() >= 5) { Greenfoot.stop(); // code when player 2 reached 5 first } -------------------------------------------------------------------------------------------------------- As u can see i already did a part of it. This is the way i want it to be, so i only need a code for an image to pop up when one of the players reached 5. I want greenfoot to load the image and not make another class with the name winner image. Can somebody help me with this last code? thank you
SPower SPower

2012/11/4

#
Let's say the image you want to display is called "player1Win.png". Well, you've got that part, but now you need to display it. Create a subclass of Actor, and call it ImageActor for easy use. Replace it's code by this:
import greenfoot.*;

public class ImageActor extends Actor
{
     public ImageActor(String fileName)
     {
         setImage(fileName);
     }
}
Now, do this when you want to display the image:
ImageActor a = new ImageActor("player1Win.png");
addObject(a, getWidth() /2, getHeight() /2);
you can image what to do when player 2 wins :) I hope this was helpful. EDIT: next time you want to insert code, click the 'code' button underneath the text field and enter your code there.
DOGGY DOGGY

2012/11/4

#
Thankyou for your response but i rather not want to create a new class. This because i make this for a schoolproject and i already handed in the documentation. So i cant change or add any classes, but as you can see in my code i already got a kind of code for the action. I just need a code which will be called on when the score is >= 5. And this code need to be a code in which an image gets called on the screen, just in your code an image is called on. I hope you understand me
SPower SPower

2012/11/4

#
Well, without new classes, the only thing you can do is this:
GreenfootImage img = new GreenfootImage("player1Win.png");
getBackground().drawImage(img,0,0);
But all the Actors will still overlay the image.
SPower SPower

2012/11/4

#
Or you can add the class at the bottom of the file, like this:
import greenfoot.*;

public class <Name> extends <Something>
{
    // your stuff here
}
private class ImageActor extends Actor
{
    // given stuff here
}
DOGGY DOGGY

2012/11/4

#
I think i will use your first recommended option. but im trying to compile it but it doesnt work. i created a new class with the name ImageActor but when i try to compile the second part a error shows it says: cannot find symbol -- method getWidth()
public void endGame()
    {
        Counter counter1 = (Counter) getWorld().getObjects(Counter1.class).get(0);
        Counter counter2 = (Counter) getWorld().getObjects(Counter2.class).get(0);
        if (counter1.getValue() >= 5) {
            ImageActor a = new ImageActor("carlos.png");  
            addObject(a, getWidth() /2, getHeight() /2);  
            Greenfoot.stop();
        }
        if (counter2.getValue() >= 5) {

            Greenfoot.stop();
            // code when player 2 reached 5 first
        }
I only wrote the code for counter1. The name carlos.png is the name of the picture i want to display can you tell what im doing wrong?
SPower SPower

2012/11/4

#
O, I'm sorry, I thought this code was in a subclass of world ;) Replace this:
addObject(a, getWidth() /2, getHeight() /2);
by this:
World w = getWorld();
w.addObject(a, w.getWidth() /2, w.getHeight() /2);
DOGGY DOGGY

2012/11/4

#
Thank you very much, i got it working now!
DOGGY DOGGY

2012/11/4

#
Since you been helping me, i was wondering if you could maybe help me with something else. As you know the max of points is 5 in my game. My ball needs to come back in the game till somebody have reached 5. I want to do this with a loop because using a loop is a must in my project.
   public void reset()
    {
        Bal oldBal = (Bal) getWorld().getObjects(Bal.class).get(0);
        oldBal.setLocation(438, 271);
    }
This code resets the bal once it is scored by one of the players. The new bal gets set on the location of the y and x Do you have a suggestion?
SPower SPower

2012/11/4

#
Well, a loop is certainly not in the right place here. A loop is meant to do thing multiple times, not something once :)
You need to login to post a reply.