A look into Action Script 3
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.
Open the actions panel (F9 or Window>Actions) and enter the following code.
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
).















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
What else are you trying to do?
when i put that code on… i can only press an arrow once in a game… can u help me fix that pls?
pls help me… you might be my only hope… because the other tutorials are not the ones im lookin for exactly
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
}
}
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;
}
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
I have only got Flash MX 2004 but how can I get Flash CS3?
You can buy it… Email me.
sniper_rifle_048[AT]hotmail[DOT]com
OMG… thnx…. although i never thought of doing that yet
thnx alot!
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
now all i need to learn is duplicating…
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
You cant use < signs.
how can i name a scene??
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
That is really cool.