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

2018/3/8

how do i change the name of the Jordan each time the loop runs and adds an object

itssemu itssemu

2018/3/8

#
public static int jordanCount; for(int makeJordan = 0; makeJordan <=11; makeJordan++) { firing = true; jordanCount++; int Y = makeJordan *25; Jordan MakeJordan = new Jordan(); addObject(new Jordan(), 575, Y + 10); }
danpost danpost

2018/3/8

#
Use a 'switch' block:
for(int makeJordan = 0; makeJordan <=11; makeJordan++)
{
    firing = true;
    jordanCount++;
    int Y = makeJordan *25;
    Actor actor = null;
    switch (makeJordan) {
        case 0:  actor = new Jordan(); break;
        case 1:  actor = new Jordan2(); break;
        case 2:  actor = new Jordan3(); break;
        // etc.
    }
    addObject(actor, 575, Y+10);
}
Make sure you have all 12 cases (case 0 through case 11) or a NullPointerException will be thrown.
itssemu itssemu

2018/3/8

#
thank you so much for the reply but I have another question, when I tried to implement this code into my game it said that Jordan2() was not defined? also how would I individually call these objects?
danpost danpost

2018/3/8

#
itssemu wrote...
when I tried to implement this code into my game it said that Jordan2() was not defined? also how would I individually call these objects?
Maybe you were unclear as to what you wanted. The 'Jordan2' and 'Jordan3' were class names I gave the other spawned actors. Apparently, what you want is to spawn a bunch of Jordan objects and keep references to them. In this case, you would simply have this:
// array field
private Jordan[] jordans = new Jordan[12];

// with this loop
firing = true;
for (int i=0; i<jordans.length; i++)
{
    jordans[i] = new Jordan()
    addObject(jordans[i], 575, i*25+10);
}
Line 8 shows how you can refer to them. Use any value, 0 through 11, in square brackets after 'jordans' to reference one.
itssemu itssemu

2018/3/8

#
yes it seems that i was unclear thankyou for this it helped a ton
itssemu itssemu

2018/3/8

#
hey sorry one last question can you change the image of only one of those actors?
danpost danpost

2018/3/8

#
itssemu wrote...
hey sorry one last question can you change the image of only one of those actors?
Sure. For example:
int rand = Greenfoot.getRandomNumber(12);
jordans[rand].setImage("Jeremiah.png");
Do not worry about the name of the file -- it is whatever image you want to set the one actor to. Another example (like from an actor class, not Jordan):
Jordan jordan = getOneIntersectingObject(Jordan.class);
if (jordan != null) jordan.setImage("Jeremiah.png");
itssemu itssemu

2018/3/8

#
thankyou so much
itssemu itssemu

2018/3/8

#
haha ok im stumped again, i can't seem to get the shot actor to be removed from the world when it touches the JCurse
        if(isTouching(JCurse.class))
        {
            getWorld().removeObject(this);
        }
itssemu itssemu

2018/3/8

#
that is the code that should work right? it worked for removing the JCurse, just the two cannot collide and then both dissapear.
danpost danpost

2018/3/8

#
itssemu wrote...
it worked for removing the JCurse, just the two cannot collide and then both dissapear.
Surely, you can have both disappear. You cannot, however, expect one to remove the other after the other has already removed the one. Only put collision detection between the two in one of the two classes. Have the actor remove the other and then remove itself. For example, in the shot class, you can put:
if(isTouching(JCurse.class))
{
    removeTouching(JCurse.class);
    getWorld().removeObject(this);
}
itssemu itssemu

2018/3/9

#
thank you!!!!
You need to login to post a reply.