Monday, March 31, 2008

Using Cos & Sin to Drive a Car


Moving Car
Play demo

This is a basic driving simulation. A simple driving demonstration allow the player to steer and move.


Top-Down Driving Game
Click here to play

This is sample of a game that use Math.cos and Math.sin values of the rotation to determine forward movement, using left and right arrow keys to directly change the rotation property of the car.

Here is the start of the Moving Car class, with the listeners and the code to handle the arrow keys. Notice that we don't need any extra imports to use the Math functions. These are part of the standard ActionScript Library .


package {
import flash.display.*;
import flash.events.*;

public class MovingCar extends MovieClip {
private var leftArrow, rightArrow, upArrow: Boolean;

public function MovingCar() {

// move every frame
addEventListener(Event.ENTER_FRAME, moveCar);

// respond to key events
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyPressedUp);
}

// set arrow variables to true
public function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = true;
} else if (event.keyCode == 39) {
rightArrow = true;
} else if (event.keyCode == 38) {
upArrow = true;
}
}

// set arrow variables to false
public function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = false;
} else if (event.keyCode == 39) {
rightArrow = false;
} else if (event.keyCode == 38) {
upArrow = false;
}
}

// turn or move car forward
public function moveCar(event:Event) {
if (leftArrow) {
car.rotation -= 5;
}
if (rightArrow) {
car.rotation += 5;
}
if (upArrow) {
moveForward();
}
}

// calculate x and y speed and move car
public function moveForward() {
var speed:Number = 5.0;
var angle:Number = 2*Math.PI*(car.rotation/360);
var dx:Number = speed*Math.cos(angle);
var dy:Number = speed*Math.sin(angle);
car.x += dx;
car.y += dy;
}
}
}

This source code is courtesy of ActionScript 3.0 Game Programming. I also use 2 reference books such as Learning ActionScript 3.0 and ActionScript 3.0 Bible.

Friday, March 28, 2008

Shooting Game

Click image to go to the link.
Read the short instruction before you start.



This is the game I just finished recently using ActionScript 2.0

Roles:

- Design the Inteface: Space ambiance, placement of title, score, lives, respawn position of the ship
- Put codes in the movie clip: player ship, enemy, bullet & main stage
- Embed music a theme from G10 Star Fox Space Armada

If you want copy of my codes, please feel free to contact me at tet@tetfx.com


Tuesday, March 25, 2008

onClipEvent - Spawning

click image to see action
or click here

In computer and video games, spawning is the in-game creation of an entity, such as a player character or enemy. Respawning is the recreation of an entity after its death or destruction.

This is the code for the enemies (small planes coming from right side of screen) to respawn.

Next week I will post again where you the player can now shoot or fire the enemies.

onClipEvent(load)
{
function reset()
{
this._y = Math.random() * 300
this._x = 600
mySpeed = Math.ceil(Math.random()*6)+1
}
reset();
}

onClipEvent(enterFrame)
{
this._x-=mySpeed;
if(timer>=12)
{
var dir = Math.ceil(Math.random()*3)
timer = 0;
}
timer++

if(dir==1)
{
this._y+=3;
}
else if(dir==2)
{
this._y+=3;
}
if(this._x<-10)
{
reset();
}
}


Wednesday, March 19, 2008

Key object: Creating keyboard controls



The Key object is a collection of ActionScript elements that handle keypress detection. Using the methods of the Key object, you can retrieve the last key pressed, or test whether a particular key has been pressed. The Key object is commonly used to create keyboard controls for Flash movies. The properties of the Key object are constants representing the keys most commonly used to control games.


KEY.LEFT
Property; constant associated with the key code value for the Left Arrow key
KEY.RIGHT
Property; constant associated with the key code value for the Right Arrow key
KEY.UP
Property; constant associated with the key code value for the UP Arrow key
KEY.DOWN
Property; constant associated with the key code value for the Down Arrow key

I imported an airplane gif to the stage, convert it to movie clip, give instance name "player" and typed this code in the ActionScript Panel:

var speed = 20

this.onEnterFrame = function () {

//move player to go down
if (Key.isDown(Key.DOWN))
{
player._y +=speed
}

//move the player to go up
else if (Key.isDown(Key.UP))
{
player._y -=speed
}

//move player to the left
else if (Key.isDown(Key.LEFT))
{
player._x -=speed
}

//move player to the right
else if (Key.isDown(Key.RIGHT))
{
player._x +=speed
}
}

player - is the instance name given to the airplane gif
player._x - the "x" refers to the horizontal axis
player._y - the "y" refers to the vertical axis

I used the "else if" statement so the plane will move one direction at a time.