Hey. I have a subscription the Computer Arts Magazine. A few months ago they dedicated an entire magazine to Flash Games. I followed a tutorial to build an Asteroids game. It works fine; except there is no code to slow the ship down. Attached is the file and here is the code on the main movie timeline in frame 2:
[AS]
initRocks();
initShip();
// listen for key presses
spacebarListener = new Object();
spacebarListener.onKeyDown = fireBullet;
Key.addListener(spacebarListener);
// start time for game
gameStartTime = getTimer();
// init score
score = 0;
function initRocks() {
rocksArray = new Array ();
// start rocks at number 0
rockNum = 0;
// create four large rocks, 100%, at random locations
for (i=0; i<4; i++){
rockMC = newRock(100);
rockMC._x = 550*Math.random();
rockMC._y = 400*Math.random();
}
}
function newRock(size) {
// random rock type
rockType = Math.floor(3*Math.random()+1);
// create a new rock
rockMC = attachMovie("Rock"+rockType, "rock"+rockNum, rockNum++);
// scale it to size
rockMC._xscale = size;
rockMC._yscale = size;
// random speed and direction
rockMC.dx = 4*(Math.random()-.5);
rockMC.dy = 4*(Math.random()-.5);
// move every 50 ms
setInterval(moveRocks,50,rockMC);
// store in array
rocksArray.push(rockMC);
return (rockMC);
}
function moveRocks(rockMC){
// move rock
rockMC._x += rockMC.dx;
rockMC._y += rockMC.dy;
//wrap around screen
if (rockMC._x > 560) rockMC._x -= 570;
if (rockMC._x < -10) rockMC._x += 570;
if (rockMC._y > 410) rockMC._y -= 420;
if (rockMC._y < -10) rockMC._y += 420;
}
function initShip() {
// start still
ship.dx = 0;
ship.dy = 0;
// move every 50ms
setinterval(moveShip, 50);
}
function moveShip() {
// rotate ship left and right
if(Key.isDown(key.LEFT)) {
ship._rotation -= 5;
}
if (Key.isDown(Key.RIGHT)) {
ship._rotation += 5;
}
//thrust forward
if(key.isDown(key.UP)) {
shipAngle = 2.0*Math.PI*(ship._rotation-90)/360.0;
ship.dx += Math.cos(shipAngle);
ship.dy += Math.sin(shipAngle);
//show thrust animation
if (ship._currentframe == 1){
ship.gotoAndPlay(2);
}
}
// move ship
ship._x += ship.dx;
ship._y += ship.dy;
// wrap around screen
if (ship._x > 550) ship._x -= 550;
if (ship._x < 0) ship._x += 550;
if (ship._y > 400) ship._y -= 400;
if (ship._y < 0) ship._y += 400;
// see if ship hit rock
if (getTimer() - gameStartTime > 3000) {
for(var i in rocksArray){
if (rocksArray*.hitTest(ship._x,ship._y)){
gotoAndStop("lose")
}
}
}
}
function fireBullet(){
if (Key.getAscii() == 32) {
//create new bullet at level 1000+bulletNum
bulletMC = attachMovie("Bullet", "bullet"+bulletNum,1000+bulletNum++);
//start at center of ship
bulletMC._x = ship._x;
bulletMC._y = ship._y;
//direction of the ship
shipAngle = 2*Math.PI*(ship._rotation-90)/360;
bulletMC.dx = Math.cos(shipAngle);
bulletMC.dy = Math.sin(shipAngle);
//attach movement function
bulletMC.moveBullet = moveBulletFunction;
setInterval(bulletMC, "moveBullet", 50, bulletMC)
}
}
function moveBulletFunction(){
//move bullet
bulletSpeed = 10.0;
this._x += bulletSpeedthis.dx;
this._y += bulletSpeedthis.dy;
//see if reached end of screen
if (this._x > 550) this.removeMovieClip();
if (this._x < 0) this.removeMovieClip();
if (this._y > 400) this.removeMovieClip();
if (this._y < 0) this.removeMovieClip();
// loop through rocks to see if bullet hit any
for (var i = _root.rocksArray.length-1; i>=0; i--) {
if (_root.rocksArray*.hitTest(this._x, this._y)){
//remove bullet
this.removeMovieClip();
//deal with rock destruction
_root.hitRock(_root.rocksArray*);
//remove rock from array
_root.rocksArray.splice(i, 1);
// see if all rocks are gone
if (_root.rocksArray.length == 0) {
gotoAndStop("win");
}
}
}
}
function hitRock(rockMC){
if (rockMC._xscale > 25) {
// if rock is large enough, split in two
for (var i=0; i<2; i++){
// starting at old location
newRockMC = newRock(rockMC._xscale/2);
newRockMC._x = rockMC._x;
newRockMC._y = rockMC._y;
}
}
// add to score
score += 10;
// remove old rock
rockMC.removeMovieClip();
}[/AS]
Any suggestions on how to give the ship brakes?
Thank you. Also I am still working on the other game.