Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-24 Thread Rich Rainbolt

Unsubscribe Remove
On Oct 19, 2007, at 9:30 PM, Troy Rollins wrote:



On Oct 19, 2007, at 10:05 AM, Alistair Colling wrote:


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;


Well, if you _really_ want to, you can do it with 1...

//
// This code will run in a frame script
import flash.display.*;

var cont:Sprite = new Sprite();
cont.name = "cont"
this.addChild(cont);
var cont1:Sprite = new Sprite();
cont1.name = "cont1"

var child1:Sprite = new Sprite();
child1.name = "child1"
cont1.addChild(child1);
cont.addChild(cont1);


var childTest:Sprite = Sprite(cont.getChildByName 
("cont1")).getChildByName("child1") as Sprite;

childTest.visible = true;
trace(childTest.name + " " + childTest);
stop();
///

Of course, I effectively casted twice in one line, but hey...  ;-)

--
Troy
RPSystems, Ltd.
http://www.rpsystems.net


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-24 Thread Rich Rainbolt

Unsubscribe Remove


On Oct 19, 2007, at 8:35 PM, 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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders




___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-23 Thread Troy Rollins


On Oct 19, 2007, at 11:35 PM, keith wrote:

Is there a reason you can't access the Sprite with the variable you  
first created?

cont.visible=false;


I think the OP was using the code as an example, but that creation  
and modification would take place at two different times in actual  
practice.


--
Troy
RPSystems, Ltd.
http://www.rpsystems.net


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-23 Thread [EMAIL PROTECTED]
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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders




___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-21 Thread [EMAIL PROTECTED]

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

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-21 Thread Troy Rollins


On Oct 19, 2007, at 10:05 AM, Alistair Colling wrote:


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;


Well, if you _really_ want to, you can do it with 1...

//
// This code will run in a frame script
import flash.display.*;

var cont:Sprite = new Sprite();
cont.name = "cont"
this.addChild(cont);
var cont1:Sprite = new Sprite();
cont1.name = "cont1"

var child1:Sprite = new Sprite();
child1.name = "child1"
cont1.addChild(child1);
cont.addChild(cont1);


var childTest:Sprite = Sprite(cont.getChildByName 
("cont1")).getChildByName("child1") as Sprite;

childTest.visible = true;
trace(childTest.name + " " + childTest);
stop();
///

Of course, I effectively casted twice in one line, but hey...  ;-)

--
Troy
RPSystems, Ltd.
http://www.rpsystems.net


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-21 Thread keith
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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders




___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] Short syntax for accessing nested display objects? (AS3)

2007-10-19 Thread Alistair Colling

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders