[Flashcoders] AS 2 Delegate question

2007-06-02 Thread Stefano Nappa

Hi, a question.


I'm talking about AS 2.





Inside a method of a class I've:





btnCollection.onRollOver = function() {


// here this is the button


var t = Number(this._name.substr(1, 1));


//


Mouse.hide();


//


cRef.cursor._visible = true;


startDrag(cRef.cursor, true);


//


cRef.setRotation(cRef.curPos[t]);


};





btnCollection is previously created (in the same method), this way:





for (i=0; ibtns; i++) {


var btnCollection = targetLevel['c'+i+'_btn'];


}





cRef is previously created this way:


var cRef = this; // it stores a reference to the class.




The var t registers a number contained in a button name (I use that
number to perform various tasks).





This is perfectly working, but now I want to use the Delegate class.





So, I did the following:





btnCollection.onRollOver = Delegate.create(this,
btnRollOver(btnCollection));





after having built this method:





private function btnRollOver(whichBtn) {


var t = Number(whichBtn._name.substr(1, 1));


//


Mouse.hide();


//


cursor._visible = true;


startDrag(cursor, true);


//


setRotation(curPos[t]);


}





nothing works like before. 


That is: I probably easily get the class reference but I've lost the
reference to the single button...




I even tried (with no results):

1. btnCollection.onRollOver = Delegate.create(this,
btnRollOver, btnCollection);

2. changing btnCollection to be a class variable (not a local one);




Please, can you help me better understand this issue? What am I doing wrong?





Thanks!







___ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: 
http://it.docs.yahoo.com/nowyoucan.html
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Ruy Adorno
Well, what I understand about the Delegate class, is that you canĀ“t send 
any parameter through the Delegate.create method, any parameter you want 
to pass, must be send by the event.


btnCollection.onRollOver = Delegate.create(this, btnRollOver);


In this case the delegate will send to the btnRollOver function only 
the parameters of the event onRollOver.
You will have to find another way to solve this problem... maybe extend 
the MovieClip class, or maybe it could be better to not use the delegate.


I hope this could help.
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Merrill, Jason
 In this case the delegate will send to the btnRollOver 
function only the parameters of the event onRollOver.
You will have to find another way to solve this problem... 
maybe extend the MovieClip class, or maybe it could be better 
to not use the delegate.

Actually, you can do it this way:

import mx.utils.Delegate

var myDel:Object = myButton.onRelease = Delegate.create(this,
myFunction);
myDel.myVariable = Hello World;  

function myFunction()
{
trace(arguments.caller.myVariable)
}

Jason Merrill
Bank of America  
GTO Learning  Leadership Development
eTools  Multimedia Team

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Alain Rousseau
Or you could use another Delegate class. There is the Proxy class I 
believe (can't remember who did it)  and the one I use the most is the 
one on dynamicflash.com

http://dynamicflash.com/2005/05/delegate-version-101/

You can pass parameters in the Delegate call


myButton.onRelease = Delegate.create(this, myFunction, param1, param2, ...);



Merrill, Jason wrote:
In this case the delegate will send to the btnRollOver 
function only the parameters of the event onRollOver.
You will have to find another way to solve this problem... 
maybe extend the MovieClip class, or maybe it could be better 
to not use the delegate.
  


Actually, you can do it this way:

import mx.utils.Delegate

var myDel:Object = myButton.onRelease = Delegate.create(this,
myFunction);
myDel.myVariable = Hello World;  


function myFunction()
{
trace(arguments.caller.myVariable)
}

Jason Merrill
Bank of America  
GTO Learning  Leadership Development

eTools  Multimedia Team

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


  

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Muzak
Create a custom Button component that has public methods and properties that 
you can access.
Have your custom Button dispatch a click event, just like the v2 Button 
component does.

Others might suggest to use a Proxy class that lets you pass extra parameters, 
I'd suggest not to do so.

Simply create your own Button and you'll have all the power you need.

regards,
Muzak

- Original Message - 
From: Stefano Nappa [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Saturday, June 02, 2007 5:33 PM
Subject: [Flashcoders] AS 2 Delegate question



 Hi, a question.


 I'm talking about AS 2.

 Inside a method of a class I've:



 btnCollection.onRollOver = Delegate.create(this, btnRollOver(btnCollection));
 after having built this method:

 private function btnRollOver(whichBtn) {
var t = Number(whichBtn._name.substr(1, 1));
//
Mouse.hide();
//
cursor._visible = true;
startDrag(cursor, true);
//
setRotation(curPos[t]);
}


 nothing works like before.


 That is: I probably easily get the class reference but I've lost the
 reference to the single button...

 I even tried (with no results):

 1. btnCollection.onRollOver = Delegate.create(this,
 btnRollOver, btnCollection);

 2. changing btnCollection to be a class variable (not a local one);

 Please, can you help me better understand this issue? What am I doing wrong?

 Thanks!


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Jesse Graupmann
Muzak,

You bring this up every time proxy is mentioned. Do you mind sharing an
actual code example that is just as quick and easy? 

Hasta,
Jesse


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: Saturday, June 02, 2007 2:53 PM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] AS 2 Delegate question

Create a custom Button component that has public methods and properties that
you can access.
Have your custom Button dispatch a click event, just like the v2 Button
component does.

Others might suggest to use a Proxy class that lets you pass extra
parameters, I'd suggest not to do so.

Simply create your own Button and you'll have all the power you need.

regards,
Muzak

- Original Message - 
From: Stefano Nappa [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Saturday, June 02, 2007 5:33 PM
Subject: [Flashcoders] AS 2 Delegate question



 Hi, a question.


 I'm talking about AS 2.

 Inside a method of a class I've:



 btnCollection.onRollOver = Delegate.create(this,
btnRollOver(btnCollection));
 after having built this method:

 private function btnRollOver(whichBtn) {
var t = Number(whichBtn._name.substr(1, 1));
//
Mouse.hide();
//
cursor._visible = true;
startDrag(cursor, true);
//
setRotation(curPos[t]);
}


 nothing works like before.


 That is: I probably easily get the class reference but I've lost the
 reference to the single button...

 I even tried (with no results):

 1. btnCollection.onRollOver = Delegate.create(this,
 btnRollOver, btnCollection);

 2. changing btnCollection to be a class variable (not a local one);

 Please, can you help me better understand this issue? What am I doing
wrong?

 Thanks!


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Muzak
This isn't about quick and easy, but about encapsulation, OOP and best 
practices.
Quick and easy also often means quick and dirty ;-)

Here's a stripped down version of a custom button.

import mx.events.EventDispatcher;
import mx.utils.Delegate;

class com.muzakdeezign.samples.MyButton extends MovieClip {
 // decorate class with EventDispatcher
 private static var dispatcherInit = 
EventDispatcher.initialize(com.muzakdeezign.samples.MyButton.prototype);

 // declare EvenDispatcher methods
 public var addEventListener:Function;
 public var removeEventListener:Function;
 public var dispatchEvent:Function;

 private var back_mc:MovieClip;

 private var __index:Number;

 function MyButton() {
  this.init();
 }

 private function init():Void {
  this.back_mc.onRelease = Delegate.create(this, this.backReleaseHandler);
 }

 private function backReleaseHandler():Void {
  this.dispatchEvent({type:click});
 }

 private function setIndex():Void {
 }

 function get index():Number {
  return this.__index;
 }
 function set index(val:Number):Void {
  this.__index = val;
  this.setIndex();
 }

}

Here's a screencast making the above:
http://muzakdeezign.com/flashcoders/create_component/MyButton.html

regards,
Muzak


- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Sunday, June 03, 2007 1:43 AM
Subject: RE: [Flashcoders] AS 2 Delegate question


 Muzak,

 You bring this up every time proxy is mentioned. Do you mind sharing an
 actual code example that is just as quick and easy?

 Hasta,
 Jesse


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Jesse Graupmann
Ha! That's rad. 

I guess the trick is writing a Flash app to write the bulk of the code...
Got links to any of those? 

;)


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: Saturday, June 02, 2007 5:54 PM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] AS 2 Delegate question

This isn't about quick and easy, but about encapsulation, OOP and best
practices.
Quick and easy also often means quick and dirty ;-)

Here's a stripped down version of a custom button.

import mx.events.EventDispatcher;
import mx.utils.Delegate;

class com.muzakdeezign.samples.MyButton extends MovieClip {
 // decorate class with EventDispatcher
 private static var dispatcherInit =
EventDispatcher.initialize(com.muzakdeezign.samples.MyButton.prototype);

 // declare EvenDispatcher methods
 public var addEventListener:Function;
 public var removeEventListener:Function;
 public var dispatchEvent:Function;

 private var back_mc:MovieClip;

 private var __index:Number;

 function MyButton() {
  this.init();
 }

 private function init():Void {
  this.back_mc.onRelease = Delegate.create(this, this.backReleaseHandler);
 }

 private function backReleaseHandler():Void {
  this.dispatchEvent({type:click});
 }

 private function setIndex():Void {
 }

 function get index():Number {
  return this.__index;
 }
 function set index(val:Number):Void {
  this.__index = val;
  this.setIndex();
 }

}

Here's a screencast making the above:
http://muzakdeezign.com/flashcoders/create_component/MyButton.html

regards,
Muzak


- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Sunday, June 03, 2007 1:43 AM
Subject: RE: [Flashcoders] AS 2 Delegate question


 Muzak,

 You bring this up every time proxy is mentioned. Do you mind sharing an
 actual code example that is just as quick and easy?

 Hasta,
 Jesse


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Muzak
Don't know of anything like this available out there, doesn't mean there isn't.
What you saw is part of a larger app that spits out a complete ARP application 
and actually writes .as files to disk (you now saw me 
copy/paisting the code into Flash).

regards,
Muzak

- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Sunday, June 03, 2007 3:22 AM
Subject: RE: [Flashcoders] AS 2 Delegate question


 Ha! That's rad.

 I guess the trick is writing a Flash app to write the bulk of the code...
 Got links to any of those?

 ;)


 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Muzak
 Sent: Saturday, June 02, 2007 5:54 PM
 To: flashcoders@chattyfig.figleaf.com
 Subject: Re: [Flashcoders] AS 2 Delegate question

 This isn't about quick and easy, but about encapsulation, OOP and best
 practices.
 Quick and easy also often means quick and dirty ;-)

 Here's a stripped down version of a custom button.

 import mx.events.EventDispatcher;
 import mx.utils.Delegate;

 class com.muzakdeezign.samples.MyButton extends MovieClip {
 // decorate class with EventDispatcher
 private static var dispatcherInit =
 EventDispatcher.initialize(com.muzakdeezign.samples.MyButton.prototype);

 // declare EvenDispatcher methods
 public var addEventListener:Function;
 public var removeEventListener:Function;
 public var dispatchEvent:Function;

 private var back_mc:MovieClip;

 private var __index:Number;

 function MyButton() {
  this.init();
 }

 private function init():Void {
  this.back_mc.onRelease = Delegate.create(this, this.backReleaseHandler);
 }

 private function backReleaseHandler():Void {
  this.dispatchEvent({type:click});
 }

 private function setIndex():Void {
 }

 function get index():Number {
  return this.__index;
 }
 function set index(val:Number):Void {
  this.__index = val;
  this.setIndex();
 }

 }

 Here's a screencast making the above:
 http://muzakdeezign.com/flashcoders/create_component/MyButton.html

 regards,
 Muzak




___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Jesse Graupmann
So is this still out of the question... 



///
/// IN FLA
///

var box = this.attachMovie ( 'box', 'box',  this.getNextHighestDepth() );

jgButton.initializeMovieClip ( box );
box.data = 'this is my data';

box.showEvents( 'onPress', 'onRollOver' );
box.addEventListener ( 'onPress', mx.utils.Delegate.create( this,
onButtonEventHandler ) );
box.addEventListener ( 'onRollOver', mx.utils.Delegate.create( this,
onButtonEventHandler ) );
function onButtonEventHandler ( evt:Object ) 
{ 
trace( newline );
trace ( 'target: ' + evt.target ) 
trace ( 'type: ' + evt.type ) 
trace ( 'data: ' + evt.target.data ) 
}


///
/// IN jgButton.as
///



class jgButton extends MovieClip
{
private static var EventDispatcherDependancy  =
mx.events.EventDispatcher.initialize( jgButton.prototype );
private static var jgButtonTypes:Object = {
onRollOut: 0,
onRollOver: 0,
onPress: 0,
onRelease: 0,
onReleaseOutside: 0,
onDragOut: 0,
onDragOver: 0
};
private var addEventListener:Function;
private var removeEventListener:Function;
private var dispatchEvent:Function;
private var dispatchQueue:Function;

public function jgButton (){}


///  STATIC  ///


public static function initializeMovieClip ( mc:MovieClip ):Void
{
// replace MovieClip class with jgButton class
mc.__proto__ = jgButton.prototype;
}



///  PUBLIC  ///

///
/// LISTENERS
///

public function showEvents (  )
{
if ( arguments.length == 0 )
{
//
//  SHOW ALL
//
for ( var type in jgButtonTypes )
{
if ( !jgButtonTypes [ type ] )
{
jgButtonTypes [ type ] = 1;
this [ type ] = jgButtonEvent ( type
);
}
}
}
else
{
for ( var i in arguments )
{
var type:String = arguments[ i ];

if ( !jgButtonTypes [ type ] )
{
jgButtonTypes [ type ] = 1;
this [ type ] = jgButtonEvent ( type
);
}
}
}
}

public function hideEvents ( )
{
if ( arguments.length == 0 )
{
//
//  DELETE ALL
//
for ( var type in jgButtonTypes )
{
if ( jgButtonTypes [ type ] )
{
jgButtonTypes [ type ] = 0;
delete this [ type ];
}
}
}
else
{
for ( var i in arguments )
{
var type:String = arguments [ i ];

if ( jgButtonTypes [ type ] )
{
jgButtonTypes [ type] = 0;
delete this [ type ];
}
}
}
}



///  PRIVATE  ///


///
/// EVENT FUNCTION
///

private function jgButtonEvent ( type:String )
{
return function()
{
var evt = {};
evt.type = type;
evt.target = this;
this.dispatchEvent ( evt );
}
}
}





_

Jesse Graupmann
www.jessegraupmann.com 
www.justgooddesign.com/blog/ 
_



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: Saturday, June 02, 2007 5:54 PM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] AS 2 Delegate question

This isn't about quick and easy, but about encapsulation, OOP and best
practices.
Quick and easy also often means quick and dirty ;-)

Here's a stripped down version of a custom button.

import mx.events.EventDispatcher;
import mx.utils.Delegate;

class

Re: [Flashcoders] AS 2 Delegate question

2007-06-02 Thread Muzak
The EventDispatcher methods need to be public

 public var addEventListener:Function;
 public var removeEventListener:Function;
 public var dispatchEvent:Function;

And I usually set the events on the Class itself, but use a child movieclip 
(which you could see in the screencast).
Someone (not necessarily you) could set events from outside the instance, 
messing up your component.

var box = this.attachMovie ( 'box', 'box',  this.getNextHighestDepth());
box.onPress = Delegate.create(this, boxPressHandler);

It's no big deal if you will be the only one who will ever use the component. 
You'll just have to remember not to use any of the 
MovieClip events from outside the instance.
I know it sounds silly, because that's why you created the class in the first 
place, but when you distribute alot of components it's 
kinda out of your hands ;-)

You'd be surprised how many people drag a Button Component from the components 
panel on stage and then go:

myButton.onRelease = function(){}

rather than using addEventListener(click, handler);

regards,
Muzak

- Original Message - 
From: Jesse Graupmann [EMAIL PROTECTED]
To: flashcoders@chattyfig.figleaf.com
Sent: Sunday, June 03, 2007 5:52 AM
Subject: RE: [Flashcoders] AS 2 Delegate question


 So is this still out of the question...



 ///
 /// IN FLA
 ///

 var box = this.attachMovie ( 'box', 'box',  this.getNextHighestDepth() );

 jgButton.initializeMovieClip ( box );
 box.data = 'this is my data';

 box.showEvents( 'onPress', 'onRollOver' );
 box.addEventListener ( 'onPress', mx.utils.Delegate.create( this,
 onButtonEventHandler ) );
 box.addEventListener ( 'onRollOver', mx.utils.Delegate.create( this,
 onButtonEventHandler ) );
 function onButtonEventHandler ( evt:Object )
 {
 trace( newline );
 trace ( 'target: ' + evt.target )
 trace ( 'type: ' + evt.type )
 trace ( 'data: ' + evt.target.data )
 }


 ///
 /// IN jgButton.as
 ///



 class jgButton extends MovieClip
 {
 private static var EventDispatcherDependancy  =
 mx.events.EventDispatcher.initialize( jgButton.prototype );
 private static var jgButtonTypes:Object = {
 onRollOut: 0,
 onRollOver: 0,
 onPress: 0,
 onRelease: 0,
 onReleaseOutside: 0,
 onDragOut: 0,
 onDragOver: 0
 };
 private var addEventListener:Function;
 private var removeEventListener:Function;
 private var dispatchEvent:Function;
 private var dispatchQueue:Function;

 public function jgButton (){}


 ///  STATIC  ///


 public static function initializeMovieClip ( mc:MovieClip ):Void
 {
 // replace MovieClip class with jgButton class
 mc.__proto__ = jgButton.prototype;
 }



 ///  PUBLIC  ///

 ///
 /// LISTENERS
 ///

 public function showEvents (  )
 {
 if ( arguments.length == 0 )
 {
 //
 // SHOW ALL
 //
 for ( var type in jgButtonTypes )
 {
 if ( !jgButtonTypes [ type ] )
 {
 jgButtonTypes [ type ] = 1;
 this [ type ] = jgButtonEvent ( type
 );
 }
 }
 }
 else
 {
 for ( var i in arguments )
 {
 var type:String = arguments[ i ];

 if ( !jgButtonTypes [ type ] )
 {
 jgButtonTypes [ type ] = 1;
 this [ type ] = jgButtonEvent ( type
 );
 }
 }
 }
 }

 public function hideEvents ( )
 {
 if ( arguments.length == 0 )
 {
 //
 // DELETE ALL
 //
 for ( var type in jgButtonTypes )
 {
 if ( jgButtonTypes [ type ] )
 {
 jgButtonTypes [ type ] = 0;
 delete this [ type ];
 }
 }
 }
 else
 {
 for ( var i in arguments )
 {
 var type:String = arguments [ i ];

 if ( jgButtonTypes [ type ] )
 {
 jgButtonTypes [ type] = 0;
 delete this [ type ];
 }
 }
 }
 }



 ///  PRIVATE  ///


 ///
 /// EVENT FUNCTION
 ///

 private function jgButtonEvent ( type:String )
 {
 return function()
 {
 var evt = {};
 evt.type = type;
 evt.target = this;
 this.dispatchEvent ( evt );
 }
 }
 }




___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com