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
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
Maybe you have an idea? Thanks in advance for your help.
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()
{
}
}
