-
infidel!
addChild problem
Hello!
Here is mai document class:
Code:
package
{
import flash.display.MovieClip;
/**
* ...
* @author infidel - aka Ovidiu Gheorghe
*/
public class mainTest extends MovieClip
{
public function mainTest()
{
addBg();
addSquare();
}
private function addBg():void
{
var myBg:bg = new bg();
myBg.x = 0;
myBg.y = 0;
myBg.alpha = 0.15;
addChild(myBg);
}
private function addSquare():void
{
var redSquare:square = new square();
redSquare.x = stage.stageWidth / 2;
redSquare.y = stage.stageHeight / 2;
myBg.addChild(redSquare);
}
}
}
The addSquare() function returns this error:
"1120: Access of undefined property myBg."
But if I move all the lines from addSquare() function into the addBg() function, everything goes well!
Can somebody please explain why?... and what is the correct way to implement this (basically I want the redSquare mc to be attached in myBg)?
Tnx a bunch,
Ovidiu
-
The variable myBg:bg that you create only has scope within the addBg() method, this is why addSquare has no knowledge of it, and this is why the code works when you move the code to the addBg() method. You should define myBg:bg as a class variable, and then all methods of that class will have access to that variable.
-
infidel!
thank you! here is a workin' version
kind regards!
Code:
package
{
import flash.display.MovieClip;
/**
* ...
* @author infidel - aka Ovidiu Gheorghe
*/
public class mainTest extends MovieClip
{
var myBg:bg = new bg();
public function mainTest()
{
addBg();
addSquare();
}
private function addBg():void
{
//var myBg:bg = new bg();
myBg.x = 0;
myBg.y = 0;
myBg.alpha = 0.15;
addChild(myBg);
}
private function addSquare():void
{
var redSquare:square = new square();
redSquare.x = stage.stageWidth / 2;
redSquare.y = stage.stageHeight / 2;
myBg.addChild(redSquare);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|