I am currently working on my master’s degree thesis - it is a Simon-style game that adds audio notes to a dynamic timeline at the bottom of the interface. The user is then able to click and drag to re-arrange the audio notes to “remix” the sequence to their liking.
Right now, I am trying to make each button trigger one of 18 library sounds at random each time it is clicked. How do I write this into my existing code?
Here is my code so far:
import flash.geom.ColorTransform;
// Variables relating to score, pattern, etc.
var pattern = new Array();
var buttons = new Array();
buttons.push(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10, node11, node12, node13, node14, node15, node16, node17, node18);
var position = 0;
var playersTurn = false;
var score = 0;
// Start generating the pattern
setTimeout(nextMove, 1000); // Call "nextMove" after 1 second
// set the text at the bottom
output.text = "Watch the sequence...";
scorebox.text = "Score: "+score;
// Tell buttons to expect clicks from mouse
node1.addEventListener(MouseEvent.CLICK, clicked);
node2.addEventListener(MouseEvent.CLICK, clicked);
node3.addEventListener(MouseEvent.CLICK, clicked);
node4.addEventListener(MouseEvent.CLICK, clicked);
node5.addEventListener(MouseEvent.CLICK, clicked);
node6.addEventListener(MouseEvent.CLICK, clicked);
node7.addEventListener(MouseEvent.CLICK, clicked);
node8.addEventListener(MouseEvent.CLICK, clicked);
node9.addEventListener(MouseEvent.CLICK, clicked);
node10.addEventListener(MouseEvent.CLICK, clicked);
node11.addEventListener(MouseEvent.CLICK, clicked);
node12.addEventListener(MouseEvent.CLICK, clicked);
node13.addEventListener(MouseEvent.CLICK, clicked);
node14.addEventListener(MouseEvent.CLICK, clicked);
node15.addEventListener(MouseEvent.CLICK, clicked);
node16.addEventListener(MouseEvent.CLICK, clicked);
node17.addEventListener(MouseEvent.CLICK, clicked);
node18.addEventListener(MouseEvent.CLICK, clicked);
// Explain to Flash what clicked means
function clicked(clickInfo:MouseEvent) {
// Body of the function
if (!playersTurn) return; // same as playersTurn==false;
// Check that the clicked button was the right button
if (clickInfo.target==pattern[position]) {
score = score + 1; //could be written score++
scorebox.text = "Score: "+score;
output.text = "Yeah!";
// Increase the position in the pattern
position = position + 1;
// Check to see if it's the computer's turn
if (position==pattern.length) {
// It's the computer's turn
position = 0;
playersTurn=false;
output.text = "Watch the sequence...";
setTimeout(nextMove, 1000);
}
// Play the button's animation
clickInfo.target.gotoAndPlay(2);
} else {
// Game Over stuff
output.text = "Game Over";
playersTurn = false;
//Play the button they should have clicked
pattern[position].gotoAndPlay(2);
// gray out the button they did click
clickInfo.target.transform.colorTransform = new ColorTransform(0.5, 0.5, 0.5);
// reset the game after a few seconds
setTimeout(restartGame, 3000);
}
}
function nextMove() {
// If there are buttons left to play through, play them first
if (position<pattern.length) {
pattern[position].play();
position++; //position=position + 1;
setTimeout(nextMove, 1000/(1+pattern.length/10));
} else {
// Generate a random number between 0 and 17
var randomNumber = Math.floor(Math.random()*18);
pattern.push(buttons[randomNumber]);
buttons[randomNumber].gotoAndPlay(2);
playersTurn = true;
output.text = "Repeat the sequence...";
position = 0;
}
}
function restartGame() {
// reset everything and start again
pattern = new Array();
position = 0;
playersTurn = false;
score = 0;
// loop through the buttons to reset their color transforms
for (var i=0; i<buttons.length; i++) {
buttons*.transform.colorTransform = new ColorTransform();
}
// Start generating the pattern
setTimeout(nextMove, 1000); // Call "nextMove" after 1 second
// set the text at the bottom
output.text = "Watch the sequence...";
scorebox.text = "Score: "+score;
}