Smooth Character Movement
In actionscript 3.0 when you program an object to move following a keypress, the object moves once then pauses before continue the moment. This is the way I have achieved smooth movement, not certain its the best or easiest way but anyway heres what I did:
Using the example of moving left
Under your 'keydown' function for the left key, type mLeft = true;
Then under your 'enterFrame' function, have an if statement saying:
if (moveLeft) {
myCharacter.x = myCharacter.x - 10;
}
Then, under your 'keyup' function for the left key, type mLeft = false;
You will need to define the following variables for the different keys:
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
(the last two if you are moving the character up and down as well as left and right)
All the code:
//Defining the variables
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var mDown:Boolean = false;
//Adding Event listeners
stage.addEventListener(KeyboardEvent.KEY_UP, reportKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(Event.ENTER_FRAME, EnterFrame);
//KeyDown function
function reportKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
moveLeft = true;
}
if (event.keyCode == Keyboard.RIGHT) {
moveRight = true;
}
if (event.keyCode == Keyboard.UP) {
moveUP = true;
}
if (event.keyCode == Keyboard.DOWN) {
moveDOWN= true;
}
}
//Key Up Function
function reportKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
moveLeft = false;
}
if (event.keyCode == Keyboard.RIGHT) {
moveRight = false;
}
if (event.keyCode == Keyboard.UP) {
moveUP = false;
}
if (event.keyCode == Keyboard.UP) {
moveDOWN = false;
}
}
//Enter Frame Function (this happens over and over repeatedly)
function EnterFrame(event:Event):void {
if (moveLeft){
myCharacter.x -= 10;
}
if (moveRight){
myCharacter.x +=10;
}
if (moveUp){
myCharacter.y -=10;
}
if (moveDown){
myCharacter.y +=10;
}
}
}
I *think* that this should work, if it doesn't I apologise and please comment and tell me what the error says or what is happening.
This blog was originally created when I was a student at Nottingham Trent University. I am now using it occasionally for blog posts on my various multimedia projects and tutorials.
Wednesday, March 05, 2008
Bibliography
Just pasting a link here to remember for my bibliography for my game project.
http://blogs.adobe.com/pdehaan/2006/07/using_the_timer_class_in_actio.html
will have to search back through some other sites I have used for when I write out my bibliography which is something I need to do along with all the other paper work I need to do for the project along with other stuff I need to do this week.
Today's plans:
Get game over/score/lives/timer etc working
Get phidgets working
That should be enough since I can see phidgets being a pain. If still more time I may do some websitey stuff which I really need to get doing.
http://blogs.adobe.com/pdehaan/2006/07/using_the_timer_class_in_actio.html
will have to search back through some other sites I have used for when I write out my bibliography which is something I need to do along with all the other paper work I need to do for the project along with other stuff I need to do this week.
Today's plans:
Get game over/score/lives/timer etc working
Get phidgets working
That should be enough since I can see phidgets being a pain. If still more time I may do some websitey stuff which I really need to get doing.
Labels:
actionscript,
bibliography,
flash,
game design,
programming,
uni project
Subscribe to:
Posts (Atom)