Is it bloody possible?!
I am trying to get my head around AS3 and am starting simply. Or so I thought.
How do I do this in AS3?
myMC_mc.onRelease = function() { myText_txt.text = myFunction("hello"); }
function myFunction( myMessage:String ):String { return myMessage; }
It seems to me that it is not possible to send a variable to a function with a button click when using addEventListener without creating a custom event.
If a custom event is the only way to do it, can someone please show me how to do that, as all the pages I'm finding on the web (a la Grant Skinner and co.) aren't working when I try it. I keep getting a "1180: Call to a possibly undefined method SendVarOnClick." where SendVarOnClick is the name of my custom event.
Thanks!
In case you want to see my shot at a custom event:
In my flash file on frame 1:
var operators:Array = new Array(); operators[0] = "+"; operators[1] = "-"; operators[2] = "/"; operators[3] = "X"; operators[4] = "=";
for(var i=0; i<operators.length; i++) { var currOperator:Object = "operator_" + i; currOperator = this.addChild(new mcOperator()); currOperator.x = ((currOperator.width + 10) * i) + 180; currOperator.y = 300; dispatchEvent(new SendVarOnClick(SendVarOnClick.ONCLICK, operators[i] ));
currOperator.addEventListener( SendVarOnClick.ONCLICK, onClickListener );
}
function onClickListener(evt:SendVarOnClick):void
{
trace( evt.operator );
}
In my SendVarOnClick.as file:
package { import flash.events.Event;
class SendVarOnClick extends Event
{
public static const ONCLICK:String = "onClick";
public var operator:String;
public function SendVarOnClick( type:String, str:String )
{
operator = str;
super( type );
}
}
}
viahttp://flexcomps.wordpress.com/2008/04/29/dispatching-custom-events-in-as3-with-data-or-object/
have you got Essential Actionscript 3.0, Colin Moock?
I'm just in the middle of trying to do pretty much the exact same thing, and I'm following what Moock says, page 225. There are some differences between what you've done there and his code, I'll get mine working then quote him for you.
Looking at your code though I think there's some confusion going on there... what's this line doing in the FLA?
dispatchEvent(new SendVarOnClick(SendVarOnClick.ONCLICK, operators[i] ));
- shouldn't that be in your mcOperator class? I guess it probably is actually, I imagine that's just there for testing and you forgot to take it out?
You do store that SendVarOnClick class where the FLA can find it?
oh I think your problem is simply this:
class SendVarOnClick
should be
public class SendVarOnClick
Originally posted by: DontBogartMe have you got Essential Actionscript 3.0, Colin Moock?
I'm just in the middle of trying to do pretty much the exact same thing, and I'm following what Moock says, page 225. There are some differences between what you've done there and his code, I'll get mine working then quote him for you.
Looking at your code though I think there's some confusion going on there... what's this line doing in the FLA?
dispatchEvent(new SendVarOnClick(SendVarOnClick.ONCLICK, operators[i] ));
- shouldn't that be in your mcOperator class? I guess it probably is actually, I imagine that's just there for testing and you forgot to take it out?
You do store that SendVarOnClick class where the FLA can find it?
haha, no I didn't leave it there, I put it there on purpose because that's where I thought it was supposed to go. (I've never used dispatchEvent before -- I've just used cheats in my AS2 code so I could get the "onRelease" and "onRollOver" handlers to work in my classes). I haven't created a mcOperator class, I only put "mcOperator" into the linkage field of my movieclip in the library.
I do have Colin Moock's book, but I got about half way through it before it stopped making sense... I'm not very good at learning from a book, to be honest, I like to have practical working examples that I can play with (hence my other thread).
So basically, what you're saying is that I should create a mcOperator class and stick that dispatchEvent into the constructor so that when a mcOperator Movieclip is brought onto the stage, it runs it?
Originally posted by: DontBogartMe oh I think your problem is simply this:
class SendVarOnClick
should be
public class SendVarOnClick
nope. Same error.
EDIT:-- Whoops, nevermind I don't get that error anymore. however it's not working when I click the mc. it's getting into the SendVarOnClick constructor ok, but it's not getting into onClickListener when the mc is clicked.
I'm pretty sure it has something to do with this line: public static const ONCLICK:String = "onClick"; but as I'm not really sure what it is doing in the first place, I'm not sure how to fix it. Why wouldn't I just use the regular CLICK event?
oh well then there's all sorts of crazy magic shit missing from your code then I'm afraid 
I'll try to give you an overview without any code so you can get a grasp of how to do what you seem to be trying to do:
You have some code on the main timeline that is going to listen for events on a bunch of things on the stage. The events it's listening for have to be custom events so you can pass some extra data.
So to do that you'll need to create two classes - one to define the custom event, and another to handle the things on the stage.
The reason you need that second class (I'll call it the mcOperator class to fit in with your code) is because the only way to make something dispatch a custom event is by coding it yourself - and that means writing a class.
Something like this: package {
import flash.display.MovieClip;
import flash.events.*;
public class mcOperator extends MovieClip {
private var symbol:String = "";
public function mcOperator($symbol:String) {
symbol = $symbol;
// Listen for when the user clicks this MC
addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void {
// dispatch the custom event, passing the symbol for this instance.
dispatchEvent(new SendVarOnClick(SendVarOnClick.ONCLICK, symbol);
}
}
}
Your main timeline code wasn't far off:
var operators:Array = new Array(); operators[0] = "+"; operators[1] = "-"; operators[2] = "/"; operators[3] = "X"; operators[4] = "=";
for(var i=0; i<operators.length; i++) { //Create a new instance of mcOperator, passing it a new value from the operators array var currOperator:mcOperator = new mcOperator(operators[i]);
// Add the operator to the stage
this.addChild(currOperator);
// position the operator
currOperator.x = ((currOperator.width + 10) * i) + 180;
currOperator.y = 300;
currOperator.addEventListener( SendVarOnClick.ONCLICK, onClickListener );
}
function onClickListener(evt:SendVarOnClick):void
{
trace( evt.operator );
}
I haven't tested it at all..... not my fault if it melts your motherboard....
btw the class file for mcOperator needs to be called mcOperator.as
DBM, you rock dude.
I know that was a pain in the ass to do for a AS3 dummy like me so I REALLY appreciate it. It's the basics of event driven code that I am struggling with (obviously), but reading through your explanation quickly seems to make sense.
I will put that in force tomorrow morning and let you know if it works (it's home time!).
Thanks!
works like a charm, once I figured out that I need to import flash.text.TextField to accomodate the textfield inside the movieclip :rolleyes:
I see I am going to have a hard time with AS3...
btw -- What is this line doing exactly? public static const ONCLICK:String = "onClick";
oh cool - can't believe it worked almost first time! Consider it a birthday present 
Originally posted by: ernieweaselfat btw -- What is this line doing exactly? public static const ONCLICK:String = "onClick";
It's not doing much really, but it helps keep your code readable and bug free by setting up a proper way for you to add listeners to catch that particular event - the ONCLICK event.
e.g. this line can be written two ways: dispatchEvent(new SendVarOnClick(SendVarOnClick.ONCLICK, symbol); and dispatchEvent(new SendVarOnClick("onClick", symbol);
Both will work just fine - but the second is more prone to typo bugs if you get that string wrong, cos it'll compile ok but you'd never catch the event. However if you misspell SendVarOnClick.ONCLICK then the compiler will complain.
Note the same applies to the addEventListener line: currOperator.addEventListener( SendVarOnClick.ONCLICK, onClickListener ); and currOperator.addEventListener( "onClick", onClickListener ); are the same.
Originally posted by: ernieweaselfat I see I am going to have a hard time with AS3...
Yeah it's been throwing me some curve balls lately too - like how all movieclips now catch all mouse events by default (they didn't use to), so when I stuck a PNG with a transparent middle over everything to make a nice border, it took me ages to work out why all my buttons had stopped responding to the mouse.
Get used to looking stuff up in Moock's book - it is pretty solid.