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

2012/10/20

Help with Object Interaction

PoleBro764 PoleBro764

2012/10/20

#
I am very new to programming and excited to be working on my first program. I have run into an unexpected problem with object interaction, however. No matter what I do-- the RedSquare and the KillCounter will not interact under any circumstance. I have tried (almost) everything and I am becoming more frustrated with myself every day. Please help. (Scenario is online w/ source code.)
Upupzealot Upupzealot

2012/10/20

#
private void setTheStage()
    {
        Actor redSquare = new RedSquare();
        addObject(redSquare, 300, 200);
        Actor blueRectangle = new BlueRectangle();
        addObject(blueRectangle, Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
        //change the next line as below
        killCounter = new KillCounter();
        addObject(killCounter, 55, 15);
    }
Done! XD
danpost danpost

2012/10/20

#
In your MyWorld class, in the setTheStage() method you are creating a new KillCounter object and setting an Actor reference variable to that object. That object is not the killCounter object that is referenced by your world KillCounter variable. Drop the word 'Actor' (the first word) from that line and things will run as you want. By removing the casting of the variable, the compiler looks for a variable that is already cast-set and finds the one you declared in the variable section of the class.
Upupzealot Upupzealot

2012/10/20

#
@danpost I'm faster than you this time!
danpost danpost

2012/10/20

#
@Upupzealot, you have shown the code and I have explained the change. I guess that together we covered it!
Upupzealot Upupzealot

2012/10/20

#
U and @SPower was the most warmhearted man here! U two always be seen helping other people!
PoleBro764 PoleBro764

2012/10/20

#
Well thanks you two. I will let you know later in the day if this works.
PoleBro764 PoleBro764

2012/10/20

#
Many thanks, it did!
Upupzealot Upupzealot

2012/10/20

#
XD
You need to login to post a reply.