Skip to main content
Participating Frequently
July 14, 2010
Answered

controlling children of sprites

  • July 14, 2010
  • 5 replies
  • 3087 views

In the code:

import flash.display.Sprite;

var spriteOne:Sprite = new Sprite()

spriteOne.graphics.beginFill(0x999999)

spriteOne.graphics.drawCircle(0,0,100)

var spriteTwo:Sprite = new Sprite()

spriteTwo.addChild(spriteOne)

addChild(spriteOne)

spriteOne.spriteTwo.alpha = .5

I get the error:

Scene 1, Layer 'Layer 1', Frame 1, Line 11 1119: Access of possibly undefined property spriteTwo through a reference with static type flash.display:Sprite.

I can control child movieclips on the stage this way, so why can't I control sprites made in AS3 this way?

This topic has been closed for replies.
Correct answer Ned Murphy

Based on the code you show, spriteTwo is not a child of spriteOne, so the error appears to be legit.

5 replies

Manjith_C_M_
Inspiring
July 14, 2010


var spriteOne:Sprite = new Sprite();

spriteOne.name="myCircle";

spriteOne.graphics.beginFill(0x999999);

spriteOne.graphics.drawCircle(0,0,100);

var spriteTwo:Sprite = new Sprite();

spriteTwo.addChild(spriteOne);

addChild(spriteTwo);

In as3 you can call sprite directly

spriteOne.alpha=.5;//-----directly---

or

var clip=spriteTwo.getChildByName("myCircle");//----------call by name

clip.alpha=.5;

J ClowAuthor
Participating Frequently
July 14, 2010

Sorry, that was a typo (I was typing a simplified example of the code I'm trying to use), but the fixed script still gives me the same error...

import flash.display.Sprite;

var spriteOne:Sprite = new Sprite()

spriteOne.graphics.beginFill(0x999999)

spriteOne.graphics.drawCircle(0,0,100)

var spriteTwo:Sprite = new Sprite()

spriteTwo.addChild(spriteOne)

addChild(spriteTwo)

spriteTwo.spriteOne.alpha = .5

Scene 1, Layer 'Layer 1', Frame 1, Line 11 1119: Access of possibly undefined property spriteOne through a reference with static type flash.display:Sprite.

Inspiring
July 14, 2010

If you read my previous post again you will notice that there is a statement that YOU CANNOT USE DOT NOTATION to access children.

Line spriteTwo.spriteOne.alpha = .5 IS WRONG. spriteOne IS A CHILD - NOT PROPERTY. Again, DisplayObjectContainer's children are not properties.

J ClowAuthor
Participating Frequently
July 14, 2010

The confusing thing is why dot notation works if you nest movieclips inside of movieclips on the stage but doesn't work when you use Actionscript to make the graphics instead....

I would like to use dot notation (because it is much cleaner than getChildAt(spriteTwo.getChildIndex(spriteOne)) ) if there is a way to do so, and I wonder if there is a way to do so when I make the object through actionscript instead of the Flash interface.

yes, and did read your previous post

Inspiring
July 14, 2010

1. What the lines

spriteTwo.addChild(spriteOne)

addChild(spriteOne)

do is add spriteOne as a child of spriteTwo --> REMOVES spriteOne from spriteTwo --> adds spriteOne as a child to the current scope. In other words sprtieOne IS NOT a child of spriteTwo any more.

2. DisplayObjectConteiner's children ARE NOT PROPERTIES. Thus, you cannot use dot notation. This is what the error says. To get children of an objects you need to use getChildAt(), getChildIndex() or getChildByName() methods.

if you did not remove spriteOne from sprtieTwo, the code could be:

spriteTwo.getChildAt(0).alpha = .5;

or, if it has many children:

spriteTwo.getChildAt(spriteTwo.getChildIndex(spriteOne)).alpha = .5;

Inspiring
July 14, 2010

J Clow wrote:


spriteTwo.addChild(spriteOne)

addChild(spriteOne)

spriteOne.spriteTwo.alpha = .5

Yep, as ned said, you're adding/putting spriteOne inside spriteTwo.

you should try spriteOne.addChild(spriteTwo); //here you'll be adding sprite2 to sprite1.

Is the same as when you say addChild(spriteOne); You could also say this.addChild(spriteOne); or stage.addChild(spriteOne); It's the same thing.

But I'm not so sure that this is the only problem in your code.

Ned Murphy
Ned MurphyCorrect answer
Legend
July 14, 2010

Based on the code you show, spriteTwo is not a child of spriteOne, so the error appears to be legit.