-
Arrow Keys Code by Jovan Game Studios
Hello.
Today I will show and explain the arrow keys code. This code can help you if you want to make game with moving object with the arrow keys.
First you go to Insert>Symbol. Now you draw your object. Then you go to Scence 1.
On the Action Script box of your object you write this code:
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
this._y -= 5;
}
if (Key.isDown(Key.DOWN)) {
this._y += 5;
}
if (Key.isDown(Key.RIGHT)) {
this._x += 5;
}
if (Key.isDown(Key.LEFT)) {
this._x -= 5;
}
}
So let's explain this code.
With the "if" function you make condition when some arrow key will be pressed. When you press arrow key the object is moving with adding or subtracting the scaleY (up and down) or scaleX (left and right). That the code.
Thank you!
Jovan Game Studios
-
FK'n_dog
-
5+5=55
Originally Posted by The FlashMaster
When you press arrow key the object is moving with adding or subtracting the scaleY (up and down) or scaleX (left and right).
You're adding or subtracting 5 to the x and y coordiantes (_x and _y properties). scaleX and scaleY have nothing to do with it (just a typo perhaps?)
-
Dignitary
Originally Posted by a_modified_dog
hmmm.....soooo Flash 5
lol
Stop teaching newbies AS1 Flashmaster.
-
Dignitary
Here it is in AS3
PHP Code:
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveIt)
function moveIt(evt:KeyboardEvent):void{
if (evt.keyCode == 37){
myMC.x -=5;
}
if (evt.keyCode == 39){
myMC.x +=5;
}
if (evt.keyCode == 38){
myMC.y -=5;
}
if (evt.keyCode == 40){
myMC.y +=5;
}
}
The differences here are
1. This function only fires when a key is down, saving processing speed.
2. Any key on the keyboard can be added in this function.
Last edited by rynoe; 08-05-2009 at 11:09 AM.
[SIGPIC][/SIGPIC]
-
Senior Member
Originally Posted by rynoe
lol
Stop teaching newbies AS1 Flashmaster.
Definitely agree with rynoe, Flashmaster's energy should be directed at teaching newbies how to use Google, or for that matter the seach feature of this forum.
Wile E. Coyote - "Clear as mud?"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|