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

2014/1/5

trouble with class arrays

getreides getreides

2014/1/5

#
Hello everyone, I'm somewhat of a Greenfoot rookie and have a hard time with arrays containing classes (instead of strings, integers or else). What I'd like to do is a program that creates objects of my classes LetterA and LetterB in a fixed succession and screen position like this: --------------------------- | | | A | | B | | | | | | | ---------------------------- I know that there would be different (and easier) ways to create such a program; it's just I'd like to know how to create an array of classes. Check out what I've worked out so far. World class structure: public class background extends World Actor class structure: public class LetterA extends Actor / public class LetterB extends Actor --------------------------------------------------------------------------------------------------------------------------------------
public class background extends World
{   
    private Class[] classes =                  // I don't know how to declare an array which lists my classes 
                                                                // LetterA and LetterB - this is probably 
    {                                                           // the wrong way.. 
        LetterA, LetterB
    };
    
    private int[] Xcoord =
    {
       100, 200
    }; 
    
    private int[] Ycoord =
    {
       100, 200
    }; 
    
    public background() 
    {
        super(800, 600, 1);
        makeletters();
    }
    
    public void makeletters()
    {
        int i = 0;
        while (i < classes.length)
        {
            ??? = new ??? (classes[i]);                            // Neither do I know how to code the loop...
            addObject(Letter[i], Xcoord[i], Ycoord[i]);
            i = i + 1;
        }
    }
}
--------------------------------------------------------------------------------------------------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class LetterA extends Actor
{
    private String letterA;
    
    public LetterA (String LetterA)     // I don't know whether I need this code at all...
    {
        LetterA = LetterA;
    }
    
    public void act() 
    {
    }    
}
--------------------------------------------------------------------------------------------------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class LetterB extends Actor
{
    private String letterB;
    
    public LetterB (String LetterB) // I don't know whether I need this code at all...
    {
        LetterB = LetterB;
    }
    
    public void act() 
    {
    }    
}
-------------------------------------------------------------------------------------------------------------------------------------- Maybe you have an idea? Thanks in advance for your help.
danpost danpost

2014/1/5

#
Your 'classes' array declaration was close, but not quite there.
private Class[] classes = { LetterA.class, LetterB.class };
would probably work. However, using class names to create objects takes a bit more knowledge of java programming. Better would be to create an array of Actors:
private Actor[] actors = { new LetterA("A"), new LetterB("B") };
Then you can add the actors into the world using 'actors' and 'actors' for the actors. As far as needing any fields or parameters in the letter actor classes, because you have a separate class for each letter, they are not necessary:
import greenfoot.*;

public class LetterA extends Actor
{
}
should be all you need (and similar for the LetterB class), created with 'new LetterA()' and 'new LetterB()'.
danpost danpost

2014/1/5

#
You could also do this to declare your array:
private Actor[] actors = { 
    new Actor(){{ setImage(/* letterA filename in dbl-quotes */); }},
    new Actor(){{ setImage(/* letterB filename in dbl-quotes */); }}};
and remove both your LetterA and LetterB classes.
getreides getreides

2014/1/11

#
Hello danpost, sorry for the late reply, but thank you very much for your help! I decided to go the actor's way, as I don't feel comfortable enough to use a class array after you mentioned that this is quite sophisticated. My code looks this now for the world class:

import greenfoot.*;

public class background extends World
{   
    private Actor[] actors = 
    { 
        new LetterA(), new LetterB() 
    };

    private int[] Xcoord =
    {
       100, 200
    }; 
    
    private int[] Ycoord =
    {
       100, 200
    }; 
    
    public background() 
    {
        super(800, 600, 1);
        makeletters();
    }
    
    public void makeletters()
    {
        int i = 0;
        while (i < actors.length)
        {
            Actor actor = new Actor (actors[i]);
            addObject(actor, Xcoord[i], Ycoord[i]);
            i = i + 1;
        }
    }
}
My Letter-Classes LetterA and LetterB are empty (as you suggested). However, whenever I try to compile, I receive an error message: "greenfoot.Actor is abstract, cannot be instantiated". What could be the problem? Again, thanks in advance for help! (Sorry when the code is not displayed correctly, for some reason the code-tag doesn' work for me)
danpost danpost

2014/1/11

#
The problem is that you already created your letter actors at lines 5 through 8. Line 31 is somewhat of an attempt to again create those actors. Remove line 31 and change line 32 to
addObject(actors[i], Xcoord[i], Ycoord[i]);
Line 32 now will add those actors already created into the world.
getreides getreides

2014/1/11

#
Hey, thank you so much, no it works! I'll have to get back to you in near future :-)
You need to login to post a reply.