Showing posts with label movement. Show all posts
Showing posts with label movement. Show all posts

Friday, March 14, 2008

Further Flash Programming

Since working on the flash game project I have regained mhy interest in programming and am learning further about actionscript and programming in general.

I am currently working on understanding object movements and the maths behind the movement of objects. Using the code from a reply to a message in a forum (http://www.kirupa.com/forum/showthread.php?p=2297490) I have created a simple movement of a block which moves where you click the mouse.

The object does not stop when it reaches where the player clicked, however. Although I will experiment into how to make it stop when it does reach the location, I also think the block continuing to move would make a good little game if it were to bounce on the walls or to loop back round the other side, or give game over if you go off the side (probably the easiest method for now). By adding objects to collect and avoid this could be a nice quick little game.

Link: http://www.fileden.com/files/2007/11/6/1567526/2.swf

Wednesday, March 05, 2008

Actionscript 3.0 Smooth character movement

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.