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

2019/10/24

Problem: Trying to set class as a parameter in a method to spawn actors

comfortablyNumb comfortablyNumb

2019/10/24

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public void act()
    {
        //get a randint and compare to var with value 20
        if(Greenfoot.getRandomNumber(1500) < FallingActorSpawnRate)
        {
            //get another randint 0-3 spawn actor depending on value
            switch(Greenfoot.getRandomNumber(3))
            {
                case 1:
                    //the parameters passed here are seen as variables not classes
                    //these classes are subclasses of FallingActors which is a subclass of actor
                    spawnActor(Distractions);
                case 2:
                    spawnActor(Hints);
                case 3:
                    spawnActor(Questions);
                default:
                    break;
            }
        }
    }
    //take class as a parameter
    public void spawnActor(Class c)
    {
        //errors are where ever c is written as it doesnt see it as a parameter
        //instead it treats it as a normal class
        //create new actor
        c newActor = new c();
        c.setSpeed(FallingActorSpeed);
        addObject(c, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
    }
tldr - trying to spawn actors through a method that takes the class as a variable. Im not sure why but it sees the classes as variables and not classes. I've thought of different methods this like avoiding the spawnActor method completely and just writing the code to spawn an actor in the switch statement but that will bloat the code and i want to keep it condensed. Help will be greatly appreciated
danpost danpost

2019/10/24

#
comfortablyNumb wrote...
<< Code Omitted >> trying to spawn actors through a method that takes the class as a variable. Im not sure why but it sees the classes as variables and not classes.
This is because on lines 29 and 30 you are using the variable that holds the class -- not the one that holds the actor (which is newActor).
I've thought of different methods this like avoiding the spawnActor method completely and just writing the code to spawn an actor in the switch statement but that will bloat the code and i want to keep it condensed.
If you create and pass an actor instead of the class and give the different falling actors a common type (by same class extended, where your setSpeed method for all those classes is located), you not only avoid a second switch, but avoid multiple setSpeed methods. An appropriate intermediate class name between those classes and Actor might be Descender.
comfortablyNumb comfortablyNumb

2019/10/24

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void act()
    {
        if(Greenfoot.getRandomNumber(1500) < FallingActorSpawnRate)
        {
            switch(Greenfoot.getRandomNumber(3))
            {
                case 1:
                    Distractions newDist = new Distractions();
                    spawnActor(newDist);
                case 2:
                    Hints newHint = new Hints();
                    spawnActor(newHint);
                case 3:
                    Questions question = new Questions();
                    spawnActor(question);
                default:
                    break;
            }
        }
    }
     
public void spawnActor()   //how do i pass the object here?
{
    obj.setSpeed(FallingActorSpeed);
    addObject(obj, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
}
so something like this right? all of these actors are subclasses of FallingActor so they all inherit the setSpeed method from there.
danpost danpost

2019/10/24

#
comfortablyNumb wrote...
<< Code Omitted >> so something like this right? all of these actors are subclasses of FallingActor so they all inherit the setSpeed method from there.
Yes ... great.
1
public void spawnActor()    // how do i pass the object here?
Use FallingActor obj for the parameter.
danpost danpost

2019/10/24

#
You need break statements for each case in your switch block so that multiple actors are not put in the world. The random number will never be 3 as the 3 possible outputs are 0, 1 and 2. Also, you only need one spawnActor call:
1
2
3
4
5
6
7
8
FallingActor fa = null;
switch (Greenfoot.getRandomNumber(3))
{
    case 0:  fa = new Distractions(); break;
    case 1:  fa = new Hints(); break;
    case 2:  fa = new Questions(); break;
}
spawnActor(fa);
The default case is not needed as ALL possible values are already accounted for in the different cases.
comfortablyNumb comfortablyNumb

2019/10/24

#
alright thanks its working
danpost danpost

2019/10/24

#
It actually might be better to move the switch block into the spawnActor method without using the parameter. That would keep your act method very clean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void act()
{
    if (Greenfoot.getRandomNumber(1500) < FallingActorSpawnRate) spawnActor();
}
 
private void spawnActor()
{
    FallingActor fa = null;
    switch (Greenfoot.getRandomNumber(3))
    {
        case 0:  fa = new Distractions(); break;
        case 1:  fa = new Hints();        break;
        case 2:  fa = new Questions();    break;
    }
    fa.setSpeed(FallingActorSpeed);
    addObject(fa, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
}
danpost danpost

2019/10/24

#
Even cleaner would be to move the if condition also:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void act()
{
    spawnActor();
}
 
private void spawnActor()
{
    if (Greenfoot.getRandomNumber(1500) >= FallingActorSpawnRate) return;
    FallingActor fa = null;
    switch (Greenfoot.getRandomNumber(3))
    {
        case 0:  fa = new Distractions(); break;
        case 1:  fa = new Hints();        break;
        case 2:  fa = new Questions();    break;
    }
    fa.setSpeed(FallingActorSpeed);
    addObject(fa, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
}
comfortablyNumb comfortablyNumb

2019/10/25

#
thanks for the help thats a really cool solution. still new to java programming and using the whole object orientated thing.
You need to login to post a reply.