Best viewed in Firefox

Awesty Productions

A look into Action Script 3

May 30th, 2007 by awesty

I haven’t done any tutorials for AS (Action script) 3 yet, but I thought it was about time I did. This is a very simple tutorial that will determine which key is being pressed. Obviously you must have Flash CS3.

First of all open a new flash document.

Select an AS 3 Flash Document

Open the actions panel (F9 or Window>Actions) and enter the following code.

function capture(event:KeyboardEvent) {
    trace("You pressed the \""+String.fromCharCode(event.charCode)+"\" key.");
};
stage.addEventListener(KeyboardEvent.KEY_DOWN,capture);

That shouldn’t look to scary if you know a bit of AS 2. Like I said at the start, this is pretty basic. The first line is declaring an Event Listener. It is basically like any listener out of AS 2. It does something, but only when a certain event happens. onEnterFrame happen every frame, but a listener on a Key Press would only happen when a key is pressed. The event listener in this tutorial is a KeyboardEvent (as you can see in the brackets). So it will only happen when something involving the keyboard happens.
The second line is just tracing which key is being pressed. The function String.fromCharCode() gets the name of a key when you give it a character code. As you can see we have event.charCode in the brackets, so it gets the char code of the key that has just been pressed and turns it into a string.
The last line is adding the event listener to the stage using the addEventListener() function. The first parameter is the type of listener. Ours is a KeyboardEvent, and it happens when a key is press (KEY_DOWN). There are many others, for example KEY_UP. If you tried using KEY_UP it would trace the name of the key that has been released.
The next parameter is the name of the function. Ours was ‘capture’, so obviously I put ‘capture’ there.

That is it for this tutorial. I will hopefully get around to doing some more Action script 3 tutorials (when I learn more of it :P ).

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

17 Comments »

Comment by KIM Subscribed to comments via email
2007-05-30 20:13:15

hey awesty…

i dont realy know where to put this comment but im trying to make a snake game but i cant get it together…

so far, i’ve only gone this far:

onClipEvent (load) {
var gright = false;
var gleft = false;
var gup = false;
var gdown = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gright = true;
}
if (Key.isDown(Key.LEFT)) {
gleft = true;
}
if (Key.isDown(Key.UP)) {
gup = true;
}
if (Key.isDown(Key.DOWN)) {
gdown = true;
}
if (gright == true) {
this._x += 20;
}
if (gleft == true) {
this._x -= 20;
}
if (gdown == true) {
this._y += 20;
}
if (gup == true) {
this._y -= 20;
}
if (this._x = 550){
this._x = 0
}
if (this._y = 550){
this._y = 0
}
}

any suggestions?

thnx

Comment by awesty
2007-05-30 22:03:46

What else are you trying to do?

 
 
Comment by KIM Subscribed to comments via email
2007-05-31 00:07:25

when i put that code on… i can only press an arrow once in a game… can u help me fix that pls?

 
Comment by KIM Subscribed to comments via email
2007-05-31 00:08:30

pls help me… you might be my only hope… because the other tutorials are not the ones im lookin for exactly

 
Comment by gankro
2007-05-31 02:33:09

change your code to this:
onClipEvent (load) {
var gright = false;
var gleft = false;
var gup = false;
var gdown = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gright = true;
}else if (Key.isDown(Key.LEFT)) {
gleft = true;
}else if (Key.isDown(Key.UP)) {
gup = true;
}else if (Key.isDown(Key.DOWN)) {
gdown = true;
}
if (gright == true) {
this._x += 20;
}else if (gleft == true) {
this._x -= 20;
}else if (gdown == true) {
this._y += 20;
}else if (gup == true) {
this._y -= 20;
}
if (this._x = 550){
this._x = 0
}
if (this._y = 550){
this._y = 0
}
}

 
Comment by gankro
2007-05-31 02:36:00

wait wait wait… I forgot to say that in the keypresses before you declare something else true declare all the others false… i.e:
if (Key.isDown(Key.RIGHT)) {
gdown = false;
gleft = false;
gup = false;
gright = true;
}

 
Comment by gankro
2007-05-31 02:49:23

okay i know im sorta spamin here but im dedicated to helping this guy now… that bottom part is obviously for making it go back to the other side so heres what you put:
if (this._x >= 551){
this._x = 0
}
if (this._y >= 551){
this._y = 0
}
if (this._x

 
Comment by Trunk Monkey
2007-05-31 04:47:48

I have only got Flash MX 2004 but how can I get Flash CS3?

Comment by awesty
2007-05-31 16:19:52

You can buy it… Email me.

sniper_rifle_048[AT]hotmail[DOT]com

 
 
Comment by KIM Subscribed to comments via email
2007-05-31 12:23:30

OMG… thnx…. although i never thought of doing that yet

thnx alot!

Comment by gankro
2007-05-31 12:26:43

sorry bout that last one getting cut off…
if (this._x >= 551){
this._x = 0
}
if (this._y >= 551){
this._y = 0
}
if (this._y

Comment by KIM Subscribed to comments via email
2007-05-31 16:23:08

now all i need to learn is duplicating…

 
 
 
Comment by gankro
2007-06-01 02:30:41

keeps getting cut off… here’s what I’ve got with a 400×400 playing field
(if this gets cut off just e-mail me at nintendogood@hotmail.com) :
onClipEvent (load) {
var gright = false;
var gleft = false;
var gup = false;
var gdown = false;
}
onClipEvent (enterFrame) {
if ((Key.isDown(Key.RIGHT)) && (gleft == false)) {
gdown = false;
gleft = false;
gup = false;
gright = true;

}else if ((Key.isDown(Key.LEFT)) && (gright == false)){
gdown = false;
gleft = true;
gup = false;
gright = false;

}else if ((Key.isDown(Key.UP)) && (gdown == false)){
gdown = false;
gleft = false;
gup = true;
gright = false;

}else if ((Key.isDown(Key.DOWN)) && (gup == false)){
gdown = true;
gleft = false;
gup = false;
gright = false;

}
if (gright == true) {
this._x += 20;
}else if (gleft == true) {
this._x -= 20;
}else if (gdown == true) {
this._y += 20;
}else if (gup == true) {
this._y -= 20;
}
if (this._x >= 400){
this._x = 0
}
if (this._y >= 400){
this._y = 0
}
if (this._y

Comment by awesty
2007-07-03 18:01:42

You cant use < signs.

 
 
Comment by KIM Subscribed to comments via email
2007-06-07 22:24:51

how can i name a scene??

 
Comment by KIM Subscribed to comments via email
2007-06-11 21:04:08

sorry this one is better than the other game i showed you heres the link

http://www.swfup.com/file/12483

the orange box is the coins
its the same map

Comment by awesty
2007-06-15 14:25:46

That is really cool.

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.