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

2016/2/2

Class and Object

Vellyxenya Vellyxenya

2016/2/2

#
Hi, I'm trying to create a little method that create an object only by passing the name of the class to its parameter:
public void newObject(Class classe)
    {
        classe cls = new classe();
    }
However, the syntax is incorrect, could you please help me writing it correctly, I tried some alternatives but none work...
danpost danpost

2016/2/2

#
I know what you are trying to do -- and it may be possible. However, if you already know the class you are creating the object from, just create the object. In other words, I do not see any use in creating a method that you need to call instead of directly creating the object needed. If this does not make sense, maybe you should explain why you are trying to work things this way and what you are trying to accomplish by adding the method.
Vellyxenya Vellyxenya

2016/2/2

#
Here are some methods (as you can see, they are all quite the same):
public void createBoss2()
    {
        if(Level == 2 && bossCounter == 600 && bossCreated == false)
        {
            Boss2 boss2 = new Boss2();
            addObject(boss2, getWidth(), getHeight()/2);
            bossCreated = true;
            informate("Let's test your ability to dodge, will you be able to survive these? ");
        }
    }

    public void createBoss3()
    {
        if(Level == 3 && bossCounter == 800 && bossCreated == false)
        {
            Boss3 boss3 = new Boss3();
            addObject(boss3, getWidth(), getHeight()/2);
            bossCreated = true;
            informate("Hold your breath and avoid my energy orbs. Care, i'll punish you hardly if you try destroying them ");
        }
    }

    public void createBoss4()
    {
        if(Level == 4 && bossCounter == 300 && bossCreated == false)
        {
            Boss4 boss4 = new Boss4();
            addObject(boss4, getWidth(), getHeight()/2);
            bossCreated = true;
            informate("Care captain, the Rift's Queen uses space rifts as a weapon... Oh and, don't attack her if there are some hives in the map, they'll try to protect her anyway. Good Luck ");
        }
    }
I wanted to create a general method that unifies those like that:
public void generalCreate(int level, Classe classe, String text)
    {
        if(Level == level && bossCounter == 800 && bossCreated == false)
        {
            classe randomBoss = new classe();
            randomBoss.setImage(GreenfootImage random);
            //Maybe some other operations... and then:
            addObject(randomBoss, getWidth(), getHeight()/2);
            bossCreated = true;
            informate(text);
        }
    }
Indeed if I directly add the object to the world there's no more problem but if I want to modify it first, I think I need the method I want to create^^
Super_Hippo Super_Hippo

2016/2/2

#
Maybe you can merge all boss classes into one class. Then you can pass a number (which seems to be the same as the 'level' variable) to the class to decide which one has to be created.
danpost danpost

2016/2/2

#
You could merge them in this fashion:
public void createBoss()
{
    if (bossCreated) return;
    int[] counts = new int[] { 600, 800, 300 };
    if ((Level > 1 && Level < counts.length+2) && bossCounter == counts[Level-2])
    {
        Actor boss = null;
        if (Level == 2) boss = new Boss2();
        if (Level == 3) boss = new Boss3();
        if (Level == 4) boss = new Boss4();
        addObject(boss, getWidth(), getHeight()/2);
        String[] informaten = new String[]
        {
            "Let's test your ability to dodge, will you be able to survive these? ",
            "Hold your breath and avoid my energy orbs. Care, i'll punish you hardly if you try destroying them ",
            "Care captain, the Rift's Queen uses space rifts as a weapon... Oh and, don't attack her if there are some hives in the map, they'll try to protect her anyway. Good Luck "
        }
        informate(informaten[Level-2]);
        bossCreated = true;
    }
}
Vellyxenya Vellyxenya

2016/2/3

#
Thank you very much for your answers! However I had one more question (again it's just to simplify the code): Instead of writing this:
        if (Level == 1) boss = new Boss1();      
        if (Level == 2) boss = new Boss2();
        if (Level == 3) boss = new Boss3();
        if (Level == 4) boss = new Boss4();
        if (Level == 5) boss = new Boss5();
        if (Level == 6) boss = new Boss6();
        //etc...
Is it possible to do sth like this?
for (int i = 0; i<9; i++)
{
      if(Level == i) boss = new (Boss+i)();
}
I know the syntaxe is incorrect but it's just to give you an idea of what I want to do^^ Is there a way to transform the string ("Boss"+i) to a class name that the compilator could understand?
danpost danpost

2016/2/3

#
Vellyxenya wrote...
Is it possible to do sth like this?
for (int i = 0; i<9; i++)
{
      if(Level == i) boss = new (Boss+i)();
}
There is no way to do it that particular way.
Vellyxenya Vellyxenya

2016/2/3

#
Ok thank you!
Super_Hippo Super_Hippo

2016/2/3

#
The only things you could do is either use 'else if' instead of 'if', so not every condition has to be checked or use a switch:
switch (level)
{
    case 1: boss = new Boss1(); break;
    case 2: boss = new Boss2(); break;
    //...
}
Vellyxenya Vellyxenya

2016/2/4

#
Yea true, the switch is more appropriate in this case, thank you!
You need to login to post a reply.