2
votes

I'm trying to to do some dynamic MovieClips placement here. I've been trying for a couple'o days and I can't find the right way to do the trick. It's the continuation for this. I didn't manage to properly make my MC's appear in the triangle, so I've made a pyramid of rectangles (suits my case better, beacuse I can change each of'em, to better fit my desired shape - a not-so-regular triangle). Here's the code with some comments:

import flash.events.MouseEvent;
import flash.display.MovieClip;

btn_toys_2.confirm.addEventListener(MouseEvent.MOUSE_UP, confirmToys);

var toysPlc:Array = new Array(); //an array for a bunch of rectangles
var toAdd:int = 100 //this is supposed to be dynamic, user defined
var toy:MovieClip = new shar_001; //an MC from a library

for (var j:int=0; j<33; j++){ 
    toysPlc.push("tPl_" + j); //here I add that bunch of rects into an array
}

function confirmToys(e:MouseEvent):void{
    for (var k:int=0; k<toAdd; k++){ //supposed to add an "toAdd" amount of "toys"
    var p:int = Math.random()*toysPlc.length; //^do so in a random rect
    toysPlc[p].addChild(toy); //supposed to place a toy in a random rect
    toy.x = Math.random()*toysPlc[p].width; //positioning
    toy.y = Math.random()*toysPlc[p].height;  //positioning
    }
}

The error I get is: TypeError: Error #1006: value is not a function.

What I DID manage is to place a single toy in a random of these rects, tho I don't remember how :) Thanks in advance!

EDIT: null asked me to clarify the case, so here's the whole picture:

I've got: - triangle-like MC (since a triangle MC is a rectangle for flash anyway, I've solved this by creating a pyramid of 33 rectangles, layered on each other); - toys (multiple frames to change whenever I need to); - text field (to input the quantity of desired toys); - confirm button (to make the magic happen once clicked);

Now I need a user to input a 3-digit number in the input field, press "confirm", and 0.4 of that quantity is supposed to appear in the "triangle-like MC". For example: user inputs a number: 600, the end value is 600*0.4=240. Now 240 "toys" are randomly spreaded between my 33 rectangles, within their width (which is different for every single one).

Hope that explains a bit more.

A simplified sample of what I need here.

1
"triangle-like MC (since a triangle MC is a rectangle for flash anyway, I've solved this by creating a pyramid of 33 rectangles, layered on each other);" you seem to have some very strange understanding of what's going on. No, triangular vector data is not a rectangle anyway. If you want a triangle, just draw a triangle. Please, take yet again another step back and exclude your solution to your problem. Don't say how you have done it or how you think it should be done, but explain the original goal. Apparently you want to place some objects on a triangle. Please elaborate more on that. - null
Maybe it'd be easier to understand if there was an image of the desired result. - null
Added, please check. - Simeon Sergeev
That helps, but the triangle thing is still unclear: in that other question you asked about positioning objects in a triangle, but for some reason it looks like you are using some stack of rectangles now that form a triangle, state that fits your need better but at the same time admit that you could not make your objects appear in a triangle properly (but that other question has an accepted answer!?) So what's going on here: is that rectangle stack what you really want (if so, explain how it works) or is it just some workaround because you couldn't get a triangle to work? - null
In other words: I'm not sure what solution you are pursuing at the moment. I don't think it's a good idea to keep on fixing the rectangle workaround if what you truly want is a real triangle. Which one do you want? - null

1 Answers

2
votes

Is there a way to fill an array with MovieClips rather than String values? That would be THE answer to this question here.

There is in fact more than one way:

  • Place all the instance names into the Array when you create it:

    var rectangles:Array = [instanceName1, instanceName2];
    

    there are no quotation marks, which create string literals. Just the names.

    This approach quickly becomes impractical for large numbers of objects.

  • Given that the instance names have a number component, iterate through the names in conjunction with getChildByName(). I assume that this is what you were trying with in your question:

    var rectangles:Array = []; // initialise empty array
    
    for (var j:int=0; j<33; j++){ 
        rectangles.push(getChildByName("tPl_" + j));
    }
    

original answer

 toysPlc.push("tPl_" + j); //here I add that bunch of rects into an array

No you don't. You are filling the Array with String objects. It's totally unrelated to any rectangle whatsoever.

Now this next line, tries to call a function on each String, which fails.

toysPlc[p].addChild(toy);

The above is equivalent to

"tPl_0".addChild(toy);
"tPl_1".addChild(toy);
// etc.

A String doesn't have that method addChild.