views:

36

answers:

3

my problem is that after i create the new movie clips i don't know how to access them

var numOfBalls:int = 5;
var balls:Array = new Array();

import flash.utils.getDefinitionByName;
function addBall(instanceName:String) {
    var mcIName:String = "ball";
    var tMC:Class = getDefinitionByName(mcIName) as Class;
    var newMc:MovieClip = new tMC() as MovieClip;
    newMc.name = instanceName;
    trace("added " + newMc.name);
    newMc.x = randRange(10, 300);
    newMc.y = randRange(10, 300);

    addChild(newMc);

    return this.newMc;
}

function randRange(start:Number, end:Number) : Number {
    return Math.floor(start +(Math.random() * (end - start)));
}

var  i = 0;
while ( i < numOfBalls) {

    balls[i] = addBall("ball_" + i);
    i++;
    }

trace (this.balls[0]); // returnes error
trace (this.balls_0); //returnes error
A: 
function addBall(instanceName:String):MovieClip {
    var mcIName:String = "ball";
    var tMC:Class = getDefinitionByName(mcIName) as Class;
    var newMc:MovieClip = new tMC() as MovieClip;
    newMc.name = instanceName;
    trace("added " + newMc.name);
    newMc.x = randRange(10, 300);
    newMc.y = randRange(10, 300);

    addChild(newMc);

    return newMc;
}

That should fixe it. return typed to MovieClip, and the fix bit, return newMc instead of this.newMc; newMC doesn't belong to this.

if you had this.newMc = newMC maybe.

George Profenza
A: 

you need to specify what the addBall function is returning

function addBall(instanceName:String):MovieClip {

and you may have to push the balls into the balls array, like

balls[i].push(addBall("ball_" + i));
daidai
A: 

try this, i'm not sure about your problem
The only thing i can see is the index i where you write the name of the new MovieClip, in ActionScript3 you can't pass a numeric value to a String without convert it with toString() method, try to fix it and see if it works

var numOfBalls:int = 5;
var balls:Array = new Array();

import flash.utils.getDefinitionByName;
function addBall(instanceName:String):MovieClip {
    var mcIName:String = "ball";
    var tMC:Class = getDefinitionByName(mcIName) as Class;
    var newMc:MovieClip = new tMC() as MovieClip;
    newMc.name = instanceName;
    trace("added " + newMc.name);
    newMc.x = randRange(10, 300);
    newMc.y = randRange(10, 300);
    addChild(newMc);

    return this.newMc;
}

function randRange(start:Number, end:Number) : Number {
    return Math.floor(start +(Math.random() * (end - start)));
}

var  i = 0;
while ( i < numOfBalls) {
    // convert i with toString() is requested in as3 or will return ERRORS
    balls.push(addBall("ball_" + i.toString ()));
    i++;
}

trace (MovieClip(this.balls[0]));
Vittorio Vittori