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/18

#
this is the entire error output java.lang.NullPointerException at Survivor.switchWorld(Survivor.java:130) at Survivor.act(Survivor.java:45) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2018/6/18

#
roonie01 wrote...
this is the entire error output << Error Omitted >>
Okay, this line:
at Survivor.switchWorld(Survivor.java:130)
which is the first line referencing a class (not Actor or World) in your project, says the error occurred in the switchWorld method at line 130 in your Survivor class. Your last posting of that class is obviously not how the class looks anymore (line 130 is not even in the switchWorld method), so -- what is currently line 130 in your Survivor class now (if the error still occurs on that line)?
roonie01 roonie01

2018/6/18

#
here is the newest code for the entire survivor class
import greenfoot.*;
public class Survivor extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    int speed = 6;
    int fn=0;  // fn - frame number: TRACK ARRAY INDEX FOR CURRENT PICTURE 
    int dn =0; //dn is direction number first index in 2nd array
    int actCount =0;
    int score=0;
    static ScoreBoard sb;
    
    static stage2 w2;
    static stage3 w3;
    GreenfootImage[][] images = new GreenfootImage[2][3];

    // CONSTRUCTOR   // runs one time only 
    // -----------
    public Survivor()    
    {         
        // load images into an array of pics
        // ---------------------------------
        
        for(int i=0; i<3; i++)
        {
            images[0][i] = new GreenfootImage("playerRight"+i+".png");
        } // end for
        for(int i=0; i<3; i++)
        {
            images[1][i] = new GreenfootImage("playerLeft"+i+".png");
        } // end for
        // set the current image
        // ----------------------
        setImage(images[0][0]);  

    } // end constructor

    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {
        setLocation(getX(),getY());
        checkForDirectionalControll();
        isTagged();
        switchWorld();

    } // end act

    public void checkForDirectionalControll()
    {

        actCount++;
        // keyboard control
        // -----------------
        if(Greenfoot.isKeyDown("right")&&actCount>=10)
        {
            setLocation(getX()+speed, getY());
            dn = 0; // dn 0 for right 1 for left
            switchImage();
            actCount =0;
        } // end if
        if(Greenfoot.isKeyDown("left")&&actCount>=10) 
        {
            setLocation(getX()-speed, getY());

            dn = 1;
            switchImage();
            actCount =0;
        } // end if
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-speed);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+speed);
        }
        //end checkForDirectionalControll
    } 
    public void CheckForShot()
    {
        if(Greenfoot.isKeyDown("space")&&actCount>=200)
        {
            getWorld().addObject(new Bullet(),getX(),getY());
        }
    }

    public void switchImage()
    {
        // increase frame number, then make sure we haven't gone off the edge of the array
        // -------------------------------------------------------------------------------
        fn++;
        if(fn>=3)
        {

            fn=0;
        } // end if

        // set the image of the actor to the new pic
        // ------------------------------------------
        setImage(images[dn][fn]);   

    } // end switchImage
    protected void addedToWorld(World world)
    {
        if (sb == null) sb = ((MyWorld)world).getScoreboard(); 
    }

    public void isTagged()
    {
        if(isTouching(Zombie.class))
        {
            removeTouching(Zombie.class);

            // w = (MyWorld)getWorld();  
            // sb = w.getScoreboard();
            sb.increaseScore(1000);

        }
    }

    public void switchWorld()
    {  
        

        if(sb.getScore()>=10100) 
        { 
            
            Greenfoot.setWorld(new stage2());
            w2.addObject(this,400,300); 
            Greenfoot.start();
            

        }

        if( sb.getScore() >=100100)
        {

            Greenfoot.setWorld(new stage3());

            w3.addObject(this,getX(),getY());
            

        }
    }
}
roonie01 roonie01

2018/6/18

#
the current line 130 is a line that is supposed to add the current actor to stage 2 when the score is at 10100 which it dose and then when i press run it runs for one act cycle and stops which it is not supposed to do
roonie01 roonie01

2018/6/18

#
okay so i noticed that my switch world method was referenced in my act method
danpost danpost

2018/6/18

#
roonie01 wrote...
the current line 130 is a line that is supposed to add the current actor to stage 2 when the score is at 10100 which it dose and then when i press run it runs for one act cycle and stops which it is not supposed to do
Where is w2 assigned a World object of type stage2?
roonie01 roonie01

2018/6/18

#
w2 is assigned to stage 2 in the global variables section and i think the reason for it screwing up is that my switch world method is called in act where should it be called
danpost danpost

2018/6/18

#
roonie01 wrote...
w2 is assigned to stage 2 in the global variables section
What line?
roonie01 roonie01

2018/6/18

#
line 13
danpost danpost

2018/6/18

#
roonie01 wrote...
line 13
No. You only declare a field named w2 of type stage2 there. Its default value is null as there is no assigning (using "=") of any stage2 object to that field.
danpost danpost

2018/6/18

#
roonie01 wrote...
okay so i noticed that my switch world method was referenced in my act method
You will probably need another condition to switch worlds or once you reach 10100, you will constantly be going to a new stage2 world. Maybe something along this line for each check:
if (getWorld() instanceof stage1 && sb.getScore() >= 10100)
roonie01 roonie01

2018/6/19

#
so I replaced my conditions with the suggested one and now my actor cannot be added to the new world but the scenario will run if you need I could post the scenario
roonie01 roonie01

2018/6/19

#
I put it up on the web site
roonie01 roonie01

2018/6/19

#
where should I call my switch world method
danpost danpost

2018/6/19

#
roonie01 wrote...
I put it up on the web site
It does not help unless you include (publish) the source code. Re-upload it, making sure you check the appropriate check box.
There are more replies on the next page.
1
2
3
4