CODING THE startGame FUNCTION

The main screen controlled the whole game’s main functionality.  One of the most important factors of my game was to make sure the games randomly loaded. It was easy to load individual games, I would only need to create a variable for the class, and then import the class with its objects on top of the main screen. But this wouldn’t work for creating random games.

It took me a while to work out how to randomise the games. I didn’t have previous knowledge of arrays, but after doing research I found out that this was what I needed to make this work. I could create a variable for the array, which would hold all of the game levels, and then use the array as this item to call. Here is the randomising game function:

 

//return a random number between 2 others

private function startGame(event:Event):void{
if(event.target != controls_mc)
{
stage.removeEventListener(MouseEvent.CLICK, startGame);

//Create an array for the different possible games
var gameArray = new Array;

var test = new Main1();
var test1 = new Main1();
var test2 = new breath();
var test3 = new Falling();
var test4 = new Stop();
var test5 = new Tapping();

//add classes to the array – this will make it easier to add more games in the future.
gameArray.push(test, test1, test2, test3, test4, test5);

//start multiple games after each other (without looping).
//choose a random number between 0 and the length of the gameArray -1.
var gameIndex = randomMinMax(0, (gameArray.length – 1));

trace(gameIndex);

//assign a variable to the object of the randomly assigned game.
var gameObject = gameArray[gameIndex];

//create a new instance of the game object and play it!
//game = new gameObject();
addChild(gameObject);

This method worked brilliantly, and didn’t end up causing any other issues with the rest of my code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s