Hi,
I have been following a basic game tutorial and I wanted to enhance it a bit. I have created a movie clip that contains three different backgrounds. I have labelled them bg1 bg2 bg3. I have exported the mc so i can use it with my .as file.
I am a real beginner at flash and do not know how to implement this into my code.
I think i have to declare the background such as
private var backgrounds:Backgrounds; my mc is called Backgrounds
Do i have to addChild now? if so where and then where abouts in my code will the background work so that when the wave/level changes from 1 to 2 the background will also change hence a ‘new level’ effect.
Any help would be muchly appreciated.
Thanks :)package {
import flash.display.;
import flash.events.;
import flash.text.*;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.geom.Point;
import flash.media.SoundChannel;
public class SpaceRocksV2 extends MovieClip {
// class definition goes here
static const shipRotationSpeed:Number = 0.1;
static const rockSpeedStart:Number = .03;
static const rockSpeedIncrease:Number = .02;
static const missileSpeed:Number = .2;
static const thrustPower:Number = .15;
static const shipRadius:Number = 20;
static const startingShips:uint = 3;
static const screenWidth:uint = 550;
static const screenHeight:uint = 400;
//game objects
private var ship:Ship;
private var backgrounds:Backgrounds;
private var rocks:Array;
private var missiles:Array;
//animation timer
private var lastTime:uint;
//arrow keys
private var rightArrow:Boolean = false;
private var leftArrow:Boolean = false;
private var upArrow:Boolean = false;
//ship velocity
private var shipMoveX:Number;
private var shipMoveY:Number;
//timers
private var delayTimer:Timer;
private var shieldTimer:Timer;
//game mode
private var gameMode:String;
private var shieldOn:Boolean;
//ships and shields
private var shipsLeft:uint;
private var shieldsLeft:uint;
private var shipIcons:Array;
private var shieldIcons:Array;
//score and level and name
private var gameScore:Number;
private var scoreDisplay:TextField;
private var gameLevel:uint;
private var levelDisplay:TextField;
//sprites
private var gameObjects:Sprite;
private var scoreObjects:Sprite;
private var levelObjects:Sprite;
//start the game
public function startSpaceRocks() {
//set up sprites
gameObjects = new Sprite();
addChild(gameObjects);
scoreObjects = new Sprite();
addChild(scoreObjects);
levelObjects = new Sprite();
addChild(levelObjects);
// reset score objects
gameLevel = 1;
shipsLeft = startingShips;
gameScore = 0;
createShipIcons();
createScoreDisplay();
createLevelDisplay();
// set up listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
addEventListener(Event.ENTER_FRAME,moveGameObjects);
//start
gameMode = "delay";
shieldOn = false;
missiles = new Array();
nextRockWave(null);
newShip(null);
}
/*************************************************
* GAME CONTROL *
*************************************************/
public function endGame() {
//remove all objects and listeners
removeChild(gameObjects);
removeChild(scoreObjects);
removeChild(levelObjects);
gameObjects = null;
scoreObjects = null;
levelObjects = null;
removeEventListener(Event.ENTER_FRAME,moveGameObjects);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
gotoAndStop("GameOver");
}
function changeBG ( evt ) {
}
/*************************************************
* KEY HANDLING *
*************************************************/
public function keyDownFunction(evt:KeyboardEvent) {
switch (evt.keyCode) {
case 37 :
//left arrow pressed
leftArrow = true;
break;
case 39 :
// right arrow pressed
rightArrow = true;
break;
case 38 :
// up arrow pressed
upArrow = true;
// show trusters
if (gameMode == "play") {
ship.gotoAndStop("thrust");
}
break;
case 32 :
//space bar pressed
newMissile();
break;
case 90 :
// z pressed
startShield(false);
}
}
public function keyUpFunction(evt:KeyboardEvent) {
switch (evt.keyCode) {
case 37 :
leftArrow = false;
break;
case 39 :
rightArrow = false;
break;
case 38 :
upArrow = false;
//remove thrusters
if (gameMode == "play") {
ship.gotoAndStop(1);
}
}
}
/*************************************************
* USER FEEDBACK *
*************************************************/
public function createShipIcons() {
shipIcons = new Array();
for (var i:uint=0; i<shipsLeft; i++) {
var newShip:ShipIcon = new ShipIcon();
newShip.x = 20+i*15;
newShip.y = screenHeight - 25;
scoreObjects.addChild(newShip);
shipIcons.push(newShip);
}
}
public function createShieldIcons() {
shieldIcons = new Array();
for (var i:uint=0; i<shieldsLeft; i++) {
var newShield:ShieldIcon = new ShieldIcon();
newShield.x = (screenWidth - 20)-i*15;
newShield.y = screenHeight - 25;
scoreObjects.addChild(newShield);
shieldIcons.push(newShield);
}
}
//remove ship icon
public function removeShipIcon() {
scoreObjects.removeChild(shipIcons.pop());
}
//remove shield icon
public function removeShieldIcon() {
scoreObjects.removeChild(shieldIcons.pop());
}
//remove the rest of the ship icons
public function removeAllShipIcons() {
while (shipIcons.length > 0) {
removeShipIcon();
}
}
//remove the rest of the shield icons
public function removeAllShieldIcons() {
while (shieldIcons.length >0) {
removeShieldIcon();
}
}
public function createScoreDisplay() {
scoreDisplay = new TextField();
scoreDisplay.x = screenWidth - scoreDisplay.width;
scoreDisplay.y = 10;
scoreDisplay.width = 100;
scoreDisplay.selectable = false;
var scoreDisplayFormat = new TextFormat();
scoreDisplayFormat.color = 0xFFFFFF;
scoreDisplayFormat.font = "Arial";
scoreDisplayFormat.align = "right";
scoreDisplay.defaultTextFormat = scoreDisplayFormat;
scoreObjects.addChild(scoreDisplay);
updateScore();
}
public function updateScore() {
scoreDisplay.text = "Score: " + String(gameScore);
}
public function createLevelDisplay() {
levelDisplay = new TextField();
levelDisplay.width = 100;
levelDisplay.x = 0;
levelDisplay.y = 10;
levelDisplay.selectable = false;
var levelDisplayFormat = new TextFormat();
levelDisplayFormat.color = 0xFFFFFF;
levelDisplayFormat.font = "Arial";
levelDisplayFormat.align = "left";
levelDisplay.defaultTextFormat = levelDisplayFormat;
levelObjects.addChild(levelDisplay);
updateLevel();
}
public function updateLevel() {
levelDisplay.text = "Wave: " + String(gameLevel);
}
/*************************************************
* SHIP FUNCTIONS *
*************************************************/
public function newShip(evt:Event) {
// if ship exists, remove it
if (ship != null) {
gameObjects.removeChild(ship);
ship = null;
}
// no more ships
if (shipsLeft < 1) {
endGame();
return;
}
//create position and add new ship
ship = new Ship();
ship.gotoAndStop(1);
ship.x = screenWidth/2;
ship.y = screenHeight/2;
ship.rotation = -90;
ship.shield.visible = false;
gameObjects.addChild(ship);
// set up ship properties
shipMoveX = 0.0;
shipMoveY = 0.0;
gameMode = "play";
// set up shields
shieldsLeft = 3;
createShieldIcons();
// all lives but the first start with a free shield
if (shipsLeft != startingShips) {
startShield(true);
}
}
public function startShield(freeShield:Boolean) {
if (shieldsLeft < 1) {
return;
}//no shield lefts
if (shieldOn) {
return;
}//shield already on
//turn shield on and set timer to turn off
ship.shield.visible = true;
shieldTimer = new Timer(3000,1);
shieldTimer.addEventListener(TimerEvent.TIMER_COMPLETE,endShield);
shieldTimer.start();
//update shields remaining
if (!freeShield) {
removeShieldIcon();
shieldsLeft--;
}
shieldOn = true;
}
//turn off shield
public function endShield(event:TimerEvent) {
ship.shield.visible = false;
shieldOn = false;
}
public function moveShip(timeDiff:uint) {
//rotate and thrust
if (leftArrow) {
ship.rotation -= shipRotationSpeed*timeDiff;
} else if (rightArrow) {
ship.rotation += shipRotationSpeed*timeDiff;
} else if (upArrow) {
shipMoveX += Math.cos(Math.PI*ship.rotation/180)*thrustPower;
shipMoveY += Math.cos(Math.PI*ship.rotation/180)*thrustPower;
}
//move
ship.x += shipMoveX;
ship.y += shipMoveY;
// wrap around the screen
var buffer:uint = 20;
//heading right and off the right side of the screen
if ((shipMoveX > 0) && (ship.x >screenWidth + buffer)) {
ship.x -= screenWidth + 2 * buffer;
}
// heading left and off the elft side of the screen
if ((shipMoveX < 0) && (ship.x < -buffer)) {
ship.x += screenWidth + 2 * buffer;
}
//heading up and off the top of the screen
if ((shipMoveY > 0) && (ship.y > screenHeight + buffer)) {
ship.y -= screenHeight + 2 * buffer;
}
//heading up and off the top of the screen
if ((shipMoveY < 0) && (ship.y < -buffer)) {
ship.y += screenHeight + 2 * buffer;
}
}
public function shipHit() {
gameMode = "delay";
ship.gotoAndPlay("explode");
removeAllShieldIcons();
delayTimer = new Timer(2000,1);
delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE,newShip);
delayTimer.start();
removeShipIcon();
shipsLeft--;
}
/*************************************************
* ROCK FUNCTIONS *
*************************************************/
public function nextRockWave(evt:Event) {
rocks = new Array();
newRock(100,100,"Big");
newRock(100, screenHeight - 100, "Big");
newRock(screenWidth - 100, 100, "Big");
newRock(screenWidth - 100, screenHeight - 100, "Big");
gameMode = "play";
}
public function newRock(x,y:int, rockType:String) {
// create appropriate new class
var newRock:MovieClip;
var rockRadius:Number;
if (rockType == "Big") {
newRock = new Rock_Big();
rockRadius = 35;
} else if (rockType == "Medium") {
newRock = new Rock_Medium();
rockRadius = 20;
} else if (rockType == "Small") {
newRock = new Rock_Small();
rockRadius = 10;
}
// choose a random look
newRock.gotoAndStop(Math.ceil(Math.random()*3));
//set start position
newRock.x = x;
newRock.y = y;
//set random movement and rotation
var dx:Number = Math.random()*2.0-1.0;
var dy:Number = Math.random()*2.0-1.0;
var dr:Number = Math.random();
//add to stage and to rocks list
gameObjects.addChild(newRock);
rocks.push({rock:newRock, dx:dx, dy:dy, dr:dr, rockType:rockType, rockRadius:rockRadius});
}
public function moveRocks(timeDiff:uint) {
for (var i:int=rocks.length-1; i>=0; i--) {
//move the rocks
var rockSpeed:Number = rockSpeedStart + rockSpeedIncrease*gameLevel;
rocks*.rock.x += rocks*.dx*timeDiff*rockSpeed;
rocks*.rock.y += rocks*.dy*timeDiff*rockSpeed;
//rotate rocks
rocks*.rock.rotation += rocks*.dr*timeDiff*rockSpeed;
var buffer:uint = 20;
// moving right and off the right of the screen
if ((rocks*.dx > 0) &&
(rocks*.rock.x > screenWidth + buffer)) {
rocks*.rock.x -= screenWidth + 2 * buffer;
}
// moving left and off the left of the screen
if ((rocks*.dx < 0) &&
(rocks*.rock.x < -buffer)) {
rocks*.rock.x += screenWidth + 2 * buffer;
}
// moving down and off the bottom of the screen
if ((rocks*.dy > 0) &&
(rocks*.rock.y > screenHeight + buffer)) {
rocks*.rock.y -= screenHeight + 2 * buffer;
}
// moving up and off the top of the screen
if ((rocks*.dy < 0) &&
(rocks*.rock.y < -buffer)) {
rocks*.rock.y += screenHeight + 2 * buffer;
}
}
}
public function rockHit(rockNum:uint) {
playExplosion();
//create two smaller rocks
if (rocks[rockNum].rockType == "Big") {
newRock(rocks[rockNum].rock.x,rocks[rockNum].rock.y,"Medium");
newRock(rocks[rockNum].rock.x,rocks[rockNum].rock.y,"Medium");
} else if (rocks[rockNum].rockType == "Medium") {
newRock(rocks[rockNum].rock.x,rocks[rockNum].rock.y,"Small");
newRock(rocks[rockNum].rock.x,rocks[rockNum].rock.y,"Small");
}
//remove original rock
gameObjects.removeChild(rocks[rockNum].rock);
rocks.splice(rockNum,1);
}
/*************************************************
* MISSILES *
*************************************************/
public function newMissile() {
//create
var newMissile:Missile = new Missile();
//set direction
newMissile.dx = Math.cos(Math.PI*ship.rotation/180);
newMissile.dy = Math.sin(Math.PI*ship.rotation/180);
//placement
newMissile.x = ship.x + newMissile.dx*shipRadius;
newMissile.y = ship.y + newMissile.dy*shipRadius;
//add the stage and array
gameObjects.addChild(newMissile);
missiles.push(newMissile);
}
public function moveMissiles(timeDiff:uint) {
for (var i:int=missiles.length-1; i>=0; i--) {
//move
missiles*.x += missiles*.dx*missileSpeed*timeDiff;
missiles*.y += missiles*.dy*missileSpeed*timeDiff;
// moved off screen
if ((missiles*.x < 0 )|| (missiles*.x >screenWidth)||
(missiles*.y < 0 ) || (missiles*.y >screenHeight)) {
gameObjects.removeChild(missiles*);
missiles.splice(i,1);
}
}
}
//remove a missile
public function missileHit(missileNum:uint) {
gameObjects.removeChild(missiles[missileNum]);
missiles.splice(missileNum,1);
}
/*************************************************
* INTERACTION HANDLING *
*************************************************/
public function moveGameObjects(evt:Event) {
//get time difference
var timePassed:uint = getTimer() - lastTime;
lastTime += timePassed;
moveRocks(timePassed);
//send timePassed to moveShips function
if (gameMode != "delay") {
moveShip(timePassed);
}
moveMissiles(timePassed);
checkCollisions();
}
//look for missiles colliding with rocks
public function checkCollisions()
{
//loop through rocks
rockloop: for(var j:int=rocks.length-1;j>=0;j--)
{
//loop through missiles
missileloop: for(var i:int=missiles.length-1;i>=0;i--)
{
//collission detection
if (Point.distance(new Point(rocks[j].rock.x,rocks[j].rock.y),
new Point (missiles*.x,missiles*.y))
<rocks[j].rockRadius)
{
//remove rock and missile
rockHit(j);
missileHit(i);
// add score
gameScore += 10;
updateScore();
updateLevel();
//break out of this loop and continue next one
continue rockloop;
}
}
//rock for rock hitting ship
if (gameMode =="play")
{
if (shieldOn == false)
{
//only if shield is off
if(Point.distance(new Point(rocks[j].rock.x,rocks[j].rock.y),
new Point(ship.x,ship.y))
< rocks[j].rockRadius+shipRadius)
{
//remove ship and rock
shipHit();
rockHit(j);
}
}
}
}
// all out of rocks, chance game mode and trigger more
if ((rocks.length == 0) && (gameMode == "play"))
{
gameMode = "betweenlevels";
gameLevel++; //advance a level
delayTimer = new Timer(2000,1);
delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE,nextRockWave);
delayTimer.start();
}
}
/*************************************************
* SOUND EFFECTS *
*************************************************/
public function playExplosion()
{
//create a new sound using explosion class
var mySound:Explosion = new Explosion;
//play uisng a SoundChannel object
var myChannel:SoundChannel =mySound.play();
}
}
}