Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-22-2009, 09:06 PM   #1
MadBeat
Member
 
Join Date: Sep 2008
Posts: 53
Default e.currentTarget question

Hi
Basically I have 16 buttons which I want to change colour when turned on and off kinda thing using colourTransform. I have achieved this by using 16 event listners and 16 functions but know there must be a better approach. I kinda got it working with one function using e.currentTarget but cant figure out to store the on/off state of each button to send to the one function whilst using e.currentTarget. E.G. Turn one button on and will have to click another twice to turn it on.

How could this be solved. Hope this makes sense. Thx.

ActionScript Code:
b1.addEventListener(MouseEvent.CLICK, onPattern, false, 0, true); b2.addEventListener(MouseEvent.CLICK, onPattern, false, 0, true); function onPattern(e:MouseEvent):void{      on1 = on1+1     if(on1 >= 2){     on1 = 0     colorInfo1.greenOffset = 0;     colorInfo1.blueOffset = 0;     colorInfo1.redOffset = 0;           e.currentTarget.transform.colorTransform = colorInfo1;     }     if(on1 == 1){                     colorInfo1.greenOffset = 20;     colorInfo1.blueOffset = -100;     colorInfo1.redOffset = -65;       e.currentTarget.transform.colorTransform = colorInfo1;     }         }
__________________
The thought of Death is a lot worse than Death itself!
MadBeat is offline   Reply With Quote
Old 10-22-2009, 09:22 PM   #2
nyghtrunner
Senior Member
 
nyghtrunner's Avatar
 
Join Date: Nov 2004
Posts: 637
Default

Here's a little class that helps out a whole lot with what you are trying to do.

ActionScript Code:
package coms.utils {         public class proxyFunction {                 public static function create($function:Function,... $rest):Function {             return function(... $moreRest):void {                 $function.apply(this,$moreRest.concat($rest));             }         }             } }

And then you use it like this:

ActionScript Code:
instance.addEventListener(MouseEvent.MOUSE_DOWN,proxyFunction.create(buttonClick,instance)); function buttonClick(e:MouseEvent,instance:*):void {     //do stuff         trace(instance); //will trace the button. }

As you will notice, you can pass the actual instance (or anything else) to the listener function along with the event. Not just the event.

Tinker around, extend it, do other stuff with it... It's a lot quicker than creating custom event dispatches, though less powerful.

Hope this helps!
nyghtrunner is offline   Reply With Quote
Old 10-22-2009, 09:25 PM   #3
RogerClark
Senior Member
 
Join Date: Sep 2009
Location: Melbourne, Australia
Posts: 1,040
Default

There are various solutions to storing the on / off state of the button.

One way is to subclass the class you are using as the button e.g. MovieClip and add an extra property e.g. public var onState:Boolean;

If you already use a custom class, just add the property to the class.

There may be other hacky ways to do it e.g using some other property of the mc that you don't care about that much e.g set very slight differences in alpha for on and off states.
But you could be better off subclassing
RogerClark is offline   Reply With Quote
Old 10-22-2009, 09:32 PM   #4
MadBeat
Member
 
Join Date: Sep 2008
Posts: 53
Default

Sick! Glad I asked thx. If u dont mind are able to explain the basis of how that bit code works!

Have tested it and works a treat but a bit of a mystery to my coding level, I can visualize how I want my code to work but have trouble implementing it?

Abracadabra!
__________________
The thought of Death is a lot worse than Death itself!
MadBeat is offline   Reply With Quote
Old 10-22-2009, 09:37 PM   #5
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

If you use that proxyFunction, be aware that you are setting yourself up such that you can never remove the eventListener. Not a small consideration if cleanup and memory management is an issue, or if your swf will be loaded into a container swf.
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;
maskedMan is offline   Reply With Quote
Old 10-22-2009, 09:38 PM   #6
MadBeat
Member
 
Join Date: Sep 2008
Posts: 53
Default

what do u mean by a sub class, i knw what a class is. Just getting to grips with classes thx. All help is awesome.
__________________
The thought of Death is a lot worse than Death itself!
MadBeat is offline   Reply With Quote
Old 10-22-2009, 09:48 PM   #7
nyghtrunner
Senior Member
 
nyghtrunner's Avatar
 
Join Date: Nov 2004
Posts: 637
Default

Yeah... I think I went beyond the pail with my reply... There's no need to complicate things like that.

I agree with RogerClark. Create a class. Give each button a number, and an open/close Boolean, and just take care of the rest in the mouse listener method.

Then it's pretty much a simple if/else statement to handle stuff.

ActionScript Code:
if (onState == true) {      //do stuff to turn button "off"      onState = false; } else {      //do stuff to turn button "on"      onState = true; }

And in the class, you can give each one a number, and each instance will "know" what number it is, so you can swap things up based on that number. Make sense?
nyghtrunner is offline   Reply With Quote
Old 10-22-2009, 09:56 PM   #8
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

no offense to nyghtrunner but I'm with maskedMan about issues with that code.

Not only does it make it nye impossible to remove the event listener (which can cause memory leaks and other memory issues). But it also creates a scope dependency that can prove whacky.

Not the function inside the proxyFunction is scoped to access values defined in proxyFunction... this leaves the two params $function and $params floating somewhere in lala land... with no reference or symbolic link going on. It could randomly get stuck in memory, be lost randomly (causing the function to fail).

Sorry, it just logically doesn't work out in the end. It plays on some of the buggy scoping issues AS3 has, which can further more cause this to break down the road if these scoping bugs get fixed. Consider it, you are relying on the internal function being scoped to proxyFunction at all times.



@OP - you can do several things to store the on/off value. You can create a property on the Button that can be set to true or false, you can create a Dictionary that you store the value of the Button in, you can check the presence of what is done when on (if transform.colorTransform == some crap), etc etc. And all of this can be accessed abstractly as e.currentTarget.
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 10-22-2009 at 10:00 PM.
lordofduct is offline   Reply With Quote
Old 10-22-2009, 10:00 PM   #9
MadBeat
Member
 
Join Date: Sep 2008
Posts: 53
Default

yeah totally, I agree. lol.
__________________
The thought of Death is a lot worse than Death itself!
MadBeat is offline   Reply With Quote
Old 10-22-2009, 10:03 PM   #10
nyghtrunner
Senior Member
 
nyghtrunner's Avatar
 
Join Date: Nov 2004
Posts: 637
Default

Quote:
Originally Posted by nyghtrunner View Post
Here's a little class that helps out a whole lot with what you are trying to do.

ActionScript Code:
package coms.utils {         public class proxyFunction {                 public static function create($function:Function,... $rest):Function {             return function(... $moreRest):void {                 $function.apply(this,$moreRest.concat($rest));             }         }             } }
Masked Man is right. This is not an "elegant" solution in many ways, just a quick one.

Here's pretty much how it works. When you call "proxyFunction.create(funcName,...rest)", you call the class, and send it the function name (funcName), and any arguments you would like to send to the function (instance, etc.).

The proxyFunction then essentially returns a function for the named function, along with the extra parameters, which are concatenated from the ...$rest (parameters passed to it). Does that make sense?

It's a nifty little hack, but as the Masked one said... It's static, so it's always there in the memory.

In retrospect, I would recommend classing out the buttons, and not using the proxyFunction, as much fun as it is.
nyghtrunner is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:22 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.