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

2018/6/12

how can i get my score from another actor

1
2
3
4
roonie01 roonie01

2018/6/19

#
I re-uploaded
danpost danpost

2018/6/19

#
roonie01 wrote...
I re-uploaded
Okay, will check it out.
danpost danpost

2018/6/19

#
This is something you still need to take care of. I find that you are keeping multiple fields for values (score 3x) and objects (score board 4x). This is poor programming. Things would be a whole lot easier with one field per value. You have a score field in your ScoreBoard class, in your MyWorld class and in your Survivor class -- frankly ridiculous. Having just the one in your ScoreBoard class is sufficient . As far as the ScoreBoard fields, you can keep the one in the MyWorld class and make it a static field. Make the getScoreBoard method static also:
public static ScoreBoard getScoreboard()
Remove those score board fields from the other worlds and in the Survivor class. You can use MyWorld.getScoreBoard() to access the scoreboard object from anywhere in your project. Remove the Greenfoot.start() call in your switchWorlds method. You also have some extra fields that are better off being local variables -- namely int randY, stage2 w2 and stage3 w3 Also, you can help yourself by getting a reference to the world earlier in the Bullet class isOnTop method and using that reference throughout (except when checking if the bullet is still in the world).
roonie01 roonie01

2018/6/19

#
what do you mean by local fields like use them where i need them?
danpost danpost

2018/6/19

#
roonie01 wrote...
what do you mean by local fields like use them where i need them?
Using proper terminology, local variables -- not fields. A field is a variable that is declared outside any method, where a local variable is one that is declared within a method (hence, it is "local" to that method; its scope is limited to the current execution of the method).
roonie01 roonie01

2018/6/21

#
okay I managed to get my stage to switch and run but now my survivor wont add to the world I am updating the one available right now
danpost danpost

2018/6/21

#
roonie01 wrote...
okay I managed to get my stage to switch and run but now my survivor wont add to the world I am updating the one available right now
Show code (switchWorld method).
roonie01 roonie01

2018/6/21

#
here is the code
public void switchWorld()
    {  
        ScoreBoard sb;
        sb = getWorld().getObjects(ScoreBoard.class).get(0);
         
        if (sb.getScore() >= 10100) 
        { 
            // check if the actor's current world is MyWorld then check if the scoreboards 
            // score is at 10100 if so set the world to stage 2 and add the survivor actor 
           
            Greenfoot.setWorld(new stage2(sb)); 
            
            getWorld().addObject(this,400,300); 
           
            

        }//end if 

       if (sb.getScore() >= 100100)
        {
             // check if the actor's current world is stge 2 then check if the scoreboards 
            // score is at 100100 if so set the world to stage 3 and add the survivor actor 
            
            Greenfoot.setWorld(new stage3(sb));
            
            getWorld().addObject(this,getX(),getY());   
            

        }
    }
danpost danpost

2018/6/21

#
The getWorld method returns the world the actor is currently in. So, lines 13 and 26 attempt to add the actor into the world it is already in -- not into the world that is about to be active. You need a reference to that new world (using a local variable) to add the actor to it. For example (for lines 11 and 13):
World newWorld = new stage2(sb);
Greenfoot.setWorld(newWorld);
newWorld.addObject(this, 400, 300);
Line 1 declares a local variable to hold a World object named newWorld, creates a new stage2 World object and assigns it to the field; line 2 sets that new world to be the new active world (which will be active once the current act step completes); and line 3 adds this actor to that new world.
danpost danpost

2018/6/21

#
Why are you still passing the scoreboard from world to world? If you did what I suggested above, you do not need to do so:
danpost wrote...
keep the one in the MyWorld class and make it a static field. Make the getScoreBoard method static also:
public static ScoreBoard getScoreboard()
Remove those score board fields from the other worlds and in the Survivor class. You can use MyWorld.getScoreBoard() to access the scoreboard object from anywhere in your project.
roonie01 roonie01

2018/6/21

#
I re uploaded the newest copy could you check it out
roonie01 roonie01

2018/6/21

#
your code made my stage2 world glitch entirely like my scoreboard blinks on and off now and my character is added ,deleted and then added again to stage 2
World newWorld = new stage2(sb);
Greenfoot.setWorld(newWorld);
newWorld.addObject(this, 400, 300);
roonie01 roonie01

2018/6/21

#
i am getting this error Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423) at javafx.stage.Window.setShowing(Window.java:921) at javafx.stage.Window.hide(Window.java:947) at greenfoot.gui.GreenfootInspectorManager.removeAllInspectors(GreenfootInspectorManager.java:148) at greenfoot.gui.GreenfootFrame.worldRemoved(GreenfootFrame.java:1315) at greenfoot.core.WorldHandler.fireWorldRemovedEvent(WorldHandler.java:866) at greenfoot.core.WorldHandler.lambda$discardWorld$0(WorldHandler.java:591) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
danpost danpost

2018/6/21

#
roonie01 wrote...
here is the code << Code Omitted >>
Your if statements need additional conditions. For example, line 6 should be:
if (getWorld() instanceof MyWorld && MyWorld.getScoreboard().getScore() >= 10100)
and something similar for line 19. Also, notice how the scoreboard is referenced. It should be similar throughout the class. You will need to make your getScoreboard method in the MyWorld class static still:
public static ScoreBoard getScoreboard()
roonie01 roonie01

2018/6/21

#
why does it need to be static I set my stage 2 constructor to
 public stage2(ScoreBoard sb)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        this.sb=sb;
        addObject(sb,66,550);
       addObject(new Survivor(),400,300);

        
    } // end constructor
There are more replies on the next page.
1
2
3
4