actionscript 3 - Custom Event Listener not working -


i new as3 , saw creation of custom events in as3 in tutorial , wanted incorporate in game. when did tutorial, seemed project. doesnt seem work new project.

here code :

package   {    	import flash.display.movieclip;  	import flash.utils.timer;  	import flash.events.timerevent;    	public class fashionfrenzy extends movieclip  	{  		  		public var buyer_mc:buyer;  		public var buyers:array;  		public var gametimer:timer;    		public function fashionfrenzy()  		{  			  			gametimecontroller();  			generatebuyers();  			addeventlistener(reachmalldoorevent.check, onreachmalldoor);    		}    		  		public function gametimecontroller()  		{  			gametimer = new timer( 25 );  			gametimer.start();  		}  		public function generatebuyers()  		{  			buyers = new array  ;  			buyer_mc = new buyer(533.2,0)  ;  			addchild(buyer_mc);  			gametimer.addeventlistener( timerevent.timer, buyerenter );  			  			  			if(buyer_mc.y==377.25)  			{  				dispatchevent( new reachmalldoorevent( reachmalldoorevent.check ) );  			}  					  			  			  		}  		  		public function buyerenter(event:timerevent)  		{    			buyer_mc.enter();  		}  		  		public function onreachmalldoor(event:reachmalldoorevent)  		{  			  			trace("my timer starts now");  		}  		  		    	}    }

here, onreachmalldoor never seems run because there wrong. cant see output saying "my timer starts now". there no error in code , output doesnt show runtime errors either. have gone wrong? want onreachmalldoor function run when y coordinate in desirable position , event dispatched.

the order of commands wrong.

generatebuyers(); addeventlistener(rea... 

the first line of these 2 1 potentially cause event dispatched. after start listening it. that's late. have start listening before event dispatched.

the probability of event dispatched low.

buyer_mc.y==377.25 

checking floating point values equality not idea. off due rounding errors etc. if .y property controlled mouse example, you'd have position mouse @ position, unlikely.

you dispatch event @ beginning.

generatebuyers(); 

that function called once. .y position evaluated. happens once , never again. .y position subject change , condition should evaluated again, doesn't happen

the structure not helpful.

it doesn't make sense object listen own events. call function , done it.

events communication between objects.


how supposed be:

the point of custom event notified something. want notified when condition

buyer_mc.y==377.25 

is true. if evaluating condition way now, there's no point in receiving notification result thereof. have already.

instead, buyer_mc should dispatch event. condition should checked in buyer class.

what code looks like

some snippets pointing out above means, code untested:

class buyer

override public function set y(value:number):void {     if (value == 377.25)         dispatchevent(new reachmalldoorevent(reachmalldoorevent.check)); // i'm @ position, send out notification     super.y = value; } 

class fashionfrenzy

buyer = new buyer(533.2, 0); // variables should start small letter buyer.addeventlistener(reachmalldoorevent.check, onreachmalldoor); 

if set .y position value, object dispatch event. figure out on own.

letting object figure out on own , receive notification main reason use custom events.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -