Reply to topic  [ 3 posts ] 
 It is time we move on (to AS3) 
Author Message
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
 It is time we move on (to AS3)
Before reading this tutorial, I suggest you read the tutorial about functions.
You might also want to take a look at how variables work.


I know this may be a hard step for you all, but let's face it.
New technology brings new possibilities.

You're probably thinking as you read this "What the fuck, you just taught us a bunch of AS2 and now you want us to just forget all about it?!" but fret not. FOR IT WAS MY PLAN ALL ALONG TO MAKE YOU MOVE ON TO AS3! What I have taught you so far will make your transition waaaay easier.

Bet you didn't see that one coming, fucktards.

Alright, I guess you want to know why the fuck to even bother with AS3, and here's why:

AS3 is a much stricter language than AS2. That means unfortunately for beginners that their code will probably not execute.
However, as most of you want to become flash game developers you have surely already met with the typical "WHAT THE FUCK IS WRONG WITH MY CODE?" scenario when programming. In AS2, all these loose pointers to objects and variables that do not exist, do nothing but fuck with you as you spend 4 hours looking for a spelling mistake in your variable and function calls. In AS3, you will instantly know what variable was spelled incorrectly, what variables have been created, what variables are used, warn or even throw errors when you use something incorrectly.
So in other words, although it is harder for beginners to get a game running, it is soooooooooooooooooo much better for the more experienced.

In AS2, the compiler does not detect any memory reference problems, meaning you can access any kind of variable at any time. Even ones you haven't created, and there will be not a single warning about it. In AS3, you will be stopped before even getting it to run, and the script will then be aborted, letting you know that you have fucked up badly.

AS3 has a lot of new APIs that enable cool blurring and special effects, it even has an api for 3D development, meaning that with AS3, you can create a 3D game. Yes, a real one. Loading in 3D models and putting them in a 3 dimensional field, and having a 3d camera moving around the scene.

In AS3 you can be more specific in the data types you want to use. In AS2 you use for example the data type "Number" (var desu:Number)
In AS3 you can create a variable of the type int, meaning an integer. A non-floating variable that cannot be a fraction. (var desu:int)
I'll explain this further later on in the tutorial.

AS3 is more object oriented than AS2. This doesn't matter to you at the moment, but to any experienced programmer, having the script language be an object oriented language means that they will instantly know how to implement it. Object Oriented Programming is basically a set of rules to make the program very easily extendable, readable and all that shit, in exchange for memory allocation and a small (not noticeable) performance loss.

How this actually does affect you is the following:
You will learn Object Oriented Programming easier.
Learning AS3 will make learning any other object oriented language a lot easier.
Creating instances of objects can easily be done following the same set of rules at any time.
Anyone will be able to look at your code and almost instantly get a high view on what it is. (This means your future self as well.)

But enough talk. Let us begin immediately. I will go through everything you know in AS2 and turn it to AS3.



We begin with Variables.
They are basically the same as in AS2.

var aids:Number;
aids = 5;

here's what's different. In AS3 you also have a more specific datatype for integers. Although you could use Number for every kind of number, it is highly encouraged to use the type "int" for any integer (whole number) as it allocates less memory and is faster when computing.
A perfect example of where to use the integer type would be in the for loops. for loops pretty much always go from one whole number to another before finishing, so starting the for loop like this is great:
Code:
for(var desu:int = 0; i < 34; i++)
{
   
}


there also exists an unsigned integer type called uint. an unsigned integer ranges from 0 to very far while a signed ranges from a big negative value to a big positive value. if your for loop never iterates with a negative number, using uint instead of int is good if the loop needs to be very VERY big. in performance and memory, there is no difference between int and uint, except for that you may have to convert the uint to a regular int when doing comparison or adding/substracting/whatever the fuck.

But as I said, you are still free to use the old datatypes as they still exist.



_root, _parent

_root is no more in AS3. But do not worry. You can still access global variables, objects and functions. In AS3 it is just called "root"
and to use it the same way you used _root, you must typecast it into a movieclip.
If you "typecast" (kind of) root as a movieclip you can access it's content exactly the same way you would access a movieclip.
There are several ways to do this.
The lazy man's way would be to just use "Movieclip(root).hello" to access the variable "hello" in root.
"Movieclip(root)" treats root as if it is a movieclip at the exact moment it is used.

The other way of doing it is to create a movieclip variable and have it store a pointer to the movieclip casted root object.

Code:
var myNewRoot:MovieClip = Movieclip(root);


By doing this, you can now access and control root like any object.
Code:
trace(myNewRoot.aids);

The code above will access the variable "aids" stored in root and trace it.
However, note that the myNewRoot variable only exists where you created it, meaning if you created it in root, you only have access to it in that particular way from root. If you are inside an object and writing the code, you will not have access to myNewRoot as it does not exist in that movieClip. You could always create another Movieclip variable of root in there and access it the same way.

If you do not want to bother with having to take care of a variable for root everywhere, just do the lazy man's sollution and replace every "_root" from your AS2 code with "Movieclip(root)".

Why is this better?
Here's why:
A Movieclip is an object. An object can be extended into another object with the same variables and functions plus some more. You can in AS3 extend root to become more than a movieclip and perform big changes to it, or simply just use it as you would use any movieclip. All functions in the movieclip class can be applied to root as well, and if you typecast it into something other than a movieclip, you can use those features instead. To fully understand this part, I recommend you to re-read this when you have mastered one of my next upcoming tutorials about object oriented programming.

In some cases you can also use "stage" instead. the stage is basically the root, but not a movieclip so you cannot perform any movieclip actions using "stage" such as gotoAndPlay(), however, later on as we get to listeners you'll see that using the movieclip version of root will not work, and we will be required to use stage instead.

The same rules apply to _parent.
In AS2, _parent takes you to the movieclip that holds the movieclip you are currently inside.
If you are in the object poopCar which resides in root.
and you're on a frame script window, coding some shit.
You decide to write _parent.aids; and by doing this, access the variable "aids" in poopCar's parent (which in this case is root).

In AS3 you need to treat parent exactly the same as root, which also means you can perform the lazy man's solution and typecast it directly into a movieclip.
The following code accesses the parent's variable "aids" and assigns it the number 5.
Code:
MovieClip(parent).aids = 5;


Here's the real cool thing about all this:

Any movieclip you create, you can create a variable of in your code and access it the same way.
In AS2 when you were in root and accessed coolGuy.arm.hand.finger, you basically just accessed a hidden variable in root called coolGuy exactly the same way you access root and parent. In fact, now that you know all this, you should also know that the _root, _parent and any similar function accessed in AS2 were nothing but global variables doing exactly what we have been doing now.

Why is this better?
Are you kidding? We have just gained a lot more control!

Also, probably most things in AS2 that started with _ have been replaced or removed. For example ._x and ._y are no longer used for coordinates.
Just use regular .x and .y like it should have been to begin with.




Functions

Functions in AS3 are not that much different.

Code:
function cirno():void
{
   
   trace("Atai wa tensai desu!");
}
cirno();

function cirno(aids:Number):void
{
   
   trace("Atai wa tensai desu!"+aids);
}
cirno(999999);

function cirno():String
{
   
   return "Atai wa tensai desu!";
}
trace(cirno());

yup. Pretty much the same all the way through. So no difference here as much as you know. I need not go any deeper than this here.



onClipEvents (Listeners)


Okay, time for what you all probably think of as the biggest difference between AS2 and AS3.

In AS3 there are no onClipEvents. You cannot insert a code onto a movieclip by selecting it and writing code in the action window.
But reminding you of the thread about functions... do you really want to? Do you really want to have to add code to all those objects one at a time OR DO YOU FUCKING WANT TO PROGRAM LIKE A REAL CHAMP WITH FUNCTIONS INSTEAD? Well, it's up to you. There is no real workaround for this other than to use functions or add the code on the frames inside the movieclip.

This is how THE PROFESSIONAL CHAMPS DO IT!

First off... let us make a function to be called every frame, or what would be called in AS2 onClipEvent(enterFrame);

Code:
function cirno(e:Event):void //a regular function called cirno, expects an object of the type "Event" and returns void (nothing)
{
   trace("Atai wa tensai desu!");
}


I'll explain why we need e:Event as a parameter soon.
First, we need to do something probably completely new to you.
We need to add a listener.

What is a listener? A listener is something that reacts at specific actions. For example a button listener will react when a certain button is pressed. So instead of asking the button "is it pressed?" over and over, it simply tells you "excuse me, I am pressed" and a certain function bound to the listener is called. We are now going to create an event.ENTER_FRAME listener that reacts basically every new frame.

Code:
addEventListener(Event.ENTER_FRAME, cirno);

So, we add a new event listener, we specify the event (in this case ENTER_FRAME, and a function name (our cirno function)).

And that's it. Cirno is now called every frame yelling "Atai wa tensai desu!". Isn't it lovely?
But this is not all.
For we can do a lot that cannot be done in AS2.
We can actually remove the listener at any time using
Code:
removeEventListener(Event.ENTER_FRAME, cirno);

you could even do it inside the function itself.
For example we had a variable counting from 0 to 5, with one iteration per frame, and then call a removeEventListener() at 5, then the function will be called exactly 5 times over the course of 5 frames, then stop.
You could assign a button to toggle the eventListener at the player's command.

Same goes for onClipEvent(Load)
The AS3 listener for LOAD is ADDED_TO_STAGE.

Among these events are a bunch of other ones such as EXIT_FRAME, UNLOAD, DELETE, SELECT, COPY, CUT, and bunch of others with all different purposes. In other words, we have more control than before once again.




Mouse Listeners

How to make a button in AS3.

Simple.
You make a function for when it's clicked
then add a listener reacting to mouse clicking it.
Example:
We have a movieclip with the instance name "button"
We create 3 functions called "over", "out" and "clicked".
Code:
function over(e:MouseEvent):void
{
//what to do when mouse is over the button
}
function out(e:MouseEvent):void
{
//what to do when mouse is no longer over the button
}
function click(e:MouseEvent):void
{
//what to do when clicked
}

Notice that the functions expect not a regular event object, but a MouseEvent object.
A MouseEvent is an extended Event, except with some extra features. These are needed for the mouse event listeners to work, but other than having to say MouseEvent instead of Event, it's no different from the regular listeners.

The following code adds mouse event listeners to the button, calling the functions.
Code:
button.addEventListener(MouseEvent.MOUSE_OVER, over); //if cursor is over the button
button.addEventListener(MouseEvent.MOUSE_OUT, out); //if cursos no longer is over the button
button.addEventListener(MouseEvent.CLICK, clicked) //if the button is clicked

And there we go. We have listeners to buttons too.
You create how many listeners you want and bind different functions to different buttons with different events however you like.
Well, that's it for game input, right? Ohshi- I almost forgot the keyboard!




Keyboard Listeners

Of course you want to be able to have keyboard input in the game, and guess what.
I'll let you guess between the following things perfect for LISTENING to a keyboard.
1: keyboard listeners
B: Keyboard Listeners
the other one: KEYBOARD LISTENERS, MOTHERFUCKER
Same thing as all other listeners.
Create a function, then add a listener.
This time the event type is a KeyboardEvent.
stage.addEventListener(KeyboardEvent.KEY_DOWN, cirno); //in this case, if a key is down
the keyboard listener needs an object to bind it with, so we'll add it to the the stage (root).
This is one of those examples where root as a movieclip cannot handle listeners but root as a "stage" does.

Okay, so now we have a function that reacts to ANY key pressed. Inside the function we will need to add some logic do decide which keyboard we actually want to do something for.

This can be done with a simple if(keyEvent.whateverButtonYouWantToReactTo){} //keyEvent is the name of the event sent into the function

for example
Code:
function cirno(keyEvent:KeyboardEvent):void
{
        if(keyEvent.keyCode == 90) //keycode is the code of the key pressed. keycodes can be found by checking an ascii table. (90 is z)
         trace("hi");
        if(keyEvent.keyCode == Keyboard.RIGHT) //keyboard.RIGHT is actually a constant number representing the right key.for lazy people who do not want to look up the key code for it.
      trace("hey");
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, cirno);






That's it for today.
I will upload an example file for all these features in AS3.

Questions are always welcome.

Anything you do not understand?
Want to argue some more about whether to change to AS3 or not?
Got anything else about AS3 you would like to ask about?


Also, I did not really proof read this, so there might be some spelling errors and shit like that.
I'll clean it up later... I really need to go to bed... it's 3 am on a weekday...


_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Wed Dec 12, 2012 8:19 pm
Profile E-mail
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: It is time we move on (to AS3)
and in case any of you are wondering.

No, I will not make any more AS2 tutorials. DEAL WITH IT.

It'll all be AS3 from now on.

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Wed Dec 12, 2012 8:25 pm
Profile E-mail
Level 39
Level 39
User avatar

Cash on hand:
2,187.55

Bank:
5,250.50
Posts: 21063
Joined: Sat Feb 14, 2009 11:44 pm
Group: Sysop
Post Re: It is time we move on (to AS3)
I suppose it's to be expected...

Carry on, lolipurple. And rest well.

_________________
Image
Yeap.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
4 pcs.


Wed Dec 12, 2012 8:42 pm
Profile E-mail WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 
 

Similar topics

 
Time to reflect!
Forum: ./General Spam
Author: Pantsman
Replies: 4
Each time I look at this sig
Forum: ./General Spam
Author: BlackAmethyst
Replies: 3
LETS DO THE TIME WARP AGAIN!
Forum: ./General Spam
Author: Zared Sabretooth
Replies: 17
Im back for reals this time fuckers
Forum: ./General Spam
Author: tuypo1
Replies: 38
Top


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Mods Database :: Imprint :: Crawler Feeds :: Reset blocks
Designed by STSoftware for PTF.

Portal XL 5.0 ~ Premod 0.3 phpBB SEO