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

2018/8/14

Inheritance of classes

RevoltecX7 RevoltecX7

2018/8/14

#
Hi there, I want ask you guys, how to inheritance the "act" function or what to do when I can't use both in one class(in Interface.class) and also don't want use this long code in every map class). Image: Photo In Interface.class public void act() I have
public void act() 
    {        
        UserInfo myInfo = UserInfo.getMyInfo();
        //save n load
        if(Greenfoot.isKeyDown("i")){
            myInfo.setInt(0,level);
            myInfo.setInt(1,xp);
            myInfo.setInt(2,xpNeed);
            myInfo.setInt(3,hpMax);
            myInfo.setInt(4,mpMax);
            myInfo.setInt(5,coins);
            myInfo.store();
        }
        
        if(Greenfoot.isKeyDown("o")){
            level = myInfo.getInt(0);
            xp = myInfo.getInt(1);
            xpNeed = myInfo.getInt(2);
            hpMax = myInfo.getInt(3);
            mpMax = myInfo.getInt(4);
            coins = myInfo.getInt(5);
            
            adjustLevel(0, true);
            adjustXp(0, true);
            adjustLife(0, true);
            adjustMana(0, true);
            adjustCoins(0, true);
        }
        
        for (Actor a : getObjects(Actor.class))
        {
            if (a.getX() > getWidth()-5){ removeObject(a); return;}
            if (a.getX() < 5){ removeObject(a); return;}
            if (a.getY() > getHeight()-5){ removeObject(a); return;}
            if (a.getY() < 5){ removeObject(a); return;}
        }
    }
And in village/fight nothing. Now what I need is - function:
for (Actor a : getObjects(Actor.class))
        {
            if (a.getX() > getWidth()-5){ removeObject(a); return;}
            if (a.getX() < 5){ removeObject(a); return;}
            if (a.getY() > getHeight()-5){ removeObject(a); return;}
            if (a.getY() < 5){ removeObject(a); return;}
        }
Put to Village and Fight acts but then I can't use save/load from Interface... How to make it workable? (I don't want use save/load in every class of map.) I want be to able save/load(when I will have save n load in Interface.class and also want to make that second code functionable for every map). Is there any way for do that? Thanks
Super_Hippo Super_Hippo

2018/8/14

#
(First, only read/write from/to UserInfo when you need to, not every act cycle (line 3). But even placing it to the if conditions won't be perfect if 'isKeyDown' is the only condition.) You can call the act method of the super class by using 'super.act()'. So the act methods of your Village/Fight could be
public void act()
{
    super.act();
    //rest of act method, only for this class, not for every Interface
}
Is that what you want to do?
RevoltecX7 RevoltecX7

2018/8/15

#
Yea, sorry. I forgot to change it(line 3), I have this problem, and I was try to find, why the hek it doesn't work. And yes is that what I looking for, thank you so much. Now it works perfectly! :)
You need to login to post a reply.