Ultimate Gear War
Join the alien war, prepare your gear and protect your base at all cost!
4.17 / 5.00 16,530 ViewsAS: Little Tricks and Tidbits!
Feel free to add your own little-known AS tricks to this thread.
The ?: Operator
How it works:
a==b?c=d:e=f
is the SAME as
if(a==b){
c=d
} else {
e=f
}
Can be very useful for short if statements. If there is no else part, use
:null
The Modulus (%)
I think it's called the modulus...
Well anyway it returns a remainder
a=random(1000)
a%2 //returns the remainder of a/2, so if a is odd it returns 1, if even it returns 0
Really nifty gidget. Very useful.
Ok that's all I got for now. Anyone else can post their tricks too.
Post yours!
Post yours!
Post yours!
the stop;
stop;
really useful for menus :D
I'm not being sarcastic
I used ?: and modulus to make my own rounding function:
function ceil(a:Number):Number {
return (1-a%1)+a;
}
function floor(a:Number):Number {
return a-a%1;
}
function round(a:Number):Number {
return a>(a-a%1)+0.5 ? ceil(a) : floor(a);
}//just an example of how the two can be used
Sup, bitches :)
At 11/23/05 03:23 PM, -cherries- wrote: the stop;
stop;
really useful for menus :D
I'm not being sarcastic
Erm... Don't you mean stop();?
Lol, I like posting useless codes! :D
Here's another one: _x and _y :DDD
_y +=5;
_x +=5;
There you go.
Err... ++!
Instead of var += 1; there's an easier way, just type var ++;
At 11/23/05 03:21 PM, Glaiel_Gamer wrote: a==b?c=d:e=f
I could add that it can also be used in the manner:
a = a<b?c:d;
The same as:
if (a<b) {
a = c;
} else {
a = d;
}
One nifty feature is to make use of the fact that 1 == true and 0 == false. For example:
onClipEvent (enterFrame) {
_x += Key.isDown(Key.RIGHT)-Key.isDown(Key.LEFT)
;
_y += Key.isDown(Key.DOWN)-Key.isDown(Key.UP);
}
is a simple moving system.
At 11/23/05 03:25 PM, SpamBurger wrote: Umm, not really an AS thread but Ill give it a try:
Email button:
on(release){
getURL("mailto:me@me.com");
}
I'm talking about almost unknown functions, not on and getURL.
Simple but usefull.
Ever inserted trace(); to debug your actionscript loops, functions, actions and movements? Now you are testing something different, but you need to go back and find a trace you forgott to remove?
I just use this. I dont even know if there is some shortcut these days, but I used it for ever, in older flash versions too.
in first keyframe:
_global.say=function (text){if(_root.debug){trace(text);}}
_root.debug=true;
Then just use say('bla'); instead of trace.
To disable the trace, remove or switch the debug=true to false;...
At 11/23/05 03:32 PM, schorhr wrote: _global.say=function (text){if(_root.debug){trace(text);}}
_root.debug=true;
Wait, if you declare a function globally, when calling it from a MC do you have to put the path to it or can you just go say(blah)
At 11/23/05 03:34 PM, Glaiel_Gamer wrote: Wait, if you declare a function globally, when calling it from a MC do you have to put the path to it or can you just go say(blah)
You can just use say(blah);
<i b u>PROTOTYPE</i b u>
Prototype totally owns, it's awesome.
Number.prototype.negate = function():Number {
return -this;
};
a = 6;
trace(a.negate());
Totally stupid example, but you get the idea.
Sup, bitches :)
first of all, use SAY instead of Trace, Liam ;-))
And yes, since I used global.
lol stop putting crap in the AS thread soon noone is going to be able to find the important stuff
- Matt, Rustyarcade.com
At 11/23/05 03:51 PM, schorhr wrote: first of all, use SAY instead of Trace, Liam ;-))
And yes, since I used global.
this is also a nice function, inspired by AS3
you have 3 variables: x,y,z
and you want to trace them:
you might do this:
trace(x+":"+y+":"+z); which becomes a pain in the ass:
use this instead (stealing the name 'cout' for somereason)
function cout():Void
{
trace(arguments);
}
basicly its the same thing as doing trace(new Array(x,y,z));
but instead you do:
cout(x,y,z);
At 11/23/05 03:23 PM, -cherries- wrote:
I'm not being sarcastic
if youout that... i might as well put:
the play;
play;
really useful for stuff :D
it's play();
and i didn't know the ? operator is little known, mostly because we hear so much about it here from either you or -liam-... LOL
At 11/23/05 06:30 PM, BobRicci wrote: play;
At 11/23/05 03:23 PM, -cherries- wrote: stop;
Lol, stop(); and play();
And you cal yourself asctionscripters? ;P
Here is a simple way if you need to do alot of keyDown checks....
function aKey():Boolean{
return Key.isDown(65);
}
and when used in code just do.....
onClipEvent(enterFrame){
if(aKey()){
//some action...
}
It beats having to re-type all the Key.isDown stuff oevr and over...
At 11/23/05 03:25 PM, SpamBurger wrote: _name="blah"
changing the name dynamically is a bad habit to get into.
ive run into a few problems that way, but whatever works...
while were here discussing AS techniques, id like to know:
which do most people use?
Math.ceil(Math.random()*num); or random(num);
i still use the long way, but thats because i dont trust simplicity...
At 11/24/05 02:16 AM, Cyrax88 wrote: function aKey():Boolean{
return Key.isDown(65);
}
that looks so unbelievably like my xkey class
maybe thats where its from, but im inclined to believe its mine
only because not many people use return types on their functions
YOUR xKey? If i'm not mistaken I made the topic on this to begin with and all you did was answer my question and 2 days later you made your xKey thus if you wanna get technical you stole my idea... :]
At 11/24/05 02:29 AM, Cyrax88 wrote: YOUR xKey?
thats right. it was your question i answered. glad to know you consider my solution your helpful tidbit (that wasnt sarcasm). ah well, you win. and, by the way, i created that custom xkey class that same night (it was 4:30am, the next morning, if you want to get technical ^^;)
have fun kids...
Eh, yeah either way i'm not gonna argue.. You did make the xKey class regardless and answer my function question, and I appreciate it. Just didn't want people to think i'm some code thief from your first post, as long as they know I had something to do with the subject to begin with lol
At 11/24/05 02:51 AM, Cyrax88 wrote: Just didn't want people to think i'm some code thief from your first post, as long as they know I had something to do with the subject to begin with lol
DISCLAIMER: cyrax is not a code theif.
he merely used the code that i provided to him
is that better?
yay! thank you very much.... *looks around and strolls off*
this is something i found handy with integrating flash files and html.
if you were to name your flash in the html file myfile.swf?variableOne="blah1"&variableTwo
="blah2"
then in your swf file _root.variableOne returns "blah1" and _root.variableTwo returns "blah2". Have found this handy for using the same flash file repeatedly where the only a few sections are altered rather than creating a unique file for each data it is editted in the html file.
Thanks to liljim for that one
At 11/23/05 03:21 PM, Glaiel_Gamer wrote: a=random(1000)
a%2 //returns the remainder of a/2, so if a is odd it returns 1, if even it returns 0
Really nifty gidget. Very useful.
Nonono, it really isn't. The only use Ive ever found for it is to make object snap to a grib by getting the remainder and adding/taking it, but there is an even easier way to do that anyway.
This is a shite excuse for an AS thread, come on...
At 11/23/05 03:21 PM, Glaiel_Gamer wrote: AS: Little Tricks and Tidbits!
Feel free to add your own little-known AS tricks to this thread.
AS: Main
The ?: Operator
How it works:
(...)
Can be very useful for short if statements. If there is no else part, use
null
Since "null" is too much for a nothing for me, I use 0 instead. The very same effect, but 75% shorter.
And yes, prototype is the second most-owning thingie in Flash (first are custom, registered classes). Got tired of gotoAndStop reloading frames? Use MovieClip.stopAt(frame):
MovieClip.prototype.stopAt=function(someFr
ame){
(this._currentframe!=someFrame)? this.gotoAndStop(someFrame):0;}
The () are indeed needed (at least in Flash MX -6-, dunno MX 2004 -7-), or else it would check only the truth of the second part of the equality; so, if someFrame!=0, the ? would return true, whatever the _currentframe is.
At 11/24/05 04:43 AM, T-H wrote: Nonono, it really isn't. The only use Ive ever found for it is to make object snap to a grib by getting the remainder and adding/taking it, but there is an even easier way to do that anyway.
You could also check if something is evenly divisible by something:
function isDiv (a:Number, b:Number):Boolean {
return a%b ? false : true;
}
Usage:
trace(isDiv (30, 10)); // true
trace(isDiv (35, 10)); // false
trace(isDiv (40, 10)); // true
At 11/24/05 05:23 AM, Rantzien wrote:At 11/24/05 04:43 AM, T-H wrote: Nonono, it really isn't. The only use Ive ever found for it is to make object snap to a grib by getting the remainder and adding/taking it, but there is an even easier way to do that anyway.You could also check if something is evenly divisible by something:
function isDiv (a:Number, b:Number):Boolean {
return a%b ? false : true;
}
Very true, but my point being: I've never had to do that, and can't think of a time when I will.
Usage:
trace(isDiv (30, 10)); // true
trace(isDiv (35, 10)); // false
trace(isDiv (40, 10)); // true
At 11/24/05 08:16 AM, T-H wrote: Very true, but my point being: I've never had to do that, and can't think of a time when I will.
Point taken. I have had to do it a couple of times, although I can't remember what I was doing at the moment.
At 11/24/05 05:23 AM, Rantzien wrote:At 11/24/05 04:43 AM, T-H wrote: Nonono, it really isn't. The only use Ive ever found for it is to make object snap to a grib by getting the remainder and adding/taking it, but there is an even easier way to do that anyway.You could also check if something is evenly divisible by something:
function isDiv (a:Number, b:Number):Boolean {
return a%b ? false : true;
}
You could just do this instead:
function isDiv (a:Number, b:Number):Boolean {
return a/b == Math.floor(a/b);
}
Well, it's not shorter but anyway.