PDA

View Full Version : side scrollers?? how do you do them



badmonur
January 25th, 2007, 10:41 PM
anyone have a good resource place or a demo tutorial of a side scroller shooting game made in flash MX. Any code snippets or stuff like that would be very useful. thx in advance

SacrificialLamb
January 26th, 2007, 12:11 AM
there is a file in this post that might help you
http://www.kirupa.com/forum/showpost.php?p=2046441&postcount=5

badmonur
January 26th, 2007, 01:33 AM
it says unexpected format..
maybe if it was a flash MX 2004 file? i forgot to mention that .

SacrificialLamb
January 26th, 2007, 05:03 AM
ok the code is on the frame

// Game Development with ActionScript
// By Lewis Moronta (c) 2003

// Star Field Functions ////////////////////////////////////////////
function initializeStarField() {

// Let's create MAXSTARS movie clips
for (var i = 0; i < MAXSTARS; i++) {

// Attach the movie from the linked symbol
attachMovie("StarLayer", "star"+i, i);

// Store the clip for easy reference
var star = _root["star"+i];

// Plot it at a random place
star._x = Math.random()*640;
star._y = Math.random()*480;

// Adjust the alpha channel for
// a depth effect...
star._alpha = Math.random()*100;

// Adjust the scale for a greater
// depth effect...
star._xscale = Math.random()*80;
star._yscale = _root["star"+i]._xscale;

// Make sure stars move from 2 to 22 pixels per frame
star.velocity = -(Math.round(Math.random()*20)+2);
}
}

function moveStarField() {

// Let's move ALL the star clips
for (var i = 0; i < MAXSTARS; i++) {

// Store for easy reference
var star = _root["star"+i];

// Movie it along with it's
// velocity own property.
star._x += star.velocity;

// Wrap it around if
// it's off the screen.
if (star._x < -star._width)
star._x = star._width+640;
}
}

// Ship and Player Functions ///////////////////////////////////////
function initializeShip() {

// Let there be Player!
attachMovie("Ship", "Player",8000);

// Position him...
Player._x = 100;
Player._y = 240;

// Y-Axis Velocity
Player.yv = 0;

// Y-Axis Acceleration
Player.ya = 4;

// Setup Score
Player.score = 0;

// Setup the Power Bar
initPowerBar();
}

function moveShip() {

// Ajust the velocities
if (Key.isDown(Key.UP)) {
Player.yv += -Player.ya;
}

if (Key.isDown(Key.DOWN)) {
Player.yv += Player.ya;
}

var MAXVEL = 20;

// Cap Velocity
if (Player.yv > MAXVEL)
Player.yv = MAXVEL;

if (Player.yv < -MAXVEL)
Player.yv = -MAXVEL;

// Move the player
Player._y += Player.yv;

// Add Friction
if (Player.yv > 0)
Player.yv--;
if (Player.yv < 0)
Player.yv++;

// Set boundaries
if (Player._y < 0)
Player._y = 0;

if (Player._y > (480-Player._height))
Player._y = (480-Player._height);


/////////////////////////////////////////////////
//// Check If Player Fired //////////////////////
/////////////////////////////////////////////////
if (Key.isDown(Key.SPACE)) {

for (var i = 0; i < MAXMISSILES; i++) {
// Store for easy reference
var missile = _root["missile"+i];

// Find an inative one and activate it.
if (!missile.fired) {
// Set flags
missile.fired = true;
missile._visible = true;

// Make it follow ship with
// some compensation values
missile._x = Player._x-42;
missile._y = Player._y+18;
break;
}
}
}
}

// Missle Functions ////////////////////////////////////////////////
function initializeMissiles() {
for (var i = 0; i < MAXMISSILES; i++) {

// Attach the movie from the linked symbol
attachMovie("Ammo", "missile"+i, 7000+i);

// Store the clip for easy reference
var missile = _root["missile"+i];

// Make sure they are inactive
missile._visible = false;

// Boolean value that
// decides to launch ammo
missile.fired = false;
}
}

function moveMissiles() {
for (var i = 0; i < MAXMISSILES; i++) {

// Store for easy reference
var missile = _root["missile"+i];

if (missile.fired) {

// Move it if it was fired
missile._x += missile._width;

// Kill it if it went off the screen
if (missile._x > 640) {
missile.fired = false;
missile._visible = false;
}

for (var j = 0; j < MAXENEMIES; j++) {

var badGuy = _root["Enemy"+j];

if (missile.hitTest(badGuy)) {

Player.score += 20;

badGuy.alive = false;
badGuy._visible = false;

if (!BOSSMODE) {

badGuy._x = badGuy._width + 640;
badGuy._y = Math.round(Math.random()*480);

} else {

// Draw it outside of the screen...
// this avoids any random explosions
// caused by the hitTest function
// testing to true...
badGuy._x = -badGuy._width;
badGuy._y = 0;
}

missile.fired = false;
missile._visible = false;

// Start and explosion
for (var k = 0; k < MAXEXPLOSIONS; k++) {

// Store the clip for easy reference
var Explosion = _root["Explosion"+k];

if (!Explosion.active) {

Explosion._x = missile._x+(missile._width/2);
Explosion._y = missile._y;
Explosion.active = true;
Explosion.gotoAndPlay(2);
break;
}
} // End for k

} // End if missile.hitTest
} // End for j

if (missile.hitTest(bigBoss)) {

Player.score += 40;
bossPower.Bar._xscale -= 0.5;

// Game Over!
if (bossPower.Bar._xscale < 0) {
bossPower.Bar._xscale = 0;

unLoadGame();
gotoAndPlay("Congrats", 1);
}

// Start and explosion
for (var j = 0; j < MAXEXPLOSIONS; j++) {

// Store the clip for easy reference
var Explosion = _root["Explosion"+j];

if (!Explosion.active) {

Explosion._x = missile._x+(missile._width/2);
Explosion._y = missile._y;
Explosion.active = true;
Explosion.gotoAndPlay(2);
break;
}
} // End for j
}

} // End if missile.fired
} // End for i
} // End moveMissiles

// Power Bar Functions /////////////////////////////////////////////
function initPowerBar() {

// Make sure the bar is over the ship's layer
attachMovie("PowerBar", "Power", 12000);

// Place the bar
Power._x = 460;
Power._y = 20;

// Adjust the bar's power level
Power.Bar._xscale = 100;
}

function initBossPowerBar() {

// Make sure the bar is over the Boss's layer
attachMovie("bossPowerBar", "bossPower", 12001);

// Place the bar
bossPower._x = 460;
bossPower._y = 450;

// Adjust the bar's power level
bossPower.Bar._xscale = 100;
}

// Enemy Functions /////////////////////////////////////////////////
function initializeEnemies() {
for (var i = 0; i < MAXENEMIES; i++) {

attachMovie("sineEnemy", "Enemy"+i, 9000+i);

var badGuy = _root["Enemy"+i];

badGuy._x = badGuy._width + 640;
badGuy._y = Math.round(Math.random()*440+40);

badGuy.xv = -Math.round(Math.random()*10+10);
badGuy.yv = 0;

badGuy.alive = false;
badGuy._visible = false;

badGuy.degrees = Math.round(Math.random()*360);
badGuy.radians = Math.PI/180*badGuy.degrees;

badGuy._alpha = Math.round(Math.random()*60+40);
}
}

function moveEnemies() {

// If the Boss steps on stage,
// make sure no new enemies are spawned.
if (!BOSSMODE) {
for (var i = 0; i < MAXENEMIES; i++) {

var badGuy = _root["Enemy"+i];

// Spit out another enemy only if random()
// returns 1 (or true) and the badGuy is not alive...
if (!badGuy.alive && (Math.round(Math.random()*30) == 3)) {
badGuy.alive = true;
badGuy._visible = true;
break;
}

}
}

for (var i = 0; i < MAXENEMIES; i++) {

var badGuy = _root["Enemy"+i];

// If alive, move the enemy along...
if (badGuy.alive) {

badGuy._x += badGuy.xv;

if (badGuy.degrees++ > 360)
badGuy.degrees = 0;

badGuy.radians = Math.PI/180 * badGuy.degrees;
badGuy.yv = Math.round(Math.sin(badGuy.radians)*2);
badGuy._y += badGuy.yv;


// If an enemy hits the Player,
// dock the Player 2 percent...
if (badGuy.hitTest(Player)) {
Power.Bar._xscale -= 2;

// Game Over!
if (Power.Bar._xscale < 0) {
Power.Bar._xscale = 0;

unLoadGame();
gotoAndPlay("GameOver", 1);
}

// Start and explosion
for (var j = 0; j < MAXEXPLOSIONS; j++) {

// Store the clip for easy reference
var Explosion = _root["Explosion"+j];

if (!Explosion.active) {
Explosion._x = Player._x;
Explosion._y = Player._y+12;
Explosion.active = true;
Explosion.gotoAndPlay(2);
break;
}
}
}

// If off the left side, reset.
if (badGuy._x < -badGuy._width) {
badGuy.alive = false;
badGuy._visible = false;
badGuy._x = badGuy._width + 640;
badGuy._y = Math.round(Math.random()*440+40);
}
}
}
}

// Explosion Functions /////////////////////////////////////////////
function initializeExplosions() {

for (var i = 0; i < MAXEXPLOSIONS; i++) {

// Attach the movie from the linked symbol
attachMovie("Explosion", "Explosion"+i, 11000+i);

// Store the clip for easy reference
var Explosion = _root["Explosion"+i];

Explosion.active = false;
}
}

// Boss Functions ////////////////////////////////////////////////
function initializeBoss() {

attachMovie("Boss", "bigBoss", 10000);

bigBoss._x = 640+bigBoss._width;
bigBoss._y = 240-(bigBoss._height/2);

bigBoss.yv = 10;

initBossPowerBar();
initializeBombs();
}

function moveBoss() {

if (BOSSMODE) {
bigBoss._x += -5;

if (bigBoss._x < 430)
bigBoss._x = 430;

bigBoss._y += bigBoss.yv;

if (bigBoss._y < 0)
bigBoss.yv = -bigBoss.yv;
if (bigBoss._y > (480-bigBoss._height))
bigBoss.yv = -bigBoss.yv;

moveBombs();
}
}

// Bomb Functions //////////////////////////////////////////////////
function initializeBombs() {
for (var i = 0; i < MAXBOMBS; i++) {
attachMovie("Bomb", "Bomb"+i, 9500+i);

var bossBomb = _root["Bomb"+i];

bossBomb._x = bigBoss._x+2;
bossBomb._y = bigBoss._y+96;
bossBomb.fired = false;

bossBomb.force = 2;
}
}

function moveBombs() {
for (var i = 0; i < MAXBOMBS; i++) {

var bossBomb = _root["Bomb"+i];

// Decide when to fire...
if (Math.round(Math.random()*30) == 3) {
if (!bossBomb.fired) {
bossBomb.fired = true;

bossBomb._x = bigBoss._x+2;
bossBomb._y = bigBoss._y+96;

bossBomb.xv = -Math.round(Math.random()*10+10);
bossBomb.yv = -20;
}
}

if (bossBomb.fired) {
bossBomb._x += bossBomb.xv;
bossBomb._y += bossBomb.yv;

bossBomb.yv += bossBomb.force;
}

if (bossBomb._x < -bossBomb._width) {
bossBomb.fired = false;
}

if (bossBomb._y > 480) {
bossBomb.fired = false;
}

if (bossBomb.hitTest(Player)) {
Power.Bar._xscale -= 2;

// Game Over!
if (Power.Bar._xscale < 0) {
Power.Bar._xscale = 0;

unLoadGame();
gotoAndPlay("GameOver", 1);
}

// Start and explosion
for (var j = 0; j < MAXEXPLOSIONS; j++) {

// Store the clip for easy reference
var Explosion = _root["Explosion"+j];

if (!Explosion.active) {
Explosion._x = Player._x;
Explosion._y = Player._y+12;
Explosion.active = true;
Explosion.gotoAndPlay(2);
break;
}
}
}
}
}

// Unloading Functions /////////////////////////////////////////////
function unLoadGame() {

for (var i = 0; i < MAXEXPLOSIONS; i++) {
var Explosion = _root["Explosion"+i];
Explosion.removeMovieClip();
}

for (var i = 0; i < MAXSTARS; i++) {
var star = _root["star"+i];
star.removeMovieClip();
}

for (var i = 0; i < MAXMISSILES; i++) {
var missile = _root["missile"+i];
missile.removeMovieClip();
}

for (var i = 0; i < MAXBOMBS; i++) {
var Bomb = _root["Bomb"+i];
Bomb.removeMovieClip();
}

for (var i = 0; i < MAXENEMIES; i++) {
var Enemy = _root["Enemy"+i];
Enemy.removeMovieClip();
}

bigBoss.removeMovieClip();
Player.removeMovieClip();
Power.removeMovieClip();
bossPower.removeMovieClip();
}
and on a black mc

// Game Development with ActionScript
// By Lewis Moronta (c) 2003

// Initialize the game
onClipEvent(load) {

// Change this number to change
// the amount of stars in the field
_root.MAXSTARS = 100;

// Change this value to adjust
// the number of missles and bombs
// that can be fired at once.
_root.MAXMISSILES = 6;
_root.MAXBOMBS = 10;

// This variable will decide
// if enemies or boss should attack
_root.BOSSMODE = false;

// This decides how many enemies
// can exist at one time...
_root.MAXENEMIES = 20;

// Setup how many explosions
// can explode at one time...
_root.MAXEXPLOSIONS = 13;

// Hide the cursor
// Mouse.hide();

// This plots stars all over the place
_root.initializeStarField();

// Create the ship
_root.initializeShip();

// Setup the enemies
_root.initializeEnemies();

// Setup the missles
_root.initializeMissiles();

// Setup the explosions
_root.initializeExplosions();

// Setup the Boss
_root.initializeBOSS();

// Now that everything is setup
// let's keep track of the time...
_root.startTime = getTimer();
}

// Let there be movement!
onClipEvent(enterFrame) {

// Let's make sure the stars
// move along and wrap around.
_root.moveStarField();

// Allow the player to move
// the space ship
_root.moveShip();

// Attack...
_root.moveEnemies();

// Fire!!!
_root.moveMissiles();

// Move the big daddy
_root.moveBoss();

if ((getTimer()-_root.startTime) > 30000) {
_root.BOSSMODE = true;
}

_root.scoreDisplay = _root.Player.score;
}