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

2013/7/31

Can't set location for classes stored in an array.

Entity1037 Entity1037

2013/7/31

#
I'm trying to a simple scrolling engine, but I'm having difficulty. I'm trying to go through an array of classes, find all of them in a List, and set the location for each one in the list. However, I have to use this in order to use methods of the classes:
(([Object's Name Here])[defined actor]).setLocation([Stuff Here]);
I tried making an array, but the Object's name is a symbol, and I don't have a way of storing a symbol. Does anyone know how to do this? Here's my code:
Class[] all = {Wall.class,Key.class,Block.class,Lava.class,Water.class};
    public void scroll(String direction){
        int v;
        int h;
        if (direction.equals("left"))h=getWidth();
        if (direction.equals("right"))h=-getWidth();
        if (direction.equals("up"))v=getHeight();
        if (direction.equals("down"))v=-getHeight();
        int a;
        while (a<all.length){
            List object = getObjects(all[a]);
            if (! object.isEmpty()){
                for (int i=0; i<object.size(); i++){
                    Actor Object = (Actor) object.get(i);
                    if (Object!=null)Object.setLocation(getX()+h,getY()+v); //It won't let me do this; I have to replace "Object.setLocation();" with "(([Name Here])Object).setLocation();"
                }
            }
            a++;
        }
    }
danpost danpost

2013/7/31

#
On lines 14 and 15, you are using 'Object' as a local name, which should not cause any problems here, but is poor programming as 'Object' is a Class name (for one) and non-final field names should not be named using leading uppercase letters (coding convention). The main problem, however, is probably due to the fact that you are using 'getX' and 'getY' in the world class without specifying an actor object to execute them on (those methods are not part of the World class API, but part of the Actor class API and need to be executed on the actor in question).
// lines 14 and 15
Actor actor = (Actor) object.get(i);
actor.setLocation(actor.getX()+h, actor.getY()+v);
The conditional 'if (actor != null)' is not necessary as all elements in the array between 0 and all.length-1 are objects that can be cast to Actor. In fact, the code from line 9 on can be re-written as:
for (Class cls : all)
{
    for (Object obj : getObjects(cls))
    {
        Actor actor = (Actor) obj;
        actor.setLocation(actor.getX()+h, actor.getY()+v);
    }
}
Entity1037 Entity1037

2013/7/31

#
Thank you! I actually feel really stupid now; all I needed to do was "actor.getX()". I don't have much experience, so I have no idea how "Class cls : all" works, but I guess I'll learn that next year.
danpost danpost

2013/7/31

#
You can find out about how to use the 'for' statement here.
Entity1037 Entity1037

2013/7/31

#
Oh I get it now! Thank you!
You need to login to post a reply.