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

2019/10/14

Spawn players based on variable from another class

Dormey Dormey

2019/10/14

#
Here is the P1 button
public class P1but extends Actor{
    private GreenfootImage P1img;
    public InfoStore y;
    public P1but(){
        P1img = new GreenfootImage("P1Button.png");
        P1img.scale(240, 80);
        setImage(P1img);    
    }
    /**
     * Act - do whatever the P1but wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){
        if(Greenfoot.mouseClicked(this)){
            Level1 t = new Level1();
            Greenfoot.setWorld(t);
            
            InfoStore.setPnum(1);
        }
    }    
}
Here is the P2 button
public class P2but extends Actor{
    private GreenfootImage P2img;
    public InfoStore y;
    public P2but(){
        P2img = new GreenfootImage("P2Button.png");
        P2img.scale(240,80);
        setImage(P2img);
    } 
    /**
     * Act - do whatever the P2but wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){
        if(Greenfoot.mouseClicked(this)){
            Level1 e = new Level1();
            Greenfoot.setWorld(e);
            
            InfoStore.setPnum(2);
        }
    }
}
Here is the menu
public class Menu2 extends World{
    private GreenfootImage BG;
    /**
     * Constructor for objects of class Menu2.
     * 
     */
    public Menu2(){    
        super(960, 720, 1);
        BG = new GreenfootImage("MenuFile.png");
        BG.scale(getWidth(), getHeight());
        setBackground(BG);
        addObject(new P1but(),480,400);
        addObject(new P2but(),480, 525);
    }
}
Here is the InfoStore
public class InfoStore{
    private static int Pnum;
    private static int LvlNum;
    /**
      * Constructor for objects of class InfoStore
     */
    public InfoStore(){
        Pnum = 0;
        LvlNum = 0;
    }
    public static int getPnum(){
        return Pnum;
     }
    public static void setPnum(int newPnum){
        Pnum = newPnum;
    }
    public static int getLvlNum(){
        return LvlNum; 
     }
    public void setLvlNum(int newLvlNum){
        LvlNum = newLvlNum;  
    }
    public void act(){
        
     }
}


Here is the level 1 code
public class Level1 extends LevelBase
{
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1(){
        setBG();
        spawnObj();
        spawnPlyrs();
    }
    public void act()
    {
        
    }
}
And here is the level base code (Where level1 gets the methods)
public class LevelBase extends World
{   
    protected GreenfootImage img;
    /**
     * Constructor for objects of class LevelBase.
     * 
     */
    public LevelBase()
    {    
        super(960, 720, 1);
    }
    public void setBG(){
        img = new GreenfootImage("LevelBG1.png");
        img.scale(getWidth(), getHeight());
        setBackground(img);
    }
    public void spawnPlyrs(){
        if(InfoStore.getPnum()==1){
            addObject(new Player1(), 107, 625);
            return;
        }
        if(InfoStore.getPnum()==2){
            addObject(new Player1(),100, 625);
            addObject(new Player2(),110, 625);
            
        }
    }
    public void spawnObj(){
        addObject(new SpawnArea(),107, 625); 
    }
}
Please help thanks!
Dormey Dormey

2019/10/14

#
So whats happening is that Im trying to spawn in the correct amount of players into the level based on a Pnum integer in the InfoStore class. The Pnum is set by the P1 or P2 buttons. The problem is that it doesn't always work and it is buggy and just really doesn't work, but I'm not getting any errors or anything. Please help me solve this thanks!
Dormey Dormey

2019/10/14

#
Oh wait ignore the public InfoStore y in the buttons. forgot to remove that. Also, Im pretty sure the problem is with something in LevelBase
danpost danpost

2019/10/14

#
Would it not be wise to set the Pnum value in InfoStore before creating the worlds in the button classes?
Dormey Dormey

2019/10/14

#
Sorry but I don't really understand what you mean by that danpost
danpost danpost

2019/10/14

#
Dormey wrote...
Sorry but I don't really understand what you mean by that danpost
In your P#but classes, you create the worlds on line 15, then ( later ??? ) on line 18, you set the number of players. The Level1 world constructor is using the value that is set to InfoStore.Pnum before you have it assigned its new (wanted) value.
Dormey Dormey

2019/10/14

#
Ok it works now, thanks for your help
You need to login to post a reply.