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

2012/5/13

Problem with Creating Actors

erdelf erdelf

2012/5/13

#
Hi, my Problem is that this method adds only one Object.
    public void spawnS(Actor obj)
    {
        addObject(obj, 1, 120);
        addObject(obj, 1, 200);
        addObject(obj, 1, 300);
        addObject(obj, 1, 400);
        addObject(obj, 1, 500);
    }
danpost danpost

2012/5/13

#
That is because you only created one. Out of curiousity, does the one created show on the top or bottom of the screen? You need to only add one in the method, but call it five times; each time with a 'new' object. So, if your actor class name was Soldier, use:
for (int i = 0; i < 5; i++) spawnS(new Soldier(), i);
and change the method to:
erdelf erdelf

2012/5/14

#
danpost wrote...
and change the method to:
public void spawnS(Actor obj, int objNum))
{
    int yLoc = objNum > 0 ? 100 * (objNum + 1) : 120;
    addObject(obj, 1, yLoc);
}
could you explain me the changed method?
davmac davmac

2012/5/14

#
It might be overkill for something this simple. The simplest fix for your code as posted would be:
    public void spawnS()  
    {  
        addObject(new MyActor(), 1, 120);  
        addObject(new MyActor(), 1, 200);  
        addObject(new MyActor(), 1, 300);  
        addObject(new MyActor(), 1, 400);  
        addObject(new MyActor(), 1, 500);  
    }  
This doesn't let you do anything with the new actors, however. It does illustrate the problem them - if you 'add' the same actor twice, you still only have one actor...
erdelf erdelf

2012/5/14

#
Isn't there a way to make the method so that I can define when I call it which obj should be added?
davmac davmac

2012/5/14

#
which obj? I thought the whole point was you wanted multiple objects?
erdelf erdelf

2012/5/14

#
I made this method to create multiple objects, yeah right, but I wanted that I can choose which objects I want to create
davmac davmac

2012/5/14

#
You mean, you want to choose which type (which class) of object to create? The easiest way is to have a method which takes a parameter specifying the type, and creates an object of that type, eg:
private Actor createActor(String type)
{
    if (type.equals("A")) return new A();
    if (type.equals("B")) return new B();
    return new C();
}
Then you have your spawn method as follows:
    public void spawnS(String type)    
    {    
        addObject(createActor(type), 1, 120);    
        addObject(createActor(type), 1, 200);    
        addObject(createActor(type), 1, 300);    
        addObject(createActor(type), 1, 400);    
        addObject(createActor(type), 1, 500);    
    }
erdelf erdelf

2012/5/14

#
Yes, thank you. I am not sure that I understand
davmac wrote...
You mean, you want to choose which type (which class) of object to create? The easiest way is to have a method which takes a parameter specifying the type, and creates an object of that type, eg:
private Actor createActor(String type)
{
    if (type.equals("A")) return new A();
    if (type.equals("B")) return new B();
    return new C();
}
Are A and B the class names?
trash1000 trash1000

2012/5/14

#
Yes, they are.
erdelf erdelf

2012/5/14

#
Ok thx. You three really helped me.
davmac davmac

2012/5/14

#
Are A and B the class names?
And C, in my example.
You need to login to post a reply.