Forum Topic: AS: Little tricks and tidbits

(4,570 views • 68 replies)

This topic is 3 pages long. [ 1 | 2 | 3 ]

<< < > >>
None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 11/23/05 03:21 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

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!


None

cherries

Reply To Post Reply & Quote

Posted at: 11/23/05 03:23 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,573

the stop;

stop;

really useful for menus :D

I'm not being sarcastic

None

liaaaam

Reply To Post Reply & Quote

Posted at: 11/23/05 03:25 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,541

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


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 11/23/05 03:25 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Umm, not really an AS thread but Ill give it a try:

Email button:

on(release){
getURL("mailto:me@me.com");
}

The name property:

_name="blah"

What it does:
Dynamically gives a MC an instance name.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Toast

Reply To Post Reply & Quote

Posted at: 11/23/05 03:28 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,924

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


None

Rantzien

Reply To Post Reply & Quote

Posted at: 11/23/05 03:29 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 11/23/05 03:32 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

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.


None

schorhr

Reply To Post Reply & Quote

Posted at: 11/23/05 03:32 PM

schorhr NEUTRAL LEVEL 11

Sign-Up: 05/28/02

Posts: 9,029

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


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 11/23/05 03:34 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

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)


None

liaaaam

Reply To Post Reply & Quote

Posted at: 11/23/05 03:42 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,541

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.


None

schorhr

Reply To Post Reply & Quote

Posted at: 11/23/05 03:51 PM

schorhr NEUTRAL LEVEL 11

Sign-Up: 05/28/02

Posts: 9,029

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


None

Rustygames

Reply To Post Reply & Quote

Posted at: 11/23/05 06:05 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

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

- Matt, Rustyarcade.com


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 11/23/05 06:15 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,559

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

My social worker says im special!

BBS Signature

None

EmoNarc

Reply To Post Reply & Quote

Posted at: 11/23/05 06:30 PM

EmoNarc LIGHT LEVEL 11

Sign-Up: 05/01/05

Posts: 1,917

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


None

rEvolution27

Reply To Post Reply & Quote

Posted at: 11/23/05 07:28 PM

rEvolution27 LIGHT LEVEL 04

Sign-Up: 10/16/05

Posts: 828

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


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 11/24/05 02:00 AM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

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

None

Jcrypt

Reply To Post Reply & Quote

Posted at: 11/24/05 02:16 AM

Jcrypt EVIL LEVEL 11

Sign-Up: 08/23/05

Posts: 926

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


None

authorblues

Reply To Post Reply & Quote

Posted at: 11/24/05 02:20 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

authorblues

Reply To Post Reply & Quote

Posted at: 11/24/05 02:23 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Jcrypt

Reply To Post Reply & Quote

Posted at: 11/24/05 02:29 AM

Jcrypt EVIL LEVEL 11

Sign-Up: 08/23/05

Posts: 926

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


None

authorblues

Reply To Post Reply & Quote

Posted at: 11/24/05 02:34 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Jcrypt

Reply To Post Reply & Quote

Posted at: 11/24/05 02:51 AM

Jcrypt EVIL LEVEL 11

Sign-Up: 08/23/05

Posts: 926

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


None

authorblues

Reply To Post Reply & Quote

Posted at: 11/24/05 02:53 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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?

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Jcrypt

Reply To Post Reply & Quote

Posted at: 11/24/05 03:54 AM

Jcrypt EVIL LEVEL 11

Sign-Up: 08/23/05

Posts: 926

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


None

BigBazz

Reply To Post Reply & Quote

Posted at: 11/24/05 04:40 AM

BigBazz FAB LEVEL 23

Sign-Up: 04/14/04

Posts: 241

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


None

T-H

Reply To Post Reply & Quote

Posted at: 11/24/05 04:43 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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


None

ZYX3D

Reply To Post Reply & Quote

Posted at: 11/24/05 05:11 AM

ZYX3D DARK LEVEL 21

Sign-Up: 07/31/04

Posts: 437

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.


None

Rantzien

Reply To Post Reply & Quote

Posted at: 11/24/05 05:23 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

Happy

T-H

Reply To Post Reply & Quote

Posted at: 11/24/05 08:16 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 11/24/05 08:20 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

All times are Eastern Standard Time (GMT -5) | Current Time: 11:27 PM

<< Back

This topic is 3 pages long. [ 1 | 2 | 3 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!