Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

ActionsScript 3:0: Mouse event listeners not firing second time

Avatar of turpya
turpya asked on
Adobe Flash
2 Comments1 Solution525 ViewsLast Modified:
I have some ActionScript I have spent the weekend working on (I am a relative newbie to actionscript), and have come stuck with mouse event listeners.

The first listener fires when an imported bitmap is clicked on. When clicked, a motion moves the bitmap to an area centre of stage. Then a second listener is setup so that when the image is clicked on, it returns to it's original position.

After this, if you click on the image back in it's starting position, nothing happens.

How can I reload/reinitialise this code, so that the listener's continually fire when the image is clicked on? I have tried removeEventListener/addEventListener in the onTweenComplete function, but this appears to work sporadically (sometimes you have to click multiple times to get the image to move).

Example code would be really(!) appreciated.

Thanks.
import soulwire.display.PaperSprite;
import gs.TweenMax;
import gs.events.TweenEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
 
var myPaperSprite:PaperSprite = new PaperSprite();
var myBitmap:Bitmap = new Bitmap();
 
var url1:String="p1.png";
var url2:String="p1u.png";
var size:uint=80;
 
var finalx:Number;
var finaly:Number;
var speed:Number;
 
var originx:Number;
var originy:Number;
var originz:Number;
 
myPaperSprite.pivot=new Point(0.5,0.5);
 
LoadBitmaps();
 
addChild( myPaperSprite );
 
myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandler);
 
setupBitmapPosition();
 
 
 
function setupBitmapPosition() {
	finalx=500;
	finaly=500;
	speed=50;
 
	originx=1700;
	originy=-164;
	originz=2200;
 
	myPaperSprite.x=originx;
	myPaperSprite.y=originy;
	myPaperSprite.z=originz;
 
	
	
	
}
 
function onClickHandler(event:MouseEvent):void {
	var speed=1;
 
	var myTween:TweenMax=new TweenMax(myPaperSprite,speed,{x:finalx,y:finaly,z:-200,shortRotation:{rotationX:900,rotationY:0}});// , rotationZ:-1000
	myTween.addEventListener(TweenEvent.COMPLETE, onTweenComplete);
 
	function onTweenComplete(event:TweenEvent=null):void {
		//myTween.clear();
		//myPaperSprite.removeEventListener(MouseEvent.CLICK, onClickHandlerReturn);
		//myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandlerReturn);
		myTween.removeEventListener(TweenEvent.COMPLETE, onTweenComplete);
	}
 
	myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandlerReturn);
 
	function onClickHandlerReturn(event:MouseEvent):void {
 
		var speed=1;
 
		var myTween:TweenMax=new TweenMax(myPaperSprite,speed,{x:originx,y:originy,z:originz,shortRotation:{rotationX:0,rotationY:0}});// , rotationZ:-1000
		myTween.addEventListener(TweenEvent.COMPLETE, onTweenComplete);
 
		function onTweenComplete(event:TweenEvent=null):void {
			//myTween.clear();
			//loadMovie("products.swf",_root);
			//myPaperSprite.removeEventListener(MouseEvent.CLICK, onClickHandler);
			//myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandler);
			myTween.removeEventListener(TweenEvent.COMPLETE, onTweenComplete);
			//myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		myPaperSprite.addEventListener(MouseEvent.CLICK, onClickHandler);
		
	}
 
}
 
function LoadBitmaps() {
	myPaperSprite.x=1;
	myPaperSprite.y=1;
	myPaperSprite.z=1;
	configureAssets1();
 
	configureAssets2();
 
	function configureAssets1():void {
		var loader:Loader = new Loader();
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler1);
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler1);
 
		var request:URLRequest=new URLRequest(url1);
		loader.x=size*numChildren;
		loader.load(request);
		addChild(loader);
 
		//myBitmap = Bitmap(loader.content);
		//addChild(myBitmap);
 
 
	}
 
	function configureAssets2():void {
		var loader:Loader = new Loader();
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler2);
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler2);
 
		var request:URLRequest=new URLRequest(url2);
		loader.x=size*numChildren;
		loader.load(request);
		addChild(loader);
 
		//myBitmap = Bitmap(loader.content);
		//addChild(myBitmap);
 
 
	}
 
	function duplicateImage(original:Bitmap):Bitmap {
		var image:Bitmap=new Bitmap(original.bitmapData.clone());
		image.x=size*numChildren;
		addChild(image);
		return image;
	}
 
	function completeHandler1(event:Event):void {
		var loader:Loader=Loader(event.target.loader);
		var image:Bitmap=Bitmap(loader.content);
 
				myPaperSprite.front=duplicateImage(image);
		removeChild(loader);
	}
 
	function completeHandler2(event:Event):void {
		var loader:Loader=Loader(event.target.loader);
		var image:Bitmap=Bitmap(loader.content);
 
				myPaperSprite.back=duplicateImage(image);
		removeChild(loader);
 
 
	}
	function ioErrorHandler1(event:IOErrorEvent):void {
		trace("Unable to load image: " + url1);
	}
	function ioErrorHandler2(event:IOErrorEvent):void {
		trace("Unable to load image: " + url2);
	}
}
ASKER CERTIFIED SOLUTION
Avatar of Gary Benade
Gary BenadeFlag of South Africa image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers