Solved

this , in an event dispatch

Posted on 2009-05-16
4
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 19

Expert Comment

by:Gary Benade
ID: 24405987
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
ID: 24426956
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
ID: 24429025

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
ID: 24429029
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Flash movie or gif/animation? 3 944
Windows 7, Adobe Flash Player and Internet Explorer 11 15 106
Include Adobe AIR libraries in Flash Builder 4 139
Play RTMP without Flash 3 772
Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

726 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