Thursday, 13 October 2011

Array scripting demo

var blockArray:Array=new Array();


for(var i:int = 0; i < 15; i++) {
//Will duplicate the cube 15 times
var cube:MovieClip = new Box();
//This is looking for a new box in the libary
cube.x=Math.random()*550
//This is loading new cubes randomly on the x axis
cube.y=Math.random()*400
//This is loading new cubes randomly on the y axis, this works alongside the x axis too
addChild(cube);
//This is bringing a duplicate of the cube from the libary onto the stage

blockArray[i]=cube;

}

stage.addEventListener(Event.ENTER_FRAME, checkhit);
//Listen to the stage on every frame
function checkhit(myevent:Event):void {
for (var i:int = 0; i < 15; i++) {
//Creates a loop to create blocks 15 times.
if (blockArray[i].hitTestPoint(mouseX,mouseY,true)){
//If any of the cubes get hit by the mouse in it's x or y direction, true means the i from blockarray is following the cubes, so the cube that is hit by the mouse will cause the transparency(alpha) down.
blockArray[i].alpha=.3;

}
}

}

No comments:

Post a Comment