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

2015/12/21

What would be the code to get an object (class) to appear when the world has been changed?

jalaw02 jalaw02

2015/12/21

#
Hi. What would be the code to get an object (class) to appear when the world has been changed? I'm not sure if you have to add it into the world's code or the class' code. If it could be possible that I could learn the code from someone with an explanation as to how it works, that'd be brilliant, as I'd have to do that a number of times! Sorry about this if it's a simple question. I don't know much about programming! Thank you.
danpost danpost

2015/12/21

#
If I understand you correctly, it could be done both ways. That is:
// in old world
World world = new AlternateWorld();
world.addObject(new SomeActor(), 300, 200);

// in new world
public AlternateWorld()
{
    super(600, 400, 1);
    addObject(new SomeActor(), 300, 200);
jalaw02 jalaw02

2015/12/23

#
Thank you! Does just what I want to do. Would there be any way that I could get an object to be replaced by another object within the same world? (For example, after 5 frames a green dot disappears and is replaced with another orange dot in a different class?) Any response would be appreciated! Thanks.
danpost danpost

2015/12/23

#
First, a green dot must be in the world for the 5 frames. By placing the code in the 'act' method of the green dot, you are ensured that the code will not execute unless one is in the world. Next, you need to track the frames; so, you need an int field, call it 'frames' in the class the code will be in and have its value increment in the act method. Finally, when the value of the field after incremented is '5', add the new object at the location the green object is at and have the green object remove itself. See if you can get it to work. If you have issues with the coding, show what you tried and we can help you fix it. NOTE: 5 frames is a very short time (usually about 1/10th of a second). You may want to increase the limiting value to something more appropriate (one second is usually between 50 and 60 frames).
jalaw02 jalaw02

2015/12/23

#
I've had a go at doing that. What I've come up with is this:
    private int frames = 50;
    public void act()
    {
         move();
         click();
         
         if (frames > 0)
            {
                frames --;
                if (frames == 0)
            
                    {
                        frames = 50;
                        getWorld().removeObject(this);
                        addObject(new dot2(), 127, 286);
                    }
             }
    }    
There is a problem (line 15) which 'cannot find symbol - method addObject(dot2,int,int) Sorry, given it my best shot!
danpost danpost

2015/12/23

#
You cannot directly call the World class method 'addObject' from a class that is not a descendant of it. You are placing the code in an Actor subclass, which means you need to get the world to add the object into (just like line 14 does to remove an object from the world).
jalaw02 jalaw02

2015/12/23

#
Hi. Thanks for that, seems to work - almost! When it comes to the transition, I am presented with a box with this:
java.lang.NullPointerException at dot.act(dot.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
When I click on dot.act(dot.java:29), it takes me to line 15 again.
danpost danpost

2015/12/23

#
Yes -- that will happen when your actor is not in a world. The last line (line 14) removed the actor from the world. Switch the two lines so the actor is still in the world when you add the dot.
jalaw02 jalaw02

2015/12/23

#
Brilliant! Problem solved. Thanks!
You need to login to post a reply.