00:00
00:00
Newgrounds Background Image Theme

Peacock6k 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!

As3: Event Basics

2,818 Views | 13 Replies
New Topic Respond to this Topic

As3: Event Basics 2012-03-28 19:40:01


As3: Event Basics
-------------------------
As3: Main

Let's say you just tried out my wonderful tile-based game mechanics tutorial, and you got the grid and all working. Now what? How do you make things move? How can you tell it to move to the mouse's position? What if you want something to happen every few seconds? What the hell does Event.ADDED_TO_STAGE mean when you start a new file in FlashDevelop and such?

Essentially, I want something to happen when something else happens.

In AS3, we use Events. An Event is when something happens. We use listeners that wait until the Event happens, and the listener calls a function that does stuff. Let's look at that ADDED_TO_STAGE junk.

if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);

The first line is needed so your game doesn't die before even starting. The second line is what we're more concerned about.

addEventListener

This is how we tell Flash to be on the look out for when the event happens. We add this listener to the main file because it doesn't need special attention. Some listeners get added to the stage, timers, sprites and movieclips, etc (more on that in a bit).

Event

The Event class. There are a lot of Event classes, but the important ones for the basics are Event, MouseEvent, TimerEvent, KeyboardEvent, and ProgressEvent.

.ADDED_TO_STAGE

This is the type of Event. What you want Flash to listen for. This code is telling Flash, "Hey, when you start to addChild stuff, hit me up."

, init

This is the function that you want to happen when Flash hits up the listener. When the listener finds out that stuff got added to the stage (ADDED_TO_STAGE), it runs the function init, which starts your game up.

private function init(e:Event = null):void {
   removeEventListener(Event.ADDED_TO_STAGE, init);

(Ignore the "= null" for this, it's not needed except in special cases). This part is very important. Functions inside our Event listeners aren't normal functions. The listener passes in a parameter to our function, so we have to let it do so. Normal functions look like

stuff()

But an Event function needs

stuff(e:Event)

The "e" there can be whatever you'd like: e, event, evt; whatever suits you. This is a parameter/variable; the listener gives our function access to the Event's information. Since this "e" is a thing inside Flash, it needs a class to let the function know what is can do. "Event" is the class in this case, though it could also be MouseEvent, etc.

Earlier, I mentioned that there are a bunch of Events. All of the types are listed in the docs if you ever want to check. This will only list the very important ones.

MouseEvent is for when the mouse does stuff. When the mouse moves, moves over a thing, moves out of a thing, clicks, double clicks, etc, use this. The caveat to using these is that you have to tell Flash what object you want the events to apply to. For instance: you make a sprite, and you want the sprite to rotate when you click it. Well, you don't want the main class to rotate (if that's even possible...); so we have to tell Flash.

sprite.addEventListener(MouseEvent.CLICK, doStuff);

So now, if we click the sprite, it will call the doStuff function.

function doStuff(e:MouseEvent):void {

We can add this listener to multiple objects as well! If you want multiple things to rotate when you click each of them:

sprite.addEventListener(MouseEvent.CLICK, doStuff)
goose.addEventListener(MouseEvent.CLICK, doStuff)
player.addEventListener(MouseEvent.CLICK, doStuff)
stage.addEventListener(MouseEvent.CLICK, doStuff)

But if everything can use the same function, how to we tell the function which thing got clicked? Don't worry, Flash has you covered. Remember the "e," "event," or "evt" from earlier? Since that's now a variable inside our function, we have access to its information. Namely, the target and currentTarget properties.

e.target;   e.currentTarget

e.target is a reference to the specific thing that got clicked. e.currentTarget will always return the thing that added the listener. For instance, if you had a ton of stuff that you wanted to rotate, you could simply add the listener to the stage, where everything is located, instead of to 100 sprites. e.target will tell you what sprite on the stage you clicked. e.currentTarget will give you the stage. This is because of a process called "bubbling," which is above the scope of this tutorial.

stage.addEventListener(MouseEvent.CLICK, doStuff);

function doStuff(e:MouseEvent):void {
   trace(e.target);  // if your mouse was actually over anything, it will trace out [object Sprite]
   // if it was hovering over empty space, it'll trace out [object Stage]
   trace(e.currentTarget);  // will trace out [object Stage]
	
   if (e.target !== stage) {  //we don't want the stage to rotate when it's clicked, so exclude that
      e.target.rotation += 45;  // rotate whatever we clicked
   }
}

TimerEvent is when we want stuff to happen regularly (once per second, half-second, etc). We add this to a timer object.

var timer:Timer = new Timer(1000);
timer.start();
timer.addEventListener(TimerEvent.TIMER, everySecond);

function everySecond(e:TimerEvent) {  // function runs based on when timer finishes (once per second in this example)

KeyboardEvent is used to see which key got pressed or released. We use the keyCode property to check which key it is. This needs to be added to the stage since the main class flunked KeyboardEvent school and doesn't know how to handle it.

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

function keyDown(e:KeyboardEvent):void {
   if (e.keyCode == 37) trace("Left Button was pressed");
}

function keyUp(e:KeyboardEvent):void {
   if (e.keyCode == 37) trace("Left Button was released");
}

The key - keyCode chart is here.

ProgressEvent is used when you load stuff to see how far along it is; this is only important since Preloaders use these. Not going to go into too much detail, but e.bytesLoaded / e.bytesTotal * 100 gives you the percent that has been loaded.

Event is for the basic stuff. ENTER_FRAME is probably the most used type as it's how to create loops: how to make your player constantly move, etc. If your game's frameRate is 30fps, ENTER_FRAME will call the function 30 times per second. The other type, of course, is ADDED_TO_STAGE (and REMOVED_FROM_STAGE).

addEventListener(Event.ENTER_FRAME, myLoop);

function myLoop(e:Event):void {
   if(sprite.x < 600) sprite.x += 10;  // moves the sprite 30 times per second
   //notice the conditional inside the loop.  you can put those, for loops, and everything else inside as well

If your game lags, it's because of this loop. I promise. A major point in coding is to minimize the stuff that goes in here. You shouldn't torture Flash by running your laggy, unoptimized code 30 times per second. But for the most part, you should be fine as long as you use common sense when you code.

And finally, if you want to get rid of a listener:

stage.removeEventListener(MouseEvent.CLICK, doStuff)

The syntax is the exact same as addEventListener. This tells the listener that it's overstayed its welcome and that it's time to go. Clicking will no longer call doStuff. But then you can add the listener back later.

You can't do anything in a game without events. Understanding them will help you make your stuff awesome.

Thanks for reading :)
If you got this far, you're awesome!

Response to As3: Event Basics 2012-03-29 05:35:16


I don't think this is a very good tutorial.

First and foremost, EventDispatcher (IEventDispatcher) is never even mentioned.

The examples contain unrelated information, like the special behavior of "stage" in the document class, which makes them more confusing and less straight to the point.

A reader is told that a function passed to addEventListener() is not "normal", but in fact: it is.
All previous knowledge of functions applies, there's just a parameter, nothing special.
Do not put the beginner in an uneasy mood.

Mentioning .target and .currentTarget without explaining bubbling is pointless.

removeEventListener is mentioned as a side node, no comment on why this is very important regarding the garbage collection, etc.

...

Response to As3: Event Basics 2012-03-29 07:02:11


At 3/29/12 05:35 AM, milchreis wrote: I don't think this is a very good tutorial.

You seem to have missed the word 'basics' in the topic title ;).
As for the tutorial: its good. This will be a great stepping stone into more advanced topics that use events.

Response to As3: Event Basics 2012-03-29 09:09:04


I've tutored about 300 hours at my school, and I've learned that a beginner really doesn't care why stuff happens, so long as it's consistent. Directional input is more important than knowing why stage gets the listener, for a newbie anyway. Custom events, bubbling, weak references, gc don't concern someone who is learning new things from this. Maybe in Intermediate Event Stuff.

Response to As3: Event Basics 2012-03-29 09:14:16


I agree with Milchreis on some of his points. The part where you talk about the event function not being 'normal' is very confusing. I understand what you are trying to say there, but you say it very badly and it will confuse the beginners (who this tutorial is for).

And on a personal note I don't really care much for the 'buddy tone', example: "Don't worry flash got you covered..." etc.. I like the more formal approach on how to write the tutorial. But this is a personal preference and doesn't necessarily make the tutorial worse.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Response to As3: Event Basics 2012-03-29 10:19:10


At 3/29/12 09:14 AM, ProfessorFlash wrote: I agree with Milchreis on some of his points. The part where you talk about the event function not being 'normal' is very confusing. I understand what you are trying to say there, but you say it very badly and it will confuse the beginners (who this tutorial is for).

I'll keep it in mind then

Response to As3: Event Basics 2012-03-29 11:47:56


At 3/29/12 07:02 AM, Sandremss128 wrote:
At 3/29/12 05:35 AM, milchreis wrote: I don't think this is a very good tutorial.
You seem to have missed the word 'basics' in the topic title ;).

No, I didn't.

Response to As3: Event Basics 2012-03-30 01:19:13


At 3/29/12 05:35 AM, milchreis wrote: I don't think this is a very good tutorial.

There's also a lack of telling us how to "easily" replace the "root" listener and whatnot that we all loved in AS2, as it was removed, completely in AS3. From what I've seen from the migration documents, the root replacement is over 3 lines long, which in my opinion, is unacceptable.

Response to As3: Event Basics 2012-03-30 02:30:34


At 3/30/12 01:19 AM, iyeru42 wrote:
At 3/29/12 05:35 AM, milchreis wrote: I don't think this is a very good tutorial.
There's also a lack of telling us how to "easily" replace the "root" listener and whatnot that we all loved in AS2,

Using root all the time in your code is horrible OOP.

Response to As3: Event Basics 2012-03-30 06:24:15


At 3/30/12 01:19 AM, iyeru42 wrote:
At 3/29/12 05:35 AM, milchreis wrote: I don't think this is a very good tutorial.
There's also a lack of telling us how to "easily" replace the "root" listener and whatnot that we all loved in AS2, as it was removed, completely in AS3. From what I've seen from the migration documents, the root replacement is over 3 lines long, which in my opinion, is unacceptable.

What you should find unacceptable is your own inability to migrate from bad habits.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Response to As3: Event Basics 2012-03-31 18:05:09


I read and read tutorials and basic 101 of AS3 and like i said, it's so new to me. I guess the fact that i knew stuff in flash, its harder for me to ALLOW myself to adapt to AS3. It's hard going from being able to move a character and a lot of other stuff to Basic 101 courses and learning why this and eventlisteners but MSGHero you said i would eventually like it more and that is why i keep reading about it. So far it's new to me and it will take time but hope that reading about it and studying it a bit i can eventually get the hang of it..

Response to As3: Event Basics 2012-03-31 18:06:48


PS: Please someone explain why using ROOT all the time is horrible? What makes it horrible coding :$.. That is what i like to know ( not being judgemental ) but rather wanting the information what makes root. being horrible in coding why is it horrible to not wanting to accept 3 lines of code for something that took 1 line before :)

Response to As3: Event Basics 2012-03-31 18:24:40


At 3/31/12 06:06 PM, Plebbi wrote: PS: Please someone explain why using ROOT all the time is horrible?

It makes your code depend on root, which is often not necessary.
Dependencies should be avoided.

being horrible in coding why is it horrible to not wanting to accept 3 lines of code for something that took 1 line before :)

Because the principle behind the 3 lines is a general one deeply woven into the language.
See how many classes extend EventDispatcher.

That's why the principle applies everywhere as opposed to As2 which had many different ways to deal with events.

Having less lines is not necessarily better or faster code.

As you should be using a code editor, the lines won't matter, because you either get them via code completion or a snippet, so you wouldn't type them out by hand anyway.

@ MGS: this proves that it actually is important (and requested) to know about EventDispatcher.

Response to As3: Event Basics 2012-03-31 21:06:34


At 3/31/12 06:24 PM, milchreis wrote: @ MGS: this proves that it actually is important (and requested) to know about EventDispatcher.

I'm really busy with class if anyone else wants dibs on it.