sending again because my first post didn't seem to get through. sorry if
this is a double post...
------------------
hi alistair,
you are confusing variable names with instance names. they're not the
same thing.
when you do this:
var cont1:Sprite = new Sprite();
cont.addChild(cont1);
you are not naming the sprite "cont1", you are simply assigning the
variable cont1 a reference to the new Sprite.
so you can't do:
cont.getChildByName("cont1");
because the Sprite referenced by cont has no instance name.
you *can* do this:
var room:Sprite = new Sprite();
var b:Sprite = new Sprite();
b.name = "ball";
room.addChild(b);
trace(room.getChildByName("ball"));
however, that technique is not recommended because it requires you to
refer to the object by an arbitrary string name ("ball") that cannot be
checked at compile time. in a pure ActionScript 3.0 program, rather than
using instance names, you should reference all display objects through a
variable. e.g.,
b.bounce()
instead of:
Ball(room.getChildByName("ball")).bounce()
(in the above code, the cast to Ball is required because
getChildByName()'s return type is DisplayObject.)
if other areas of your code need access to the ball, pass them a
reference to it. for example, you might have a Room class with a
getBall() method:
public function getBall ():Ball {
return theBall;
}
all that said, if you are just creating instances on the timeline in the
Flash authoring tool, and you name them using the properties panel, you
can reference them directly by name. e.g., if you have a clip that you
named "ball" on the main timeline, you can reference that clip using
this code on that timeline:
trace(this.ball);
the preceding code works because the compiler automatically adds a
variable named 'ball' to the document class (the class representing the
main timeline), and assigns 'ball' a reference to your named clip.
colin
keith wrote:
Is there a reason you can't access the Sprite with the variable you
first created?
cont.visible=false;
-- Keith H --
Alistair Colling wrote:
Hiya everyone, good to see the list back up and running.
I'm trying to access a sprite that is inside of 2 other sprites and I
have 2 questions about this:
1) It seems that the syntax for targeting this sprite once it is
created is quite long winded (2 temporary vars have to be created):
var cont:Sprite = new Sprite();
this.addChild(cont);
var cont1:Sprite = new Sprite();
cont.addChild(cont1);
var child1:Sprite = new H1();
var target =cont.getChildByName("cont1");
var targ2 = target.getChildByName("child1");
targ2._visible = false;
2) Also, I can't data type my target var as a Sprite otherwise I get
an error (unless I cast the type as well).
If anyone knows of a better of doing this I would be really interested
to hear it :)
Cheers,
Ali
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders