Solved

this , in an event dispatch

Posted on 2009-05-16
4
258 Views
Last Modified: 2013-11-11
Hi,

Imagine I have an FLA with a movieclip and inside it there is another movieclip called button_mc, the main movieclip is associated with class a.


for class a II will write an external class, which goes something like the one attached.

when button_mc.on_release is dispatched the event handler can use the class variables or call to a class function..

the problem is the this keyword now relates to button_mc inside the event handler and even inside the class functions that get called by button_mc.onRelease..

this takes out the benefit of using the this keyword and makes the code invalid.... since it is wrongly addresses another movieclip then the one intended to...

the only solution I found was to replace the this keyword with an absolute addressing... but that is ugly..

what is the correct way to handle this ?
class a extends movieclip

{

	private var var1:Number
 
 

	function a ()

	{

		this.button_mc.onRelease = _handle_button_mc_onRelease;

	}

	

	function a2 (n)

	{

		this.var1 = n;

	}

	

	function _handle_button_mc_onRelease ()

	{

		// this wont work anymore

		

		var i = this.var1;

		

		// this call will not work and even if it did

		// a2 inner code would not work because of the 

		// this keyword

		

		this.a2 (i);

	}

}

Open in new window

0
Comment
Question by:moshem
  • 3
4 Comments
 
LVL 19

Expert Comment

by:Gary Benade
Comment Utility
I never did like the way AS2 event handlers work, AS3 acts as you would expect
class a extends movieclip

{

	private var var1:Number =1;

 

 

	this.button_mc.onRelease = _handle_button_mc_onRelease;

	

	

	function a2 (n)

	{

		this.var1 = n;

	}

	

	function _handle_button_mc_onRelease ()

	{

		var i = var1;

		a2( i+1);

		trace( i); // 1

		trace(var1); // 2

	}

}

Open in new window

0
 
LVL 1

Author Comment

by:moshem
Comment Utility
so that's it ?

AS2 behave just like this and creates a mess of events ?

what's the workaround ? what is the recommended way to handle this ?

(except for moving to AS3) - this is a problem since I need to teach the designers to know to add simple onrelease event handlers first..
0
 
LVL 19

Accepted Solution

by:
Gary Benade earned 500 total points
Comment Utility

import mx.utils.Delegate;
 

class a extends movieclip

{

        private var var1:Number

 

 

        function a ()

        {

                this.button_mc.onRelease = Delegate.create(this, _handle_button_mc_onRelease);

        }

        

        function a2 (n)

        {

                this.var1 = n;

        }

        

        function _handle_button_mc_onRelease ()

        {

                // this now points to the classes scope thanks to Delegate

                

                var i = this.var1;

                

                this.a2 (i);

        }

}

Open in new window

0
 
LVL 19

Expert Comment

by:Gary Benade
Comment Utility
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now