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

#
// players code
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 MyWorld w;
    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 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

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

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

    public void switchWorld()
    {   
       
        if(getWorld()instanceof MyWorld)
        {
           
            if(sb.getScore()>=10100) 
            {
                Greenfoot.setWorld(new stage2());
                
            }
        }//end if
        if(getWorld()instanceof stage2)
        {
            
            if( sb.getScore() >=100100)
            {
                Greenfoot.setWorld(new stage3());
                
            }
        }//end if

    }
}
// scoreBoard code where i want to get the code from
import greenfoot.*;  

public class ScoreBoard extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    FontImage pic;
    int score=0;
    int finalScore =0;

    // CONSTRUCTOR   // runs one time only
    // -----------
    public ScoreBoard()    
    {
        pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
        setImage(pic);
    } // end constructor

    public void increaseScore(int newPts)
    {//increases score 
        score=score+ newPts;
        pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
        setImage(pic);
    }   //end increase score
    
    public int getScore()
    {
        finalScore=finalScore +score;
        return finalScore;
    }
   
    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {

    } // end act

} // end class
roonie01 roonie01

2018/6/12

#
my player has a nullpointer exception when i run this
danpost danpost

2018/6/12

#
roonie01 wrote...
my player has a nullpointer exception when i run this
Post the entire error output please.
roonie01 roonie01

2018/6/12

#
java.lang.NullPointerException at Survivor.switchWorld(Survivor.java:115) at Survivor.act(Survivor.java:44) 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)
roonie01 roonie01

2018/6/12

#
thats it
danpost danpost

2018/6/12

#
roonie01 wrote...
<< Error Omitted >>
I believe that this error only happens when the score is still zero. The score is fine when tagging a Zombie object, however. This is because that is when you reach out to the MyWorld object to get the ScoreBoard object (which is not the best time to do so). The best time to get it is when the Survivor object is added to the world:
/** in Survivor class */
protected void addedToWorld(World world)
{
    w = (MyWorld)world;
    sb = w.getScoreboard();
}
The action code should then only refer to w and sb for those objects.
roonie01 roonie01

2018/6/12

#
that works but now when my actor switches worlds and add the actor the whole program freezes
roonie01 roonie01

2018/6/12

#
can i add my other worlds to the parameters
roonie01 roonie01

2018/6/13

#
this is what my survivor class looks like now
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 MyWorld w;
    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 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)
    {
        w = (MyWorld)world;

        sb = w.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,getX(),getY());
            addedToWorld(w2);
        }

        if( sb.getScore() >=100100)
        {
            Greenfoot.setWorld(new stage3());

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

        }

    }
}
danpost danpost

2018/6/13

#
roonie01 wrote...
can i add my other worlds to the parameters
Probably not what you want to do. Remove lines 124 and 132 to start with. The addedToWorld method is called automatically by greenfoot (you do not have to add a call to it in your code). Change the addedToWorld method to this:
protected void addedToWorld(World world)
{
    if (sb == null) sb = ((MyWorld)world).getScoreboard(); 
}
and remove line 12.
roonie01 roonie01

2018/6/13

#
ok and now my actor wont add to my other world and my other world is in runnable
roonie01 roonie01

2018/6/13

#
this is the code for 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 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,getX(),getY()); 
            

        }

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

            Greenfoot.setWorld(new stage3());

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

        }
    }
}
danpost danpost

2018/6/14

#
roonie01 wrote...
ok and now my actor wont add to my other world and my other world is in runnable
What do you mean by "is in runnable"? Also, show relevant code (class code where you try to add "my actor" in "other world").
roonie01 roonie01

2018/6/14

#
I meant unable to run and heres my world code
import greenfoot.*;  


public class stage2 extends World
{
    // GLOBAL DECLARATIONS
    // ===================
    int actCount=0;
    int randY = Greenfoot.getRandomNumber(600);
    ScoreBoard sb;
    // CONSTRUCTOR  (populate world here)
    // ===========
    public stage2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        sb= new ScoreBoard();
        addObject(sb,66,550);
        
        addObject(new Survivor(),400,300);
        
         
        
    } // end constructor
    
    
    // ACT METHOD  (often left empty in World subclass)
    // ==========
    public void act()
    {
       randY= Greenfoot.getRandomNumber(600);
        actCount++;
        showText(""+actCount,50,500);
        
        //if the actCount is equal to 100 act cycles then add three Zombies  
        // also resets the actCounter 
        //============================================
        if(actCount==100)
        {
          
            actCount=0;
            // tells world to only allow 25 total aliens or forerunner ships
            //==============================================================
           if (getObjects(null).size()<35)
           {
                addObject(new Zombie(),0,randY);
                addObject(new Zombie(),0,randY);
                addObject(new Zombie(),0,randY);
               
              
            }

        }
    }
    public ScoreBoard getScoreboard()
    {
        return sb;
    }
    
} // end class
 
danpost danpost

2018/6/14

#
Please post (copy/paste) the entire error output for review.
There are more replies on the next page.
1
2
3
4