PDA

View Full Version : Loaded object won't accept mouse clicks



iwanczuk
July 16th, 2008, 06:58 PM
I'd appreciate any advice on why the following code won't work. The problem is that the eventListeners for mouse clicks are not being acted upon. Thanks in advance...

package {
import flash.events.*;
import flash.net.*;
import flash.display.*;

public class test extends Sprite {

public var currentAssembly = new Sprite;
public var currentAssemblyLoader:Loader = new Loader();
public var currentAssemblyURL:URLRequest = new URLRequest();
public var loadedAssemblyArray:Array = new Array();

public var currentAssemblyPointer= new int;

public function test():void {
currentAssemblyLoader.contentLoaderInfo.addEventLi stener(Event.INIT, assemblyLoaded, false, 0, true);
currentAssemblyLoader.contentLoaderInfo.addEventLi stener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
currentAssemblyLoader.contentLoaderInfo.addEventLi stener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
currentAssemblyLoader.load(new URLRequest("http://panelec.twoslackers.com/graphics/CBAT1L1.swf") );
}

public function assemblyLoaded(evt:Event):void {
var theSWF:DisplayObject = evt.target.content;

var index = new int;

currentAssemblyLoader.unload();
index = loadedAssemblyArray.length;
loadedAssemblyArray[index] = theSWF;
loadedAssemblyArray[index].x = 10;
loadedAssemblyArray[index].y = 10;
loadedAssemblyArray[index].scaleX = .75;
loadedAssemblyArray[index].scaleY = .75;
loadedAssemblyArray[index].addEventListener(MouseEvent.MOUSE_DOWN, currentAssemblyMouseDown, false, 0 , true);
loadedAssemblyArray[index].addEventListener(MouseEvent.MOUSE_UP, currentAssemblyMouseUp, false, 0 , true);
addChild(loadedAssemblyArray[index] );
}

function currentAssemblyMouseDown(evt:Event):void {
currentAssemblyLoader.load(new URLRequest("http://panelec.twoslackers.com/graphics/CBAT1L1.swf") );
evt.target.startDrag();
trace("click");
}

function currentAssemblyMouseUp(evt:Event):void {
evt.target.stopDrag();
}

public function loadProgress(evt:ProgressEvent):void {
trace("loading");
}

public function onIOError(evt:IOErrorEvent):void {
trace("Unable to load data.");
}
}
}

iwanczuk
July 20th, 2008, 10:38 PM
Found the solution, but I still don't know why the original code didn't work. The solution was to move the variable currentAssembly from being a public variable to being a private variable of the function assemblyLoaded().