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

2017/5/14

Passing variables between worlds

Brookili Brookili

2017/5/14

#
So I'm making a project where the user can choose between four characters to play from a menu screen. If I want their selection to create a player actor in the first world that is of the character they chose, and for that character to stay the same as they move between levels, how would I do that? I have a separate object storing information such as the character type, but whenever I try to move between levels, I just end up getting a NullPointerException. The object storing info:
public class Information
{
    private CheckL1 KL1;
    public Hero hero;
    public int heroName;
    public Information()
    {
        heroName = KL1.check();
    }
    /**
     * Act - do whatever the Information wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public int getHero()
    {
        return heroName;
    }
The method of player character movement between worlds:
    private void progress()
    {
        if(isTouching(CheckL1.class))
        {
            if(info.getHero() == 0)
            Greenfoot.setWorld(new L2(0));
            if(info.getHero() == 1)
            Greenfoot.setWorld(new L2(1));            
            if(info.getHero() == 2)
            Greenfoot.setWorld(new L2(2));
            if(info.getHero() == 3)
            Greenfoot.setWorld(new L2(3));
        }
}
The object used to allow movement between worlds:
public class CheckL1 extends Actor
{
    public int ero;
    
    public CheckL1(int parEro)
    {
        ero = parEro;
    }
    
    /**
     * Act - do whatever the CheckL1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    public int check()
    {
        return ero;
    }
}
The switch statement for the first world that makes the desired character based on input:
        switch(hero)
        {
            case 0: addObject(fighter, 25, 20); break;
            case 1: addObject(mage, 25, 20); break;
            case 2: addObject(soldier, 25, 20); break;
            case 3: addObject(thief, 25, 20); break;
        }
Sorry if this is a lot, but any and all help is appreciated!
danpost danpost

2017/5/15

#
Are all four actors (fighter, mage, soldier and thief) from different subclasses of the Hero class?
Brookili Brookili

2017/5/15

#
Not exactly, they're all different versions of the Hero class. It's based off of the Hero class's constructor, where its four classes are from a parameter.
    public Hero(String parName)
    {
        name = parName;
        setImage(name + "0.png");
        health = 100;
        movespeed = 3;
        sLimit = 25;
        attackType = 0;
        aFrames = 5;
        ad = 30;
        if(name == "Thief")
        {
            movespeed = 4;
            health = 70;
            sLimit = 20;
            ad = 25;
            prog = 3;
        }
        if(name == "Fighter")
        {
            movespeed = 2;
            health = 150;
            sLimit = 30;
            ad = 40;
            prog = 0;
        }
        if(name == "Soldier")
        {
            prog = 2;
        }
        if(name == "Mage")
        {
            movespeed = 3;
            health = 85;
            sLimit = 23;
            attackType = 1;
            aFrames = 20;
            ad = 20;
            prog = 2;
        }
    }
danpost danpost

2017/5/15

#
danpost wrote...
Are all four actors (fighter, mage, soldier and thief) from different subclasses of the Hero class?
That is fine. Now, it looks like you are creating all 4 Hero objects and using the switch to place to proper one in the world. Also, it looks like the choice of hero is held by an int value and the Hero objects are created with a String object. Then the Hero objects use the String value (incorrectly, I might add) to set the fields appropriately for each type hero. I said incorrectly because you are comparing String objects (are they the same object)-- not String content (are the character sequences of the two objects the same). Use the equals method to compare content of String objects. For example:
if ("Mage".equals(name))
Put values known not to be null first to avoid a NullPointerException. A literal value is never null; but a referenced value could be. I was thinking that if you passed the int value in the Hero constructor, you can use a switch to set the state as well as set the name in that constructor. I am not quite sure how the CheckL1 objects are used; but it would seem that having them check for the intersecting object would be better than the other way around. Then, it can create the Hero object directly and set the Information class 'Hero hero' field to it before setting the new world active(make that field 'static' so the Hero object can be maintained throughout the different levels). The worlds can then add the 'Information.hero' object into themselves.
You need to login to post a reply.