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

2011/8/8

Constructor maddness!!

Robto Robto

2011/8/8

#
Hi, I've been following the tutorial how to add text to the scenario. No problems there, I've added a constructor, filled the img with a string and have that string updated based on action.... But then the maddness starts. How to I add a second label? I've tried it with adding another label in world which works ok, but when I want to update this dynamically, the trouble starts. I fully duplicated the labels code in both the world and actor. This didnt work because Label was already declared in the world (I tried to add another public myActor(Label label) in the actor class. So, I figured the costructor is not reusable and I have to add another constructor. I created another constructor, same code updated declerations but... it still needs to be accessible in the same actor that already has the first label
addObject (new myActor(label), getWidth()/2, getHeight()/2);
Can and how do I make two seperate labels accessible by the same actor? Both labels must be updated by actions that the actor does (will be more in the future). I have a feeling that I can reuse the 1st constructor. Is that so?
davmac davmac

2011/8/9

#
Should there two labels from the beginning, or does it need to be one to begin with, with another one added later?
Robto Robto

2011/8/9

#
Hi davmac, There should be two or more labels from the beginning. They are basically the counters that show the collected resources. Your remark got me thinking btw. It is possible to have two text fields in the same label that are actively updated? If possible seperate from eachother? (which is actually not neccesary as it constantly displays the current value of the int's). That might be the solution. The trouble I have is that in the turorial "showing text" the label is passed to the ball so that each bounce updates the label. I can pass my 1st or 2nd label (I created to seperate label classes) to the ball ok, but I can only pass one label (class) to the "ball" at the time. label to actor because it was
davmac davmac

2011/8/9

#
It looks like there is some text missing from the end of your post? However: You can have a constructor with multiple arguments:
public MyActor(Label label1, Label label2)
{
    // ....
}
Does that help? (By the way, I made "MyActor" begin with a capital letter, because it is a class name and that is the convention in Java, although the rule isn't enforced by the compiler).
Robto Robto

2011/8/9

#
Hi davmac, I tried changing the code with your tip, but somehow I don't get it. Next I looked at your tutorial with the rocket and tried go get two counters in place re-using the class Counter (the original which updates the score with 5 and another which updates the score with 10). That didn't work (the original label is updated by 15 :-)).
public class Space  extends World
{

    private Counter theCounter;
    private Counter theCounter2;
    /**
     * Constructor for objects of class Space.
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Rocket(), 300, 340);
        theCounter = new Counter();
        addObject(theCounter, 40, 340);
        theCounter2 = new Counter();
        addObject(theCounter2, 40, 300);
    }

    public Counter getCounter()
    {
        return theCounter;
    }
public class Counter  extends Actor
{
    private int totalCount = 0;

    public Counter(Counter counter, Counter2 counter2)
    {
        setImage(new GreenfootImage("Score: 0", 20, Color.WHITE, Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("Score: " + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
private void hitAnAsteroid()
    {
        Space spaceWorld = (Space) getWorld();
        Counter counter = spaceWorld.getCounter();
        counter.bumpCount(5);
        
        //Space spaceWorld = (Space) getWorld();
        Counter counter2 = spaceWorld.getCounter();
        counter2.bumpCount(10);
    }
Can I re-use the Class counter to have a second label which is dynamically updated? Say for example a counter that counts the shots? Or do I need to create a new class?
Robto Robto

2011/8/9

#
Lol... by wrting out the code of the different classes in my previous post, it hit me... I had to add the following in Space
public Counter getCounter2()
    {
        return theCounter2;
    }
and the following code in Shot
private void hitAnAsteroid()
    {
        Space spaceWorld = (Space) getWorld();
        Counter counter = spaceWorld.getCounter();
        counter.bumpCount(5);
        
        //Space spaceWorld = (Space) getWorld();
        Counter counter2 = spaceWorld.getCounter2();
        counter2.bumpCount(10);
    }
Off to try to get this working in my own code! Thanks for the help davmac
You need to login to post a reply.