-1

i get Error #1010 when i use this while loop:

while (pos.length>0)
{
    coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];

    (pos_array[index]).x = coo.x;
    (pos_array[index]).y = coo.y;
    index++;
}

The error says: A term is undefined and has no properties.

What is wrong with my loop because I used the same loop for other programms and i got no such error.

Thank you for your attention.

7
  • Can't tell from this code. 'pos', 'pos_array', any element in 'pos_array', any element in 'pos' and 'index' could all be undefined.
    – RIAstar
    Oct 28, 2011 at 12:04
  • they are all defined: var coo:Object: var pos_array:Array= new Array(); var index:uint=0;
    – CMS
    Oct 28, 2011 at 12:05
  • What about the elements in 'pos' and 'pos_array'? On what line is the error thrown? You don't have a debugger at your disposal?
    – RIAstar
    Oct 28, 2011 at 12:12
  • everything is fine. I used the debugger to see which line it is. and the error is in the loop. but don't know where
    – CMS
    Oct 28, 2011 at 12:18
  • Does the element in 'pos_array' already exist when you set its 'x' property? e.g. when you do '(pos_array[0]).x = coo.x', is there an instance of a class with attribute x at that position in the array?
    – RIAstar
    Oct 28, 2011 at 12:42

3 Answers 3

0

Your while loop is breaking.

pos.length will never change and eventually pos_array[index] will be out of bounds.

When when you are out of bounds it is undefined. So basically you are doing.

undefined.x = coo.x;

And just like the error says undefined has has no properties.

I can't see how this loop ever worked.

Try this instead much cleaner

var savedX:Number = 0
for each( var obj:Object in pos_array ){
  coo = new MovieClip()
  coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
  obj.x = savedX;
  obj.y = 0;
  savedX += coo.width;
}
8
  • i tryed that and I got no error but the mc's are not arranged side by side like they used to be. they are now stacked. how can i arrange them side? i have allready stored positions in array pos
    – CMS
    Oct 29, 2011 at 16:19
  • your logic is flawed. you are using an array to store positions which is fine however, you accessing them randomly which has the possibility of using one position more then once. you need to remove the element from the pos array once you have "used" that position.
    – The_asMan
    Oct 31, 2011 at 15:21
  • i would do it like this: for each( var obj:Object in pos_array ){ coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0]; obj.x+=loader.width/4; obj.y =0; if(x>loader.width) { x=0; y=loader.height/2; } }
    – CMS
    Oct 31, 2011 at 16:47
  • I'm not sure that code will do what you are expecting it to do. I would start with verifying the values of pos and make sure they are all good locations.
    – The_asMan
    Oct 31, 2011 at 17:20
  • ok can you tell me a way to do this. I have stored some mc's in an array and i want to put these mc's on the stage side by side. how can I do that? thank you
    – CMS
    Oct 31, 2011 at 17:33
0

Without knowing what the collection contains, I'm assuming it is filled with DisplayObjects or an object that has x and y properties?

Cast the reference so the compiler understands what the collection contains. For example:

DisplayObject(pos_array[index]).x = coo.x;
DisplayObject(pos_array[index]).y = coo.y;

...or whatever type your collection contains.

2
  • if i change the loop condition to : "while(pos.length==0)" it works
    – CMS
    Oct 29, 2011 at 16:05
  • yes the array contains movie clips but this is not the solution because i get a new error " undefined method"
    – CMS
    Oct 29, 2011 at 16:08
0

Perhaps pos.length and pos_array.length are not equal when the loop starts.

Try this:

while (pos.length>0)
{

    coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
    if (pos_array[index])
    {
        (pos_array[index]).x = coo.x;
        (pos_array[index]).y = coo.y;
    }
    index++;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.