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

2021/10/8

Adding an object

Windeycastle Windeycastle

2021/10/8

#
Hello, I am learning to program with Java, and I am making a tic tac toe game. I have a selector you can move around with the arrowkeys on the keyboard. That works, but now I would like it to spawn a cross or circle when you press enter. The addObject gives an error when I try to use it . Does anyone know what I'm doing wrong here?
       if(Greenfoot.isKeyDown("enter"))
        {
            cross cross1 = new cross();
            int e = getX();
            int f = getY();
            MyWorld.addObject(new cross(),e,f);
        }
This piece of code is written in the Selector class, which extends Actors. I have a cross class and naught class too, both empty.
danpost danpost

2021/10/8

#
Windeycastle wrote...
The addObject gives an error when I try to use it
MyWorld is the name of the class that creates your World objects. It is only the type of your world, not a reference to any particular world. Use:
getWorld().addObject(new cross(),e,f);
The getWorld method returns a reference to the World object that the Actor object (Selector object, in this case) is currently in.
Windeycastle Windeycastle

2021/10/8

#
Ok, thank you very very much! It worked!!
You need to login to post a reply.