-
attach 1 MC from library multiple times
This attaches my MC from the library one time on the stage but I want it to attach it more than once. Anyone know what I'm doing wrong?
code:
import flash.display.MovieClip;
var wi:Number = stage.stageWidth;
var hi:Number = stage.stageHeight;
var enw:Number = Math.random()*wi;
var enh:Number = Math.random()*hi;
var i:Number;
var targ:MovieClip = new en();
var array:Array = new Array();
for (i=0; i<10; i++)
{
array[i] = targ;
addChildAt(array[i], i);
array[i].x = enw;
array[i].y = enh;
array[i].push(array[i]);
}
The linkage name of my MC from the library is en
-
.
Hi,
I think you got slightly confused somewhere along the lines, try something more like so
PHP Code:
import flash.display.MovieClip;
var wi:Number = stage.stageWidth;
var hi:Number = stage.stageHeight;
var i:Number;
var targ:en;
var array:Array = new Array();
for (i = 0; i < 10; i++)
{
targ = new en();
targ.x = Math.round(Math.random() * wi);
targ.y = Math.round(Math.random() * hi);
array.push(targ);
addChild(targ);
}
for (i = 0; i < array.length; i++)
{
trace("array:" + i + "\t x:" + array[i].x + "\t - y:" + array[i].y);
}
I added some traces for you and the Math.round, stops you getting 56.95 or 103.02 as you x and y values
-
Last edited by sandman99; 11-12-2014 at 10:38 PM.
-
does the following assign an instance name to each object being attached? If so then why can't I target it.
code:
import flash.display.MovieClip;
var wi:Number = stage.stageWidth;
var hi:Number = stage.stageHeight;
var i:Number;
var targ:en;
var array:Array = new Array();
for (i = 0; i < 10; i++)
{
targ = new en();
targ.x = Math.round(Math.random() * wi);
targ.y = Math.round(Math.random() * hi);
targ.name = "eni"+i;
array.push(targ);
addChild(targ);
}
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(event:Event):void
{
eni1.x += 3;
}
for (i = 0; i < array.length; i++)
{
trace("array:" + i + "\t x:" + array[i].x + "\t - y:" + array[i].y + "\t Name:" + array[i].name);
}
-
.
Hi,
Instead of eni1.x += 3; you can target the array contents like so array[0].x += 3;, arrays start at 0 remember.
Or you can read up about getChildByName
-
ok, this works. No need to give instance name since it can be called via array. Thank you again.
Originally Posted by fruitbeard
Hi,
Instead of eni1.x += 3; you can target the array contents like so array[0].x += 3;, arrays start at 0 remember.
Or you can read up about getChildByName
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|