View Full Version : Adding a dynamic movieclip to a dynamically generated movieclip
srajakaruna
January 19th, 2009, 02:02 AM
var _mc:MovieClip = new MovieClip();
var this['clip_new'] = new MovieClip();
for (var _i:int = 0; _i<10; _i++){
this['_clip'+_i+''] = new MovieClip();
_mc.addChild(this['_clip'+_i+'']);
}
addChild(_mc);
this['_clip0'].addChild(this['clip_new']); // error
how to add a dynamically generated moviclip as a child, to a dynamically generated parent movieclip .. pls help
Felixz
January 19th, 2009, 06:24 AM
var clip:Sprite = new Sprite();
var clips:Vector.<Sprite> = new Vector.<Sprite>(10, true);//for FP9: var clips:Array = new Array(10);
for (var i:int = 0; i < 10; i++) {
clips[i] = new Sprite();
clip.addChild(clips[i]);
}
addChild(clip);
clips[4].doSomething();
ayumilove
January 19th, 2009, 06:29 AM
just wandering why would people use _ underscore for their variable names?
When comparing both code, the threadstarter code looks kinda messy/difficult to read
compare to Felixz
//Felixz have not removed the underscore from _i++ ?
for (var i:int = 0; i<10; i++) {
clips[i] = new Sprite();
clip.addChild(clips[i]);
}
Felixz
January 19th, 2009, 06:42 AM
I have fixed it right when you have posted... ^^
The underscores are used to have simiral variable names or when one is already used.
Generally they are used to private properties while public ones exist, ex.
private var _num:int
public function get num():int {
doSomething();
return this._num;
}
public function set num(value:int):void {
doSomething();
this._num = value;
}
ayumilove
January 19th, 2009, 06:54 AM
I have read in AS3 Optimization that the get and set functions are slow compare to the ones that you make your custom function for that purpose.
And I have a doubt on function returning a value.
Assuming that the function is able to return either a string or int,
how should we write that?
function getProductInfo(): ???{
//returns product name
//returns product price
}
Felixz
January 19th, 2009, 07:47 AM
package {
public class Product {
private var _name:String;
private var _price:Number;
public function Product(name:String, price:Number) {
this._name = name;
this._price = Math.abs(price);
}
public function get price():Number {
return this._price;
}
public function get name():String {
return this._name;
}
}
}
function getProductInfo:Product {
return new Product(someName, somePrice);
}
Well, OOP has no problems with returning that.
And as you can see, the product name and price is read-only, so nobody can change it by random.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.