00:00
00:00
Newgrounds Background Image Theme

809118566 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Little tricks and tidbits

9,019 Views | 65 Replies
New Topic Respond to this Topic

AS: Little tricks and tidbits 2005-11-23 15:21:49


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:

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!

Response to AS: Little tricks and tidbits 2005-11-23 15:25:09


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 :)

BBS Signature

Response to AS: Little tricks and tidbits 2005-11-23 15:28:47


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 ++;


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-23 15:29:44


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.


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-23 15:32:15


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.

Response to AS: Little tricks and tidbits 2005-11-23 15:32:45


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;...

Response to AS: Little tricks and tidbits 2005-11-23 15:34:27


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)

Response to AS: Little tricks and tidbits 2005-11-23 15:42:04


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 :)

BBS Signature

Response to AS: Little tricks and tidbits 2005-11-23 15:51:16


first of all, use SAY instead of Trace, Liam ;-))
And yes, since I used global.

Response to AS: Little tricks and tidbits 2005-11-23 18:05:39


lol stop putting crap in the AS thread soon noone is going to be able to find the important stuff


- Matt, Rustyarcade.com

Response to AS: Little tricks and tidbits 2005-11-23 18:15:59


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);

Response to AS: Little tricks and tidbits 2005-11-23 18:30:20


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

Response to AS: Little tricks and tidbits 2005-11-23 19:28:38


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

Response to AS: Little tricks and tidbits 2005-11-24 02:00:27


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


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 02:16:35


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...

Response to AS: Little tricks and tidbits 2005-11-24 02:20:32


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...


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 02:23:01


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

BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 02:29:28


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... :]

Response to AS: Little tricks and tidbits 2005-11-24 02:34:20


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...

BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 02:51:52


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

Response to AS: Little tricks and tidbits 2005-11-24 02:53:34


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?

BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 03:54:59


yay! thank you very much.... *looks around and strolls off*

Response to AS: Little tricks and tidbits 2005-11-24 04:40:28


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

Response to AS: Little tricks and tidbits 2005-11-24 04:43:48


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...

Response to AS: Little tricks and tidbits 2005-11-24 05:11:40


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.

Response to AS: Little tricks and tidbits 2005-11-24 05:23:00


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


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 08:16:50


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

Response to AS: Little tricks and tidbits 2005-11-24 08:20:31


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.


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 09:10:53


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.


BBS Signature

Response to AS: Little tricks and tidbits 2005-11-24 09:20:09


At 11/24/05 09:10 AM, Claxor wrote: function isDiv (a:Number, b:Number):Boolean {
return a/b == Math.floor(a/b);
}

Well, it's not shorter but anyway.

I know you could use that, but the modulo way is a teeny bit better, and shorter to type, so I can't really see any point to use that way instead.

In any way, that function was only for showing a use for the modulo operator. If I need to check a number divisibility by another number, I wouldn't use that function, just the equation.


BBS Signature