Hello guys!
I am very new to Flash and AS3 so I am sorry if my question is too obvious but I cannot figure out what is going on.
Basically what I want is to import random sprites from the library to the stage by calling them dynamically. The sprites have different shapes and colors. For example there are circles, squares, triangles etc with the colors blue, red, green, yellow, pink, grey and brown.
I have done the random import for one image which is easy using the Math.random() method but I want to run a loop so everytime (for example ) a Blue Circle appears, I want to erase from the array all the sprites that are either Blue or Circles. So in this example where a Blue Circle appears, the Blue Square should be erased and also the red, green, yellow, pink, grey and brown circles.
I use a TimerEvent every one second.
And this TimerEvent uses an update function.
The problem is that every time “for” is run it seems to skip certain elements from the array. And if a Blue Circle appears, only the blue, green, yellow and pink circles are erased and the red, grey and brown ones are not. Do you have any idea why this might be the case?
My code is the following.
(Btw, I know I can put them all in a big or but since what I want to do is not working I wanted to try them out separately first. That’s why that’s a big if.
function update (event:TimerEvent):void
{
//randomIndex
var randomIndex: uint = Math.floor( Math.random()*images.length);
stage.addChild(images[randomIndex]);
var mrx:Number = Math.random()* (stage.stageWidth - images[randomIndex].width/2);
var mry:Number = Math.random()* (stage.stageHeight- images[randomIndex].height/2);
while ((mrx > 800) || (mrx < 150))
{
mrx = Math.random()* (stage.stageWidth - images[randomIndex].width/2);
}
while ((mry > 600) || (mry < 150))
{
mry = Math.random()* (stage.stageHeight- images[randomIndex].height/2);
}
images[randomIndex].x = mrx;
images[randomIndex].y = mry;
var animage:Object = images[randomIndex];
var aname:String = animage.name;
if ( aname.indexOf("Circle")>0 )
{
if (aname.indexOf("Blue")>0 )
{
for (var i:uint = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Blue")>0) )
{
trace("remove the shape " + images*.name);
images.splice(i,1);
}//end if
}//end for
}
else if (aname.indexOf("Brown")>0)
{
for (i = 0; i< images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Brown")>0) )
{
trace("remove the shape" + images*.name);
images.splice(i,1);
}
}
}
else if (aname.indexOf("Green")>0)
{
for (i = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Green")>0) )
{
trace("remove the shape " + images*.name);
images.splice(i,1);
}
}
}
else if (aname.indexOf("Grey")>0)
{
for (i = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Grey")>0) )
{
trace("remove the shape " + images*.name);
images.splice(i,1);
}
}
}
else if (aname.indexOf("Pink")>0)
{
for (i = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Pink")>0) )
{
trace("remove the shape " + images*.name);
images.splice(i,1);
}
}
}
else if (aname.indexOf("Red")>0)
{
for (i = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Red")>0) )
{
trace("afairesi tou sximatos " + images*.name);
images.splice(i,1);
}
}
}
else if (aname.indexOf("Yellow")>0)
{
for (i = 0; i<images.length; i++)
{
if ((images*.name.indexOf("Circle")>0) || (images*.name.indexOf("Yellow")>0) )
{
trace("remove the shape " + images*.name);
images.splice(i,1);
}
}
}
}//end else circle
}//end update
The initialization of the sprites and the array happens above this function and it’s the following.
var cb = new CircleBlue; cb.name = " Circle Blue";
var cbr = new CircleBrown; cbr.name = " Circle Brown";
var cg = new CircleGreen; cg.name = " Circle Green";
var cgr = new CircleGrey; cgr.name = " Circle Grey";
var cp = new CirclePink; cp.name = " Circle Pink";
var cr = new CircleRed; cr.name = " Circle Red";
var cy = new CircleYellow; cy.name = " Circle Yellow";
var images:Array = [cb, cbr, cg, cgr, cp, cr, cy]
There are 9 more shapes but I didn’t add them cos the code would be very big. There are no errors and the code partly works but doesn’t do what I really want to.
Thank you so much for your time and your help!