View Full Version : Tricks of the Trade
senocular
02-03-2003, 07:43 PM
Im going to post little AS tricks here when I think of them, to learn and have here for reference. Off-hand I cant really pull them off of the top of my head, but they come to me when I need them in coding. When that happens Ill come back and post anything thats considerably 'tricky' enough to be posted here.
Right now Ill do Daisychaining and callbacks. These I know now because I used them recently ;)
Daisychaining
Though not the technical term (I dont think - its just what I call it) daisychaining is the act of throwing a bunch of methods on the end of an object (and each other) to have them all called in one line of code. Ex:
objClass = function(name, value){
this.name = name;
this.value = value;
};
objClass.prototype.showName = function(){
trace(this.name);
return this;
};
objClass.prototype.showValue = function(){
trace(this.value);
return this;
};
objInstance = new objClass("cletus",12).showName().showValue();
// outputs "cletus" and "12".
The trick lies in returning the object, this in the prototype functions. Then, when called, the action is executed and the object is returned. When returned, the next method is executed on the returned value which is the object itself.
Chances are you've seen this before with the easy version of a string replace: str.split().join(); I used it here on Kirupa in the Gaming battle (http://www.kirupaforum.com/showthread.php?s=&threadid=11294) creating and drawing objects in on the screen (and instantly adding onEnterFrame events to dynamically created objects).
Callbacks
These are functions associated with an event. When the event is triggered, the callback function is run in responce. Setting an onMouseDown function is setting a callback function. However, you can also incorporate such functions into your own code allowing you to set a temporary set of instructions to be run at a later time. A simple example of this can be implemented in a tweened or multi-framed transition. Lets say you have about 6 or so pages of content in your movie and a small animation leading up to and leading out of each page. In moving from page to page you have to not just goto the new page but play out of the current. What you could do is set your buttons in each page to play() the exit animation and set up a callback function which is to be run at the end of that animation. Example:
// button
on(release){
action = function(){
_root.gotoAndPlay("page2_label");
};
play();
};
// last frame
action();
Now certainly, in that simple example you could just set a variable and throw it in a gotoAndPlay at the end of the animation, but this allows for many actions to occur which can be similar or vastly different (if anything at all) from any other set callback functions. I used it here on Kirupa in the XML menu example (http://www.kirupaforum.com/showthread.php?s=&threadid=13697) using a callback menu to open the next menu section after closing the current.
senocular
02-04-2003, 12:48 PM
Function Appending (MX)
One common problem which may arise within your coding is the addition of actions within an onEnterFrame event (or any event for that matter). Once you set an event like onEnterFrame, if you ever try to add on a function call using onEnterFrame = you will not append the call to the current action but replace it, therefore preventing the original from being run again. To get around this, you can do the following:
// assign
this.onEnterFrame = function1;
// append
oldEnterFrame = this.onEnterFrame;
this.onEnterFrame = function(){
oldEnterFrame();
function2();
};
So what happens is the old onEnterFrame is saved in the variable oldEnterFrame and the new onEnterFrame is set to equal a function running that AND the new function you want to add to the event, in this case, function2. So the old onEnterFrame runs, then the new function all within the next onEnterFrame call. And really, this can be done everytime you assign an onEnterFrame action (assuming you want to add to whats there) even if theres nothing there to begin with because the oldEnterFrame call, if no onEnterFrame has yet been set, will just be null. However, this is a little bulky and would be a pain to continue using in the manner. Instead, lets write a function that will do that for us. Luckily MX allows Function 'object' prototypes which makes this job a lot easier. We can write two versions of whats above, one to insert the new function before the old function, and one to insert the new function after the old function. If you have one funcion relying on results from another, you'd want to be able to control which one gets called first on an onEnterFrame...
// prototype assignment
Function.prototype.addAfter = function(newFunc){
var oldFunc = this;
return function(){
oldFunc();
newFunc();
};
};
Function.prototype.addBefore = function(newFunc){
var oldFunc = this;
return function(){
newFunc();
oldFunc();
};
};
// whenever needed use in the following manner:
this.onEnterFrame = function1;
this.onEnterFrame = this.onEnterFrame.addBefore(function2);
First onEnterFrame is assigned function1, then its assined the return of the addBefore call which gives a new function with this.onEnterFrame and the passed function2 called together.
jbradley
02-04-2003, 02:00 PM
First thing: don't 'daisychain' like that.
Returning "this" as a property will return the entire object. This method will cause infinie recursion if you are not extremely careful with your code - and a worse practice if you are coding complex components with various levels of initialization.
Best method to add extra properties onto an object for them to be called is to develop a routine that calls a list of objects in that object which need to get called ... basically a function queue. This can be emulated with function arrays. All you need to do is to push the function on the end of the array, or splice/join in different areas to change the order of execution.
Second: your callback method is not good either. It is bad practice to define a function inside a function, in any language. If you want to design a callback, program a callback function for your component. Using arguments.callee will help with complicated interaction when you don't know what callback function to execute if you are developing a generic class.
That's my first post. I'm checking out the other stuff now... :)
senocular
02-04-2003, 02:09 PM
TIP: Dont animate on the main timeline. Instead do everything within movieclips. This makes the movie more portable .. helps in Flash 5 where you can have MX like events. It also gives you more control in handling the project on a whole allowing to easily scale/reposition etc.
True & False as Numbers (Key movement)
You probably already know that you can evaluate numbers as being either true or false with 0 being false and any non-zero number being true. What you can also do is use true and false themselves as numbers as well. In terms of value, true is 1 and false, 0 - which makes sense since in terms of number value 1 is evaluated to be true and 0 to be false. So true and false can then be used in mathematical calculations, ie. true * 5 = 5, false + 10 = 10 etc. This can save you some time/code if you had something like the following:
// is a member is a true or false boolean
if (isAMember){
dailyAllowance = 10;
}else{
dailyAllowance = 0;
}
// can be shortened to
dailyAllowance = 10 * isAMember;
One of the more applicable uses of this, I think, is in Key controlled movement. When dealing with key movement you commonly run accross multiple if statements of if Key.UP this, if Key.DOWN this, etc. This can be reduced to just a couple of lines
my_mc.speed = 5;
my_mc.onEnterFrame = function(){
this._x += (Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT)) * this.speed;
this._y += (Key.isDown(Key.DOWN) - Key.isDown(Key.UP)) * this.speed;
}
since Key.isDown returs true or false, you get more or less 1 or 0. That gives you, from that equation, either -1, 0 or 1, all which will give you a distance of movement when multiplied by a clips 'speed'.
PS. these are 'tricks' not 'good programming practices' ;)
jbradley
02-04-2003, 02:10 PM
Originally posted by senocular
Function Appending (MX)
Heh. Originally posted as an idea on Flashcoders, many moons ago. :)
Anyhow, appending functions like this can work sometimes, but can cause issues later on. If the code is ever run without initialization (ie, run again but not initialized with the =function1), you will be appending multiple events that are identical to the enterFrame event.
Best practice is to, again, use function tables and have your onEnterFrame() event be the caller for the table listing. This way, you have complete control over order of execution, removal or joining of new functions, or additing of functions at the beginning or end of the chain.
Luckily MX allows Function 'object' prototypes which makes this job a lot easier. We can write two versions of whats above, one to insert the new function before the old function, and one to insert the new function after the old function. If you have one funcion relying on results from another, you'd want to be able to control which one gets called first on an onEnterFrame...
You might want to be aware that everything in MX is an object. Actionscript, like Javascript is an object-based language (not class based like C/C++). Note that I said "object-based" and not "object-oriented," as those terms are not really interchangeable.
Functions and everything else are already objects. This is why I can parse _global as an array (because it is an array) and why I can return methods of a function because those methods are more objects of the function object (objects of an object).
This functionality allows us to do some very interesting things with the language, though for sake of clean coding and portability, many of those things are not very good ways of approaching the situation (as this and my last post point out).
cheers.
jbradley
02-04-2003, 02:22 PM
True & False as Numbers (Key movement)
I'd think it's safe to say that any programmer should know that true == 1 and false == 0, a fundamental aspect of coding ... and one which can be useful at times.
// can be shortened to
dailyAllowance = 10 * isAMember;
Yea, you could do that. But it's not legible. No one is going to notice that if they see it in your code. It works, but it's not really good practice, as you said yourself ... :)
Better, with one line:
dailyAllowance = isAMember ? 10 : 0;
This queries the isAMember, if true it evaluates the first element before the semi-colon, else it evaluates the latter element. This is better because it can be compounded with functions easily, and still keep the code simple and easier to understand (provided the reader knows the semantics of the language).
dailyAllowance = isAMember ? getAllowance() : 0;
... food for thought.
senocular
02-04-2003, 02:23 PM
well Ive lost all interest in posting anything else here :-\
jbradley
02-04-2003, 02:36 PM
Originally posted by senocular
well Ive lost all interest in posting anything else here :-\
Oh, come on now. Your help is definitely appreciated. I just wanted to clarify your posts because you are posting on more complicated subjects ... things that are routinely discussed on lists like Flashcoders. As with all things, there are many different ways of approaching a concept. In the case of daisychaining and callbacks, there are already standard and accepted methods in OO. Deviating from those standards can cause confusion to someone that's getting deeply involved with OO design.
The biggest headaches can come when you get into full-scale development of large component and applications in MX is the deviation from standard coding practices (or poor practices).
I think your tips are wonderful, they just needed to be reworked.
Don't stop dude ... keep it up. This is a productive discussion and I learned a few new approaches to the same idea. Sure, there can be better ways to do something ... but sometimes all that matters is that something works. Know what I'm sayin?
peace.
lostinbeta
02-04-2003, 10:51 PM
Best of Kirupa definitely, but I left a redirect in the actionscript section.
Very interesting :) And Sen, it's good to exchange points of view sometimes, no?
Let this be the thread for the future AS-tricked, shall we?
I've just found this at as.org, in a post by FlashGuru (my God he's good :crazy: ):
Array duplication made easy
To make a duplicate of an array proves more difficult than it seems. For instance, if you try this piece of code:a=[1,2,3];
b=a;
a.pop();
trace ("a="+a);
trace ("b="+b);it will return a=1,2
b=1,2even though you've applied the method Array.pop on a only. b is a reference to a, an alias if you prefer, not a copy.
So here's a little prototype to solve the problem. All you have to do is use the Array.splice method, with no parameter to keep the whole array.// Credits: FlashGuru
Array.prototype.copy=function(){
return this.slice();
};
tester=[1,2,3,4];
tester2=tester.copy();
jbradley
02-05-2003, 03:57 PM
That's a good snipped of code if you want to have a completely separate function for doing that - but it's also identical to re-referenceing an already defined function in AS.
Try:
a = [1,2,3];
b = a.slice();
Or:
a = [1,2,3];
b = a.concat();
Or:
a = [1,2,3];
b = a.join();
These will execute much faster if you've got huge arrays to deal with (say 256x256).
peace.
senocular
02-05-2003, 07:41 PM
Theres a place for comprehension and theres a place for speed. Chances are, for basic array manipulation you arent going to have to worry about speed in which case the function overhead for that definition is negligble. Seeing copy being used with an array will let you know right away that you are copying the array, slice doesnt indicate that as clearly. Besides, if it was speed you are after youd give the function a single character name.
Also know you can use this:
Array.prototype.copy = Array.prototype.slice;
It doesn't call slice within copy but rather just gives another name for it.
tester=[1,2,3,4];
tester2=tester.copy();
trace(tester2); // 1,2,3,4
jbradley
02-05-2003, 09:34 PM
Originally posted by senocular
Theres a place for comprehension and theres a place for speed.
Also know you can use this:
Array.prototype.copy = Array.prototype.slice;
It doesn't call slice within copy but rather just gives another name for it.
tester=[1,2,3,4];
tester2=tester.copy();
trace(tester2); // 1,2,3,4
Good job!
But speed does have it's place. I wrote a Mandlebrot explorer that renders a 400x200 sized image to the screen using lineTo methods. To calculate the fractal and escape value for each pixel about 400 lines are called, about 100 times (per pixel!). Actionscript execution is very fast, but the number of lines of AS called here get way out of hand ...
80000 * 400 * 100
3.2e9 (that's 3.2 billion)
If I can save a few milliseoncds by not creating a reference to another function (which has to be checked each time it's called), then I'm a happy camper.
But, I tend to do things out of the norm sometimes ... :)
[out of the subject]You scared the **** out of me when I saw you answered my post, jbradley :P
Anyway, thank you both, that's very interesting, and will definitely be included in the official AS trick, if I ever get to write it :).
And jb, can I see that Mandelbrot of yours? I tried to do one for the Bit-contest some time ago, but another French beat me to it :evil: [/out of the subject]
I like this thread :beam:
jbradley
02-06-2003, 06:33 AM
Ok, I couldn't find my larger version so I'm uploading a smaller and more compact one. I decreased the code significantly in this version since it's just supposed to show the idea, instead of being a full fractal explorer (like my other version), where you can view different types of fractals. This is just the Mandlebrot.
The settings are as follows:
Smoothness
The higher the number the more accurate a picture of the escape velocity of any point inside or outside the Mandlebrot fractal set. When the function is calculated over and over, a certain value approaches infinity. The coloring of the fractal, called domain coloring, maps this speed of infinite approach. The higher the smoothness the more accuracte the calculations.
Trace Size
'Pixels' on the screen are drawn with lines. Line size can range from 1x1 to 5x5 (one number provided, the size of the line 'square). At 5 there's a pixel line that gets skipped ... oh well. Stick with 4 or lower, though 5 can be cool to look at, and with a smoothness of around 50 renders very fast.
Stop
Just stop the current rendering. Very processor intensive application, so the clicking may take a second to register in the application.
Resume
Start from the last place you were in the rendering. You can change the trace size during rendering, but it messes everything up.
Redraw
Redraw the screen. There's a problem sometimes when you click Render with the screen not being cleared. This will fix that if you change line size and the application starts slowing down (lines over lines). Just click this and it should clear and dump the old drawing. If clicked during a trace, it'll clear and start where the trace left off ... so let the rendering finish.
Render
Initializes everything. Gotta click this to get anything to happen really. :)
Clicking in the screen will zoom the view. Zoom is inifinite, and approximately 10x each time you click. So, you can zoom way way in to the set. Sometimes a click outside the view will register (don't know why and didn't care to fix it in this 10 min. compacted version). Tread lightly but have fun exploring.
Enjoy.
senocular
02-07-2003, 08:50 AM
Originally posted by jbradley
First thing: don't 'daisychain' like that.
Returning "this" as a property will return the entire object. This method will cause infinie recursion if you are not extremely careful with your code
thought Id ask...
can you give an example of this please jbradley?
thanks
couldnt think of one off the top of my head when you posted, came to mind again today and thought Id throw it out there.
senocular
02-10-2003, 06:58 AM
In Flash AS Editing (MX)
Code Formatting/Indention
There are a couple of new ways to help you in editing Actionscript in Flash MX (expert mode). One feature is Auto-indent which automatically indents your script as you type. Along with this you have Auto-format (ctrl+shift+F) which take a script and correctly indent the lines for you appropriately if they arent already. This is good for when you've copied and pasted a large block of code off of the internet (like this board) or some other source which, then, did not have porper formatting. Auto-format will approriately format it for you making it easier to read. One other method of formatting (indenting text) which surprisingly flashguru only recently figured out, is Selection indenting. Given any number of lines of selected text, when hitting the tab key, those lines will indent by one tab stop outward. This is useful for encasing a large block of code in, say, an 'if' statement, with the ability then to select that block and properly indent it. Shift-tab will reverse indent.
Code Duplication
One way to save on typing is code duplication. This especially for things such as onClipEvents where you may have a few events on a single movieclip (and therefore a single page of code) and you may find it easier to, instead of retyping "onClipEvent(){} for each block, just duplicating the first one over again for reuse. This is nothing new in the world of computers - duplication but it may be something which you havent mastered yet and is worth using if you're finding yourself coding a lot ;) There are 2 basic ways of duplication. One is copy-and-pasting. I use the CPP method - or Copy Paste Paste. What this is, is selecting code to copy and then, pasting twice immediately thereafter. Once to replace the currently selected text, and another to create the duplicate text. Essentially this is the same as deselecting and pasting once, but this just does it with a little more ease because it doesnt require the use of the mouse. Should you need to position the new text somewhere else, being that its selected, you can just click and drag it to a new location. The other way is Duplication through dragging. In Windows the key is ctrl, and macs I believe its command, but when moving selected text with this key down, a copy of that text will be made as opposed to that text being moved. This however, only works when the cursor is moved outside of the selected text so when you need duplicate some code beneath the original, CPP might be better fit. However, duplicating this way with dragging also lets you duplicate text while maintaining the contents of your clipboard which you may want for later use. This technique is probbaly best used on variable names where to reuse/copy that variable, simply find a previous instance of it, double click and ctrl+drag it to the new location to make a copy. At times, this may save you some typing and some time.
Code Re-arrangement
As noted before, you can select code and move it around with your mouse by dragging. This can be good to change the orders of your script for whatever purposes are necessary. In doing this, however, you may want to be sure to also copy the return character of the line above the first line of your selection (selecting from the bottom up). This way you would just need to move your code snippet. at the end of the line which it needs to follow and the line spacing will remain consistent. If this is a little hard to do sometimes, or just as a possibly simpler method of rearrangement, also consider going into normal mode (ctrl+shift+N) to move things around. There, for an if statement for example, you wouldnt have to select the whole statement but just the if line. Moving that will move the entire statement.
For more options, be sure to look in the Actionscript Panels menu in the far right of the panel titlebar if you havent already.
senocular
02-11-2003, 07:31 PM
^ along those same lines are the object suffixes (MX). Just in case you didnt know, MX will provide a popup of 'hints' for your type code as you are coding. To get these hints for your object instances there are certain naming conventions you can follow that would give you these popup hint boxes. They are all listed in Flash help but Ill throw them here as well.
_mc
MovieClip
_array
Array
_str
String
_btn
Button
_txt
TextField
_fmt
TextFormat
_date
Date
_sound
Sound
_xml
XML
_xmlsocket
XMLSocket
_color
Color
_camera
Camera
_mic
Microphone
_stream
NetStream
_connection
NetConnection
_so
SharedObject
_video
Video
Example: Using myMovieClip_mc as an instance name will give you a list of all the methods associated with movieclips when you type that instance name in the actionsript window.
senocular
02-11-2003, 08:04 PM
I posted this before in Random but I think it could use to be here as well.
Writing MX applications from Flash 5
Lets say you're stranded off somewhere and you desperately need to create an MX swf and make darn good use of the Drawing API in MX (or anything else MX) but all you have at your disposal is Flash 5. What do you do? You code your movie in Flash 5. But that wont work, will it? No, not right off, it wont. The Flash player when playing that movie will see that it is a Flash 5 movie and play it accordingly, however, what you can do is change the value which the player sees as being the movie version from flash 5 to flash MX, then anything you've written in Flash 5 thats now in that Flash 5 swf, can be read as though it were made in MX. To do this, you'll need a hex editor. A free one I use for windows (small light and fast) is frhed which can be found here (http://www.kibria.de/frhed.html). Though it may not be the editor of choice for more massive projects since it is just some guys personal learning excursion ;).
Anyway, what you would do is open the Flash 5 swf in a hex editor and change the last character in the first block of values (4th group of 2) from a 5 to a 6. ie.
46 57 53 05
to
46 57 53 06
Then you will fool the flash player into thinking the swf is Flash 6 format and it will run methods like moveTo and lineTo to actually draw on the screen - things that can be written just as easily in Flash 5, just not seen unless the swf is converted to be of Flash 6 (so note if you ever need to try this, testing your movie wont let you see Flash 6 (MX) actions).
putting
lineStyle(0,0,100)
lineTo(200,200)
in your Flash 5 movie, publishing it, converting the swf as described above, will create a flash (MX) movie built in flash 5 which draws a line from 0,0 to 200,200.
jbradley
02-11-2003, 08:35 PM
ASnative function handlers for the MX Drawing API. These will allow you to program the Drawing API functions in Flash 5.
// [MovieClip.prototype] beginFill
MovieClip.prototype.beginFill = ASnative(901, 1)
// [MovieClip.prototype] beginGradientFill
MovieClip.prototype.beginGradientFill = ASnative(901, 2)
// [MovieClip.prototype] moveTo
MovieClip.prototype.moveTo = ASnative(901, 3)
// [MovieClip.prototype] lineTo
MovieClip.prototype.lineTo = ASnative(901, 4)
// [MovieClip.prototype] curveTo
MovieClip.prototype.curveTo = ASnative(901, 5)
// [MovieClip.prototype] lineStyle
MovieClip.prototype.moveTo = ASnative(901, 6);
// [MovieClip.prototype] endFill
MovieClip.prototype.endFill = ASnative(901, 7)
// [MovieClip.prototype] clear
MovieClip.prototype.clear = ASnative(901, 8)
I just referenced them really quick above. You may need to finagle them a bit to get them to work, but in the Flash 6 Player, these ASnative functions are direct handlers to the API elements. You should be able to pass in the variables as you normally would, ie. mc.moveTo(x,y)
cheers.
senocular
02-11-2003, 08:48 PM
for more on ASNative see
http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASNative
senocular
02-12-2003, 07:47 PM
_name as a variable/argument
One thing you can do to possibly help you streamline some actions in flash is to base your unique actions off of a movieclip/button name.
For example, suppose you have 3 buttons in your movie and you need them each to load in one of 3 images named bob.jpg, joe.jpg, or terry.jpg. What you can do is name your buttons bob, joe and terry and then do something this.
openNameImg = function(){
getURL(this._name +".jpg", "_blank");
}
bob.onRelease = joe.onRelease = terry.onRelease = openNameImg;
each use the same function but get different results based on their name opening up the corresponding image.
Basically all your doing is treating the _name as a string variable you set using the property panel :-\ but it can be helpful none the less.
Be aware that _name is NOT read-only and can be changed during runtime.
Originally posted by senocular
Be aware that _name is NOT read-only and can be changed during runtime.
Changing the instance name during run-time..?
senocular
02-13-2003, 02:04 PM
Originally posted by h88
Changing the instance name during run-time..?
if you want
senocular
02-14-2003, 10:04 AM
Easy strip HTML tags
put your html (nothing too complicated ;)) into a dynamic, html-enabled, textfield as .htmlText, then extract .text and your tags are stripped. Seems obvious but can be overlooked.
senocular
02-14-2003, 11:46 AM
Non object Object objects
Every object you make inherits from the generic object Object. So if you have any object prototypes, all objects of whatever type they are will have access to them since they inherit from the object Object.
If you dont want your object to inherit from the object object (such as the object you get in return from Color.getTransform()) then you can do one of two things.
1. create your object with the Object object but without the new keyword:
myObject = Object();
2. nullify the __proto__ of your object so it doesnt reference and prototype object.
myObject = new Object();
myObject.__proto__ = null;
This can be helpfull, for example, if you want to cycle through an object with a for..in and not have to worry about prototype functions popping up in the loop and without the need to mess around with ASSetPropFlags (http://www.flashguru.co.uk/000037.php).
Now in the case of something like Color.getTransform(), if you need it to reference some prototype function, you can set the getTransform object's __proto__ to be any object prototype you want so it has access to those methods i.e.
Object.prototype.getbb = function(){ trace(this.bb); }
c = new Color(my_mc).getTransform();
c.__proto__ = Object.prototype;
c.getbb(); // traces bb prop of getTransform object
Note: using __proto__ = null will remove all prototype references, not just the object Object protos
Prevent Caching while loading your TextFiles through LoadVars (Another Trick)
I have found somewhere a new trick and thought that I should share it here:
//Create a new LoadVars class:
myLoadVars = new LoadVars();
//Create a dummy Random Variable through new Date and getTime
myLoadVars.random = new Date().getTime();
//Now here comes the trick
myLoadVars.sendAndLoad("TextFile.txt", myLoadVars, "GET");
//so now it would look like 'TextFile.txt?' then it attaches the 'random' variable
myLoadVars.onLoad = function(success){
trace(this.message)
}
//Notice that you can't use one LoadVars object for this method,
Please Note that this Won't work on Local TextFiles!
Check updates page 3.
senocular
02-14-2003, 02:04 PM
Originally posted by h88
Please Note that this Won't work on Local TextFiles!
Which is why you check the _url of the movie before doing that ;) something like
String.prototype.noCache = function(){
return (_url.substr(0,4) != "file") ? this + "?"+new Date().getTime() : this;
}
// ...
loadMovieNum(("myMovie.swf").noCache() ,1);
Good idea, This thread is the coolest so far!
It's not going to work. sendAndLoad will load the textfile using GET (And here goes the trick), and therefore,'textfile.txt?' (With '?'), it needs some work tho, but i guess am going now.
Ok, here (edited):
_global.GetMethod = function () {
return (_url.substr(0, 4) == "file") ? "POST" : "GET";
};
myLoadVars = new LoadVars();
myLoadVars.preventCache= new Date().getTime();
myLoadVars.onLoad = function(success){
trace(success ? this.message : "error");
}
myLoadVars.sendAndLoad("TextFile.txt", myLoadVars, GetMethod());
The subject say's it all, really neato code by Jonas. :)
// MovieClip.onDoubleClick Event v1.0
// by Jonas Galvez (jonas@onrelease.org)
MovieClip.prototype.addProperty("onDoubleClick",
function() { return this.$onDoubleClick },
function(f) { this.$onDoubleClick = f; Mouse.addListener(this); }
);
Mouse.onMouseDown = function() {
if(this.last_click == undefined) this.last_click = 300;
if(getTimer() - this.last_click < 300)
this.broadcastMessage("$onDoubleClick");
this.last_click = getTimer();
};
Mouse.addListener(Mouse);
// this.onDoubleClick = function () {
// trace("DoubleClick!");
// };
lostinbeta
02-25-2003, 12:12 PM
$onDoubleClick
I can only assume you got AS mixed up with PHP here. Would I be correct? If so, you might want to change that :P
If not... then what is the $ for?
It is an indicator to anyone else using the code that these are 'internal' properties. Other than that, it's just a character and we can use them with the variable names.
senocular
02-25-2003, 02:04 PM
That dbl click code doesnt check for tripple click (clicking three times fast will result in 2 doubleclicks)
MovieClip.prototype.addProperty("onDoubleClick",
function() { return this.$onDoubleClick },
function(f) { this.$onDoubleClick = f; Mouse.addListener(this); }
);
Mouse.onMouseDown = function() {
if(this.last_click && getTimer() - this.last_click < 300) this.broadcastMessage("$onDoubleClick");
else this.last_click = getTimer();
};
Mouse.addListener(Mouse);
wont be a double double on a tripple ;)
^ Clicking once will result in a doubleclick, once the movie is immediately exported!
senocular
02-25-2003, 02:25 PM
so did the other one :P
senocular
06-05-2003, 09:22 AM
Custom MovieClip Properties (MX)
These Ive been using a lot. They make it easier for you to manipulate movieclips with new properties defined by you. Example: _left, _right, _top, _bottom, _xcenter and _ycenter:
MovieClip.prototype.addProperty("_left",
function(){
return this.getBounds(this._parent).xMin;
},
function(x){
this._x += x - this.getBounds(this._parent).xMin;
}
);
MovieClip.prototype.addProperty("_right",
function(){
return this.getBounds(this._parent).xMax;
},
function(x){
this._x += x - this.getBounds(this._parent).xMax;
}
);
MovieClip.prototype.addProperty("_top",
function(){
return this.getBounds(this._parent).yMin;
},
function(y){
this._y += y - this.getBounds(this._parent).yMin;
}
);
MovieClip.prototype.addProperty("_bottom",
function(){
return this.getBounds(this._parent).yMax;
},
function(y){
this._y += y - this.getBounds(this._parent).yMax;
}
);
MovieClip.prototype.addProperty("_xcenter",
function(){
var b = this.getBounds(this._parent);
return (b.xMax+b.xMin)/2;
},
function(x){
var b = this.getBounds(this._parent);
this._x += x - (b.xMax+b.xMin)/2;
}
);
MovieClip.prototype.addProperty("_ycenter",
function(){
var b = this.getBounds(this._parent);
return (b.yMax+b.yMin)/2;
},
function(y){
var b = this.getBounds(this._parent);
this._y += y - (b.yMax+b.yMin)/2;
}
);
(the formatting is a little off here in the forums so the code looks a little weird ;))
These all simplify the use getbounds by making these properties associated with movieclips that allow you to check or set those properties. _left, _right, _top, and _bottom represent those positions of the movieclip's bounding box whereas _xcenter and _ycenter are the absolute center positions of the movieclip (which may or may not be equivalent to _x and _y depending on whether or not the registration of the movieclip is at the center or elsewhere).
I think the biggest advantage comes in the ability to easily set the _top or _left of a movieclip etc. This makes alignment a snap.
I recently posted my little point class on layer51 protos too. Nothing special, but its something I use a lot. It adds to the movieclip object _loc, _mouseLoc, _cursorLoc and _direction.
You can find it here http://proto.layer51.com/d.aspx?f=919
Same concept there, using Flash MX's addProperty function to add properties to movieclips making your life easier.
lostinbeta
06-05-2003, 12:37 PM
Very good to know =)
Simple and practical, that's brilliant! :)
senocular
06-15-2003, 10:41 PM
a little editing trick in
Commenting
- when commenting large blocks of code you may want to easily comment or uncomment any one block without too much effort.
The most painful way is using // It comments out one line so allows you to be selective, but in terms of larger blocks you're out of a lot of typing.
In favor of that is /* */ for commenting a lot of lines at once. These are much faster but can still be a little cumbersome since they have both a beginning and and an end. So in turning these on and off you would have to add/delete these at bot the front and the end of the code block deleted.
The trick here is using both // and /* */ together. In doing this, you can create a /* */ block that is easily commented or uncommented by typing/deleting one character. JOY =) Example:
/*
trace("this");
trace("is");
trace("commented");
//*/
//*
trace("this");
trace("is NOT");
trace("commented");
//*/
(note the forum formatting wont show the second part uncommented though it really is in Flash)
The only change is the / infront of the first /* This makes the /* a commented * with // The ending //*/ is a commented ending */ but if the /* is in effect, the // commenting on that line wont 'count' and that */ will properly terminate the comment block
Its a little thing but it can be useful when testing and debugging when you find yourself doing this a lot with different sections of code.
senocular
06-28-2003, 06:52 PM
Code Base
Keeping foundation code out of the way
- When dealing with rather large projects (or even smaller ones for that matter), you may be providing for your project a set of base classes or prototypes from which a lot of your structure will be based on or will take advantage of. If not linked in an external file and added with a #include, then usually this code is just pasted somewhere in the first frame of your movie. This, however, can at times be a bit cumbersome having large blocks of code sitting there in your main timeline taking up space and just getting in the way. This doesnt have to be the case though.
To get that junk out of your way completely, giving yourself a completely fresh (but enhanced) Flash pallete to work from, you can decide to place your code in a movieclip. This movieclip will then act as a container for your code; a place for it to be stored yet still be available for use on the main timeline or where ever you decide you may want to use it.
What to do:
make a movieClip:
be sure to export for actionscript and do so in the first frame. The linkername is irrelavent - just be sure not to give it a name that might be used by something else.
in that movieclip place your code any way you want, in whatever layers you want (to help with organization and seperation of classes or whathaveyou)
before all the script in that movieclip put a #initclip and after all the script put a #endinitclip
Thats it. The movieclip does not have to be on the stage; It can operate just fine out of the library and not incovenience you any other respect. Now you have a completely clean flash movie to work from (aside from that clip in the library) with enhanced features all outlined in you classes/prototypes youve put in your code base ... container ... movieclip ... thing. :)
ahmed
06-28-2003, 10:11 PM
that's awesome Sen, very useful =)
jedhu0920
03-17-2005, 05:57 PM
Does loading code from movieClips in the library work with variables and functions, or only classes and prototypes?
senocular
03-17-2005, 06:08 PM
it works with anything, not just classes.
jedhu0920
03-22-2005, 01:25 AM
so if i define a variable in a movieclip, would i get to it by the following path? _root.variable
senocular
03-22-2005, 05:14 AM
if within an #initclip - #endinitclip block and that movie clip was set to export in the first frame.
theflash
04-18-2005, 01:36 AM
This is a little trick I found today itself...
When I create MC and inside the MC itself I do the following...
//Any event handler (mouse)
this.onRelease = function()
{
_parent.klikHandle(_me);
}
stop on frame1 , stop on frame 2...
at this stage the RollOver effect will not play second frame....
but when u give 2nd frame a lebel "_over" it will go to frame 2 on the RollOver event...
But remember you should have assigned one of the onMouse handlers... be it release or press or like that.....
I hope I explained in a right way........
theflash
04-18-2005, 09:14 AM
well more on my prev note:
use the same names as on buttons "_up" 1st frame, "_over" 2nd, "_down" 3rd in the MC and that becomes your button :) automatically.... (given stop() on first frame and some mouse event defined, (any one) )
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.