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

2014/11/24

Scrolling Super World issues with main actor

1
2
Dillybar Dillybar

2014/11/24

#
In my game, I am attempting to remove the main actor and add a new main actor in its place (Similar to what happens in mario when he shrinks). However, when I do so, the new main actor is placed back on the "starting" screen instead of where the old main actor was. Hoping you can help, thanks.
danpost danpost

2014/11/24

#
Ok, I am getting to this one now (I did not forget about you). Give me some time to review and get my thoughts together.
danpost danpost

2014/11/24

#
I believe you can do something like the following to change the main actor without resetting its location:
// in class of current main actor
SWorld world = (SWorld)getWorld();
Actor futureMain = new Replacement(); // create replacement actor
world.addObject(futureMain, getX(), getY()); // set in world at location
world.mainActor = futureMain; // set as new main actor
world.removeObject(this); // remove actor that has been replaced
Let me know whether it works or not (I think it should).
Dillybar Dillybar

2014/11/25

#
Sorry for my delayed reply. Sadly, this did not work. When I tried to implement the code, the new actor appeared on the left and right side of the screen while the screen flashes. However, the other actors on the screen still operate normally. Here is the code that I put in my project in case it was an error on my end (I called it when the main actor intersected with an enemy):
SWorld world = (SWorld)getWorld();  
Actor futureMain = new Heart(); // create replacement actor  
world.addObject(futureMain, getX(), getY()); // set in world at location  
world.mainActor = futureMain; // set as new main actor  
world.removeObject(this); // remove actor that has been replaced  
danpost danpost

2014/11/25

#
That is strange -- because I did test it out and it seemed to work for me. Does it do the same thing with the flashing screen if you do not change the actor? The flashing sounds like a scrolling issue.
Dillybar Dillybar

2014/11/25

#
No, the scrolling works perfectly until I try to remove the main actor.. I will try it on a different computer tomorrow.
Dillybar Dillybar

2014/11/25

#
Here is the error message from my code: java.lang.NullPointerException at MarioG.removeMario(MarioG.java:91) at MarioG.act(MarioG.java:39) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Dillybar Dillybar

2014/11/25

#
And here is the full if statement where I call the code, perhaps something here conflicts. Thanks again for all of the help.
        if (gotHit == true)
        {
            SWorld world = (SWorld)getWorld();    
            Actor futureMain = new Heart(); // create replacement actor    
            world.addObject(futureMain, getX(), getY()); // set in world at location    
            world.mainActor = futureMain; // set as new main actor    
            world.removeObject(this); // remove actor that has been replaced
            MyWorld myWorld = (MyWorld)getWorld();
            myWorld.decreaseLives();
            myWorld.countLives();
            gotHit  = false;
            invincibilityDelayCount = 0;
        }
danpost danpost

2014/11/25

#
You are using 'getWorld' in line 8 after you remove 'this' from the world; so, it returns 'null' -- the actor is no longer in a world to get. This, however, can be easily fixed by changing line 8 to the following:
MyWorld myWorld = (MyWorld)world;
'world' is the same world -- it was just not in a field that reflects it as being of the sub-type 'MyWorld'.
Dillybar Dillybar

2014/11/25

#
Ok, so the error is gone. However, the world still flashes back and forth with a heart on each side of the screen. Any idea on how to fix this?
danpost danpost

2014/11/25

#
At this point, it would seem best to upload the scenario while including the source so that I can download it an take a more in-depth look at what is going on. You can always log in on the site, go to the scenario and delete it (if you wish) once I have obtained it.
Dillybar Dillybar

2014/11/25

#
Okay I will! Thanks again
Dillybar Dillybar

2014/11/25

#
Here is a link: http://www.greenfoot.org/scenarios/12605
danpost danpost

2014/11/25

#
Ok, I have it. I will take a look. However, I will not be able to respond for about 6 or so hours.
danpost danpost

2014/11/25

#
I think I know the problem and am looking into a simple solution.
There are more replies on the next page.
1
2