package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.Event;
import flash.display.*;
public class item extends Sprite {
private var wide:int =0;
private var high:int =0;
private var xPos:int =0;
private var yPos:int =0;
private var imgLoader:ImageLoader;
public var loaded:Boolean = false;
private var image:Bitmap;
private var image2:Bitmap;
public var fileName:String = "";
private var mouseHover:Boolean = false;
private var itemList = [];
////////////////////////////////////////////////////Constructor
public function item(xPosT:int, yPosT:int, a:int, b:int, imgName:Array) {
wide =a;
high =b;
xPos = xPosT;
yPos = yPosT;
fileName = imgName[0];
addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
addEventListener(MouseEvent.CLICK, clickHandler);
imgLoader=new ImageLoader();
imgLoader.addEventListener(ImageLoader.IMGS_LOADED,allLoaded);
imgLoader.loadImgs(imgName);
addEventListener("inspectYourChildren", inspectAndKillRelay);
}
///////////////////////////////////////////////////Helper Methods
public function findChildLocation():Array {
var totalHeight:int =yPos;
for (var i:int =0; i<itemList.length-1; i++) {
totalHeight += itemList[i].getHeight();
}
return [getXPos() + getWidth(),totalHeight];
}
public function inspectAndKillRelay(e:Event) {
trace("A child of " + fileName + " asked for an inspection.");
inspectAndKill();
}
public function inspectAndKill() {
trace(fileName + " Inspect and Kill ");
var anyFound:Boolean = isMouseAbove();
//trace(" Value of calling object is " + anyFound);
// for (var i:int =0; i<itemList.length; i++) {
// if (itemList[i].isMouseAbove()) {
// trace(fileName + "NO KILL b/c of " + itemList[i].fileName);
// anyFound=true;
// break;
// }
// }
if (!anyFound) {
trace(fileName + "YES KILL");
for (var ii:int =0; ii<itemList.length; ii++) {
if (contains(itemList[ii])) {
removeChild(itemList[ii]);
}
}
}
}
public function isMouseAbove() {
if (xPos<=mouseX&&mouseX<xPos+wide) {
if (yPos<=mouseY&&mouseY<yPos+high) {
return true;
}
}
if (itemList.length!=0) {
for (var i:int =0; i<itemList.length; i++) {
if (itemList[i].isMouseAbove()) {
trace(fileName + "NO KILL b/c of " + itemList[i].fileName);
return true;
}
}
}
trace(fileName + " - KILL");
return false;
}
////////////////////////////////////////////////////Handles
public function setWidth(a:int):void {
wide = a;
}
public function getWidth():int {
return wide;
}
public function setHeight(a:int):void {
high = a;
}
public function getHeight():int {
return high;
}
public function setXPos(a:int) {
xPos = a;
}
public function getXPos():int {
return xPos;
}
public function setYPos(a:int) {
yPos = a;
}
public function getYPos():int {
return yPos;
}
public function addSubItem(newThing:item):void {
itemList[itemList.length] = newThing;
var newPositions = findChildLocation();
itemList[itemList.length - 1].setXPos(newPositions[0]);
itemList[itemList.length - 1].setYPos(newPositions[1]);
itemList[itemList.length - 1].addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
}
////////////////////////////////////////////////////Event Listener stuff
public function mouseOverHandler(event:MouseEvent):void {
image2.alpha = 1;
trace("Event - OVER - "+fileName);
for (var i:int =0; i<itemList.length; i++) {
addChild(itemList[i]);
}
}
public function mouseOutHandler(event:MouseEvent):void {
image2.alpha = 0;
trace("Event - OUT - "+fileName);
trace("----------Inspect Self----------");
inspectAndKill();
trace("----------Inspect Children----------");
dispatchEvent(new Event("inspectYourChildren"));// launch parent inspection
trace("----------End Inspection----------");
}
public function clickHandler(event:MouseEvent):void {
trace("Event - CLICK - "+fileName);
}
private function allLoaded(e:Event):void {
loaded = true;
image = Bitmap(imgLoader.bitmapsArray[0]);
image.x = xPos;
image.y = yPos;
image.width = wide;
image.height = high;
addChild(image);
image2 = Bitmap(imgLoader.bitmapsArray[1]);
image2.x = xPos;
image2.y = yPos;
image2.width = wide;
image2.height = high;
image2.alpha = 0;
addChild(image2);
trace("Event - LOADED - "+fileName);
}
}
}