A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: attach 1 MC from library multiple times

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    63

    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

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    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 (
    010i++)
    {
        
    targ = new en();
        
    targ.Math.round(Math.random() * wi);
        
    targ.Math.round(Math.random() * hi);

        array.
    push(targ);

        
    addChild(targ);
    }


    for (
    0< array.lengthi++)
    {
        
    trace("array:" "\t x:" + array[i]."\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

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    63
    Thank you very much
    Last edited by sandman99; 11-12-2014 at 10:38 PM.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    63
    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);
    }


  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    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

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    63
    ok, this works. No need to give instance name since it can be called via array. Thank you again.

    Quote Originally Posted by fruitbeard View Post
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center