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

2013/12/25

Create two actors and give one another

bernhard bernhard

2013/12/25

#
I create two Actors (startGame1, tutorialButton1) in a world and I want to give the actor startGame1 the tutorialButton1 and then just the other way around. I typed this:
 StartGame startGame1 = new StartGame(tutorialButton1);
TutorialButton tutorialButton1 = new TutorialButton(startGame1);
Of course it gave me an error: "illegal forward reference", marking "tutorialButton1". If I just swap the lines like this:
TutorialButton tutorialButton1 = new TutorialButton(startGame1);
StartGame startGame1 = new StartGame(tutorialButton1);
it is the same, but it marks "startGame1". I think this is, because I try to give an actor that has not been created yet. However, I can't really figure out any way to solve the problem. Thanks for your help in advance.
erdelf erdelf

2013/12/25

#
    TutorialButton tutorialButton1;
    StartGame startGame1 = new StartGame(tutorialButton1);  
    tutorialButton1 =  = new TutorialButton(startGame1);  
shouldnt this work`?
danpost danpost

2013/12/25

#
My Usage of 'this' demo scenario has an example of how to link one object with another. Refer to how the portals are created in the world class and what the constructors of the portal class do (there are two constructors in that class: one to create the initial portal and one to cross-link a previously created portal with the one being created).
danpost danpost

2013/12/25

#
erdelf wrote...
    TutorialButton tutorialButton1;
    StartGame startGame1 = new StartGame(tutorialButton1);  
    tutorialButton1 =  = new TutorialButton(startGame1);  
shouldnt this work`?
'tutorialButton1' is still 'null' in line 2.
erdelf erdelf

2013/12/25

#
and`? the reference is there, if he isnt using methods with the reference in the constructor, then there should be no problem i think
danpost danpost

2013/12/25

#
erdelf wrote...
and`? the reference is there, if he isnt using methods with the reference in the constructor, then there should be no problem i think
Only the value is passed to the constructor; not the field.
bernhard bernhard

2013/12/25

#
Erdelf: unfortunately this does Not work, i already tried it. I will try wüst danpost told me tomorrow.
davmac davmac

2013/12/25

#
Erdelf:
and`? the reference is there, if he isnt using methods with the reference in the constructor, then there should be no problem i think
This implies a severe misunderstanding of how parameter passing works. If you pass a null value to a constructor or method, then on the other end, all you have is null. You can save the null value somewhere but it is still null and it won't automatically change. bernhard: To create a cyclic reference between two objects you really have two choices: 1. Have one object create the other and pass (this) to it. This way both objects have references to each other. You can add a getXXX() method to retrieve the second object from the first if necessary. Your code above then becomes something like:
StartGame startGame1 = new StartGame();  
TutorialButton tutorialButton1 = startGame1.getTutorialButton(); 
// note that you must write a getTutorialButton method in the StartGame class!
2. Create both objects and use a setXXX() method to pass the first one to the second one. The first object will not be able to reference the second object in its constructor, however:
StartGame startGame1 = new StartGame();  
TutorialButton tutorialButton1 = new TutorialButton(startGame1);
startGame1.setTutorialButton(tutorialButton1);
// You must also write the setTutorialButton method.
erdelf erdelf

2013/12/25

#
@davmac, i just forget that sometimes, sry
danpost danpost

2013/12/25

#
@davmac, a third option is similar to your second option, except that line 3 in your code would be done in the TutorialButton constructor passing 'this':
public TutorialButton(StartGame startGame1)
{
    this.startGame1 = startGame1;
    startGame1.setTutorialButton(this);
}
The 'setTutorialButton' method would still need written in the StartGame class. I prefer this option because the cross-linking is done at one time.
bernhard bernhard

2013/12/27

#
Thank you guys, it works now. However, I got 4 Buttons in the world and I want all of them to disappear i one is clicked. I'm going to try to make it work and hope to do it.
bernhard bernhard

2013/12/27

#
Works perfectly, thank y'all.
You need to login to post a reply.