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

2013/6/3

Removing intro label from world

Fadel Fadel

2013/6/3

#
Hi in my main world while the game isnt running I display an intro text with games name, instructions and etc. When I run the game I want to remove that intro. Cant get it working
public MainWorld()
    {
        super(800, 400, 1);

        Intro intro = new Intro();
        addObject(intro, getWidth()/2, getHeight()/2);
    }
public void act()
    {
       removeObject(intro);
    }
...
because as soon as you start the world, it removes the intro, and then intro is no longer in the world. What you need to do is something like this:
private boolean addedIntro = false;
...
public void act()
{
    if (/*A variable that determines the time intro has been in the world.*/ && !addedIntro)
    {
        removeObject(intro);
        addedIntro = true;
    }
}
Hoped that helped
Fadel Fadel

2013/6/3

#
No, when I start the game I want to remove it , but it stays...
That's weird, I don't think it should be doing that. Ok, this is going to sound stupid, but have you pressed the "Play" button at the at the bottom of the Greenfoot program?
Fadel Fadel

2013/6/3

#
Yes, of course. I also think it should work, but somehow it doesnt....
OH, Figured it out. You didn't make intro an instance variable. It's only declared in the constructor. You need to declare it outside any methods or constructors in order for it to delete. I wonder why it's not giving you an error for not finding "intro" on line 10.
Fadel Fadel

2013/6/4

#
I have it declared.
Initialized* That's what I meant to say. But do you have it outside the constructor?
It should look like this:
private Intro = new Intro();
...
//Your Constructor
public MainWorld()
{
...
}
...
//Other methods
Fadel Fadel

2013/6/4

#
Got it working. In the main world in act class I immediately change world and in that subclassed world I didnt delete that intro so it stayed... :)
Alright, as long as it's working.
danpost danpost

2013/6/4

#
You could just have easily used this in the act method:
removeObjects(getObjects(Intro.class));
or in 'public void started()'.
Ah, That makes sense, and is a lot easier. danpost, you always suggest the best ways to accomplish something.
Fadel Fadel

2013/6/4

#
Wow , thanks .
You need to login to post a reply.