Be a Supporter!

AS: Little tricks and tidbits

  • 6,077 Views
  • 67 Replies
New Topic Respond to this Topic
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
AS: Little tricks and tidbits 2005-11-23 15:21:49 Reply

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!

cherries
cherries
  • Member since: Jun. 7, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:23:29 Reply

the stop;

stop;

really useful for menus :D

I'm not being sarcastic
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:25:09 Reply

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
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:28:47 Reply

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
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:29:44 Reply

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
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Little tricks and tidbits 2005-11-23 15:32:15 Reply

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.

schorhr
schorhr
  • Member since: May. 28, 2002
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:32:45 Reply

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

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Little tricks and tidbits 2005-11-23 15:34:27 Reply

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)

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:42:04 Reply

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
schorhr
schorhr
  • Member since: May. 28, 2002
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 15:51:16 Reply

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

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Little tricks and tidbits 2005-11-23 18:05:39 Reply

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


- Matt, Rustyarcade.com

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 18:15:59 Reply

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


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
EmoNarc
EmoNarc
  • Member since: May. 1, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 18:30:20 Reply

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

rEvolution27
rEvolution27
  • Member since: Oct. 16, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-23 19:28:38 Reply

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

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:00:27 Reply

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
Jcrypt
Jcrypt
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:16:35 Reply

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

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:20:32 Reply

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
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:23:01 Reply

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
Jcrypt
Jcrypt
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:29:28 Reply

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

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:34:20 Reply

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
Jcrypt
Jcrypt
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:51:52 Reply

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

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 02:53:34 Reply

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
Jcrypt
Jcrypt
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 03:54:59 Reply

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

BigBazz
BigBazz
  • Member since: Apr. 14, 2004
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 04:40:28 Reply

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

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 04:43:48 Reply

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

ZYX3D
ZYX3D
  • Member since: Jul. 31, 2004
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 05:11:40 Reply

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.

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 05:23:00 Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 08:16:50 Reply

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
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 08:20:31 Reply

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
Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Little tricks and tidbits 2005-11-24 09:10:53 Reply

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