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

2013/6/6

Use string to define a class name?

Entity1037 Entity1037

2013/6/6

#
I'm trying to use a string array so I can scroll through different classes instead of hard programming everything. I'm trying to do something like:
String[] objects = {"Wall.class"};
    int collisionAmount=0;
    public void wallCollision()
    {
        while (collisionAmount<objects.length){
            //Down check
            for (int i=-getImage().getWidth()/2+2; i<getImage().getWidth()/2; i++){
                Actor object = getOneObjectAtOffset(i, getImage().getHeight()/2,objects[collisionAmount]);
                if (object!=null&&ymove>0){ymove=0;}
            }
            //Up check
            for (int i=-getImage().getWidth()/2+2; i<getImage().getWidth()/2; i++){
                Actor object = getOneObjectAtOffset(i, -getImage().getHeight()/2,objects[collisionAmount]);
                if (object!=null&&ymove<0){ymove=0;}
            }
            //Left check
            for (int i=-getImage().getHeight()/2; i<getImage().getHeight()/2; i++){
                Actor object = getOneObjectAtOffset(-getImage().getWidth()/2, i,objects[collisionAmount]);
                if (object!=null&&xmove<0){xmove=0;}
            }
            //Right check
            for (int i=-getImage().getHeight()/2; i<getImage().getHeight()/2; i++){
                Actor object = getOneObjectAtOffset(getImage().getWidth()/2, i,objects[collisionAmount]);
                if (object!=null&&xmove>0){xmove=0;}
            }
            collisionAmount++;
        }
        collisionAmount=0;
    }
I get an error saying that I'm defining a string in the third parameter when I define the actor "object" instead of a class. Is there a way I can make it defined as a class? Thank You.
danpost danpost

2013/6/6

#
Instead of using a String array, you should be using a Class array:
Class[] classes = { Wall.class };
Entity1037 Entity1037

2013/6/6

#
Oh, I didn't know those existed. Ok, that makes a lot of sense! Thank you!
You need to login to post a reply.