Jane, Stop this crazy thing?

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 += bulletSpeed
this.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.

You can put one in it and work around like that… But now I know you just copied this entire code set. Which is kind-a depressing to me… And I wonder if I should even give you the code or allow you the pleasures of figuring it out yourself…

Now… TO give you a hint… To make it slow down you would just do the opposite of thrusting… Easy and simple.

Hmmm. Interesting, Marz.

I posted this thread back on 12/01 and it took you a week to reply to it. Did you spend a whole week writing and rehearsing your sarcastic remarks towards me? Did mommy help you with it?

I don’t know what the problem is. I credited in my opening statement the code was from a magazine. And, yes it dawned on me to write the code in reverse:

[AS]
//thrust backwards
if(key.isDown(key.DOWN)) {
shipAngle = 2.0Math.PI(ship._rotation-90)/360.0;
ship.dx -= Math.cos(shipAngle);
ship.dy -= Math.sin(shipAngle);

    }[/AS]

I even tried messing with the line shipAngle = 2.0 …
Instead of *, I used /.

It didn’t work.

Look, I don’t have a problem with you or anyone. There is nothing wrong with using a tutorial, studying the code, and experimenting to change it around. I added sound for when the ship shoots and collides with a rock.That wasn’t in the magazine. I got that knowledge from kirupa.

If you want to see an original actionscripting game code, find my post with the Freeway game. Sure the game already exist, but after playing on PS2 I looked at it and said, “I could build this.”

Marz, not everyone is a Ripper or lazy to learn.

If you are still depressed, I’ll send you some Prozac.

Take it easy.