Why can't I execute an anonymous function directly, like:[code]The trace statement should output 'hello', but it just outputs 'undefined'. This is supposed to work in JavaScript (according to Mr Douglas Crockford). I thought Actionscript was compatible with JavaScript in this regard...
I came to AS3 from JS world, and I should confess that anonymous functions are my weakness. I tend to use them everywhere. Now, coming to AS3 I've heard and read in lots of places, that AS and Flash are enormously bad at handling garbage collection, that one should empty, dispose and remove all event handlers and objects manually to avoid weird and unexplainable memory leaks and crashes. Not sure what part of this is true, but I would like to follow best practices right from the beginning. How bad is idea of using anonymous functions as event handlers? Consider for example a code like this:
Ccontorls.elapsed is the setter, which apart from setting current play time for video player, updates the whole UI, and stream is NetStream object, which streams the actual video. There are lot's of other places where anonymous function may make code cleaner and more intuitive. Check the following code for simple fade-in effect for the control bar:
public function showControls() : void { var self:Controls = this; if (!visible) { visible = true; fadeTimer = new Timer(30, 10); [Code] .....
I totally like how it looks and fits into the code, but I'm concerned about leaks. While Event.ENTER_FRAME handler probably would never become harmful in this form, what about timer listeners. Should I remove those listeners manually, or they will be removed automatically, as soon as I set fadeTimer = null ? Is it possible to remove listeners with anonymous functions properly at all?
I currently have a class that has a method that creates new duplicates of a clip on the screen, changes some of their properties (such as color, and text display), and then arranges them in a straight column.Inside that method, I have some clipEvent Statements for each button.
My needs are to execute a javascript whitout calling any external function of the html file when a flash button is clicked. I need to obtain the effect that happen when the user put in his address bar the text javascript:"alert("Hello World!"); and press enter I need to do this whitout calling any external functions, whitout calling anything in the html file, all just with the swf. Of course the script that i must call is another than javascript:"alert("Hello World!"); but let's begin with this easy script and when working i can thikn to implement the complex one
i tried this
ActionScript Code: on(release) { function runJS(script:String):Void {
[Code]...
but is not working, i get a syntax error, maybe cuse of the " in "Hello World!"
I want to ask if what i need to do is possible and, if yes, why my AS code is not working.
I have some functions that when applied directly to buttons work as planned, but when I create a function from them to apply to the array, they fail to execute properly. This function is meant to set all 56 buttons to alpha 0, and as unselected. What happens is the buttons flash onscreen at 100% before setting to alpha 0. They do behave as if unselected, but don't know if that's due to the code.
This function should fade the button up on rollover. But on rollover the button only increase by 10% then stops. Each subsequent rollover increases it another 10%. Code: for (_root.i = 0;i<56;i++){ _root["btn"+_root.i].onRollOver = function() { this._alpha<100 ? this._alpha += 10 : this.onEnterFrame = ""; }}
This function should fade the button out on rollout. Since the rollover increases them only by 10% increments (above), the result is the buttons just bounce back and forth between alpha 20 and 30. Code: for (_root.i = 0;i<56;i++){ _root["btn"+_root.i].onRollOut = function() { (this._alpha>20 && this.bSelected==false) ? this._alpha -= 10 : this.onEnterFrame = ""; }}
Last one: This function should set several behaviors on release. Right now none of them appear to work. Code: for (_root.i = 0;i<56;i++){ _root["btn"+_root.i].onRelease = function() { this.bSelected = true; this.enabled = false; pastBtn.enabled = true; pastBtn._alpha = 20; pastBtn.bSelected = false; pastBtn = this; }}
I have 3 functions in my main document class that I want to execute when the main timeline reaches a particular frame number. how do I do that? see the code and comments
I'm having some weirdness with a project that I'm doing in IE. Long story short, I've got a basic shell that loads in external SWFs based on user feedback. Once those SWFs get loaded in, they execute functions from the shell (_root) movie. It works great in Firefox, but immediately breaks (no way) in IE. The movie gets loaded in, but none of the functions run.
I suspect it has something to do with security, because, for a variety of reasons, I have to load the SWFs from their absolute location, rather than their relative location. When I test it locally (but loading the movies from their absolute, live location), everything still works, but I get that damn security warning that reads like this:
When I place some codes in a keyframe, these codes are showing on all the following frames up till the next keyframe with codes. My question is, does Flash execute codes only in the keyframe or it execute whatever is in the frame regardless whether its a keyframe or not?
I noticed that in anonymous event handler this is referenced to global, not my class. I know that I can use outer referenced variables (because it creates closures), but how to get right this context?
I have an event listener with an anonymous function literal, rather than a function reference. What I am trying to figure out is how to remove that event listener.Obviously, I need a reference to the anonymous function, so the two functions are ===. But, something is going wrong... it just doesn't work, ad this situation doesn't generate any feedback to tell me what might be wrong.There is some good info here, but not a clear explanation.
Code: private static var _myFunc:Function; public static function addCreditsToolTip(clip:DisplayObject):void
In as3, I am adding event listener and then attaching the anonymous function to it:[code]Now this whole piece of code is looped n times. Now, I have n myBoxes and whenever I roll my mouse over the box, it should alert the name. But, what I see is that the last value of count is used by each box. How can I pass parameter or value to the anonymous function? ( as roll over , I believe, expects only one variable)
little bit what Im trying to do: I have 2 SWF, one.swf and two.swf 1 LocalConnection to send 1 variable from one.swf to two.swf
one.swf code:
var param:Number = new Number(5); var sending_lc:LocalConnection = new LocalConnection(); sending_lc.send("_Connection1", "myMethod", param);
[code]...
I need to be able to use param value out of the function, because a I need to perform a switch with this value, since the function wich receives de param value is an anonymous function I dont know how to do to get the value from outside the function because a dont have a reference name to the function so, the normal way to return a value is not going to do the job.
I am trying to play an MP3 File in Flash Lite 2.1 (I'm using Flash 8 to develop it). The filename of the MP3 file is imported from an XML file.Here is a copy and paste from the help file on how to do this:
// create a new Sound object var my_sound:Sound = new Sound(); // If the sound loads, play it; if not, trace failure loading. my_sound.onLoad = function(success:Boolean) {
[code]....
When I test this code on its own, it does work fine for me. However, I want the above to be executed inside a function that does a number of other things... Here is a sample of what I'm trying to get working.
function myMusic() { // create a new Sound object var my_sound:Sound = new Sound(); // If the sound loads, play it; if not, trace failure loading.
[code]....
I think it's got to do with the fact that once I put it inside a function you are nesting functions (there is a function call from the onLoad event).
I tried changed the line:
my_sound.onLoad=function(success: Boolean) { to my_sound.onLoad=myFunctionName(parameters go here)
and then I created a funciton called myFunctionName but I wasn't sure how to send the sound object or what parameters the function should have or how to get the .start() method to work inside the new function.
up.addEventListener(MouseEvent.CLICK, function clickFunc(event:MouseEvent):void { revealSpinner(event,51.42,1,spinner);
[Code].....
The above code adds a listener to a couple of MC's. Originally the methods were anonymous but I have named them clickFunc() in order to try an reference them in my remove listener.
Here is my remove listener code. Both of these snippets are in separate functions. The add listener method is called before the remove method.
If I want to send an object to a function with one child called foo equals "bar", i need to do the following:[code]is there a way to declare the object in the function itself ? something like that:[code]
Getter functions allow obj.meth syntax instead of obj.meth(), I'd like to create an anonymous one of these to return from another function. function get ():Object { } is invalid syntax. I don't suppose Flex offers an easy way to get this functionality, if it's even possible?
I'm trying to differentiate anonymous functions like:
function() { trace("WOO"); }
from the other ones ('named'?) like
var _FUNC:Dynamic = function() { trace("WOO"); }
The reason I want to do that is because I can't compare between two anonymous functions, because they are two different ones.
var _TEST:Dynamic = function(a:Dynamic):String { var _TESTA:Dynamic = function() { trace("WOO"); }; var _TESTB:Dynamic = _FUNC; return (a == _TESTA) + ", " + (a == _TESTB); }
If I run _TEST(_FUNC);, I'll get back "false, true". Even though they are the same function, they are NOT the same object.Is there a way to compare those such that functions that they are the same if they perform the same task? Is there a way to serialize functions? So that maybe I can compare the serialized representations and see if they share the same 'code'.
In c++ if I have a function that takes a class I have defined elsewhere as a parameter like so:void moveto(CPoint2D point) { ... }I can invoke it and pass along data to it like this:moveto(CPoint2D(0,0));basically creating the object and invoking it's constructor to be passed to the functionIs there an equivalent to this in AS3?if I have a function like this:function initialize(min:Point, max:Point): void { ... }how do I do something along the lines of :initialize(Point(0,0), Point(10,10))This throws an error im assuming because it thinks I want to cast something to a point and the notation I found earlier to do something similar only seemed to work if using the basic Object class.
If an object, or say... a document class, creates an instance of a class (and stores it as a variable), and it doesn't pass any arguments to the constructor of the class, can that class object, by simply having a function of it called by the parent, tell who sent it that command/request? In other words, can a class object know who called it (such as it's parent) via a (seemingly) anonymous call?
I have an arbitrary number of files that I need to load in an AIR app.I want to iterate through an array of File object and create and launch Loaders for each one's File.url.When they are done (event COMPLETED or IOErrorEvent.IO_ERROR), I want to stuff their data somewhere. If they fail, I want to make an exception report. I cannot find any way to reference the File object from the event listener (certainly not the IO_ERROR)The best way would be to create individual event handlers for each Launcher that had the File information "hard-coded" into it. At the very least, I could use the function itself as an index to a Dictionary:
foo(fileToLoad : File) : void { var theCompletedHandler : Function = {
[code]....
Can I use event.currentTaget and use the Loader instance as an index? Will that have any weird dependencies ?
I'm new to AS3, and can't figure out why this loop isn't behaving the way it "should."
[Code]...
How can I give each anonymous function a reference to each object represented by f, rather than a simple reference to f each time? Specifically, why is it that each copy of the anonymous function gets bound to a single reference to f? How (I should say why) exactly does AS3 differ from JavaScript in this regard?
I have a button labeled 'blueButton' and I'd like to use an anonymous function to handle the click, like so:
blueButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void { trace( "-----click detected-----" ); // now do other stuff });
Problem is, the click handler is called multiple times per click. The number of times called seems to be random (sometimes 2, sometimes 4, sometimes 3 etc). could I be missing something in my code, or maybe I set up the button wrong?
also I noticed that it seems to always get called once on the first click. After the first click is when it starts getting called additional times, maybe that has something to do with it?
I need to get a javascript var in my Flash application. I like to be able to just set a variable in the javascript (client constraints) rather than define a function.
Can this be done? I am trying to use the ExternalInterface.call()