PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
help with attachMovie and positioning
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: help with attachMovie and positioning

  1. #1
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755

    help with attachMovie and positioning

    this is the function I currently call:

    code:

    function createTemplates():Void {
    for (i = 0; i < totalProducts; i++) {
    var attachProduct:MovieClip = attachMovie("templateClip", "product" + i, i);
    attachProduct._y +=10;
    attachProduct._y += i * 230;
    }
    }




    searching..I read several threads..and tried to implement the same "solution"

    using "x" & "y" vars for rows & columns... and then running a loop.. through them...however..I am NOT getting the results I want..

    either nothing happens...only 1 clips gets attached.. 1 clip nothing is populated..etc..

    I like to be able to control the loops through the loop paramters like x<2
    & y<10 etc..etc..

    here is my last attaempt:

    code:

    function createTemplates():Void {
    for (i = 0; i < totalProducts; i++) {
    var attachProduct:MovieClip = attachMovie("templateClip", "product"+ i, i);
    for (x=0; x<2; x++) {
    for(y=1; y<10; y++){
    attachProduct._x = x*50;
    attachProduct._y = y*50;
    }
    }
    }
    }



    I get the LAST clip in my XML file only!! HAHAH


    anybody see what Im doing wrong?

    thanks

  2. #2
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    The problem is a little hard to explain. Look at what you're doing for each clip. You're basically sending the same commands to every single clip, so they all end up in the same place. You have to use the indexing number i. Here's how it works.

    code:

    function createTemplates():Void {
    //Define the grid width (in number of cells);
    var width:Number = 4;
    //Define the item spacing;
    var spacing:Number = 50;

    for (i=0; i<totalProducts; i++) {
    var attachProduct:MovieClip = attachMovie("templateClip", "product"+i, i);

    /*Ok, we want to use the index i. For the first four elements, we want to
    have a _y value of 0. For the next four, we want a _y value of 50. For the
    next four, _y = 100. So the formula will be (How many times 4 divides into
    i) * 50. To find out how many times a number divides into another number,
    we simply divide by the number and take the floor.*/
    attachProduct._y = Math.floor(i / width) * spacing;

    /*Now for the x values, we want to find out the remainder thats left after
    we divide by four, and then multiply this by 50. So for instance item 5 should
    have an _x value of 50. The reamainder of 5 after dividing by 4 is 1. 1 * 50 = 50.
    This works for any number. Luckily, Flash has a handy operator for working
    out the remainder, and its the modulo operator %*/
    attachProduct._x = (i % width) * spacing;

    //That should do it!
    }
    }



    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  3. #3
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    AWESOME!... I actually edited it somewhatso I had independent variables for Hspacing & Vspacing (productClips arent always perfectly square) but your example was top notch! thanks.

    off to try the object thread now!

    actionscript Code:
    var totalClips:Number = 25;

    function createTemplates():Void {
        //Define the grid columns
        var columns:Number = 5;
        //Define the spacing/padding
        var hPadding:Number = 5;
        var vPadding:Number = 5;

        for (i = 0; i < totalClips; i++) {
            var newClip:MovieClip = attachMovie("templateClip", "product" + i, i);
            //initial positions
            newClip._x = 10;
            newClip._y = 10;
            //grid positioning
            newClip._x += (i % columns) * (hPadding + newClip._width);
            newClip._y += Math.floor(i / columns) * (hPadding + newClip._width);
           
        }
    }

    createTemplates();

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