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

2013/11/2

Passing Character Data through world

Tavi Tavi

2013/11/2

#
Hi Guys ! I have a character with a number of attributes ( Stardamage multiplier, clawDamagemultiplier, level, money, and XP). However, when i go trough a portal into a new world, i lose all of these attributes.. What's the cleanest and fastest way to pass on these values :) so i dont lose them in the second world.. This is the code which is written in each of the 7 worlds.
//Level
        LevelCounter levelcounter = new LevelCounter();
        addObject(levelcounter, 100, 30);
        //XP
        xpbar = new XpBar();
        addObject(xpbar, 395, 27);
        //HP
        HealthBar healthBar= new HealthBar();
        addObject(healthBar, 100, 125);
        //DAMAGE
        StarDamageMultiplier stardamagemultiplier = new StarDamageMultiplier ();
        addObject(stardamagemultiplier, 698, 33);

        ClawDamageMultiplier clawdamagemultiplier = new ClawDamageMultiplier ();
        addObject(clawdamagemultiplier, 698, 78);

        DamageBar damagebar = new DamageBar();
        addObject(damagebar, 700, 125);

        //Mesos
        MesoCounter mesocounter = new MesoCounter();
        addObject(mesocounter, 100, 80);

        //Character
        addCameraFollower(new Rogue(levelcounter, mesocounter, healthBar , xpbar), 0, 0);
I Hope you guys can help !
danpost danpost

2013/11/2

#
You will need to create references (instance fields in your worlds) to all the objects listed above. Once that is done, you can do the following. After creating the new world, transfer the values of the objects across from the old world to the new one:
// from WorldOne, going to WorldTwo
WorldTwo w2 = new World2();
w2.healthBar.setHealth(healthBar.getHealth());
w2.stardamagemultiplier.setDamageMultiplier(stardamagemultiplier.getDamageMultiplier());
// etc.
Greenfoot.setWorld(w2);
Tavi Tavi

2013/11/2

#
The values are created in my Rogue class, not in my world one clas.. does the code still work this way ?
danpost danpost

2013/11/2

#
Yes. But getting them takes a little more work:
Rogue rogue = (Rogue) getObjects(Rogue.class).get(0);
w2.healthBar.setHealth(rogue.getHealthBar().getHealth());
// etc.
Tavi Tavi

2013/11/2

#
Thank you :) , My project however seems to need some reconstruction, is there any way i can send it for review ? I'm getting stuck on multiple issues and i cant seem get throught them. Help will be Greatly appreciated..
Tavi Tavi

2013/11/2

#
Anyone ?
danpost danpost

2013/11/3

#
Work on one issue at a time. It is too easy to get confused when you try to work on more than one thing at a time. Compile often to prevent compounding errors. For now, it might be best to reduce it to a working state and build on it from there.
You need to login to post a reply.