:love: Learning AS3 and loving it
This creates a random number of items from 1 to 10.
I’d like to know, depending on how many items were created, how to play a sound when clicking each item, in increments:
[COLOR=#0000ff]Example: if 3 items are created,
1st click will play sound “one”.
2nd click will play sound “two”,
3rd click will play sound “three”[/COLOR][COLOR=#008000]
[/COLOR]
thanks!
Sounds.as
package
{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
public class Sounds
{
[Embed(source = "/sounds/0.mp3")]
private var Number0:Class;
//... to number 10
[Embed(source = "/sounds/10.mp3")]
private var Number10:Class;
public var [COLOR=#0000ff]number0[/COLOR]:Sound = new Number0();
public var [COLOR=#0000ff]numberChannel0[/COLOR]:SoundChannel = new SoundChannel();
public var number10:Sound = new Number0();
public var numberChannel10:SoundChannel = new SoundChannel();
public function Sounds()
{
}
}
}
Main.as
package
{ //v0.1.2
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
//Classes needed to play sounds
import flash.media.Sound;
import flash.media.SoundChannel;
public class Main extends MovieClip
{
private var _objectsRemaining:uint;
//A variable to track the current index number
//of the counted object being accessed in the loop
private var _objectsCounted:uint;
private var _gameStatus:String;
private var _gameWon:Boolean;
private var _startMessage:String;
//Display the Game Over Won screen
public var _gameOver:GameOver = new GameOver();
[COLOR=#0000ff]//Create the _sounds object from the custom Sounds class
private var _sounds:Sounds = new Sounds();
//An array to store the names of all the sounds
private var _cardSounds:Array = new Array();[/COLOR]
function hideThisButton(event:MouseEvent):void
{
//Works:
trace(_cardSounds[_objectsCounted]);
//_sounds.numberChannel[_objectsCounted] = _sounds._cardSounds[_objectsCounted].play();
[COLOR=#008000]// Works:
_sounds.numberChannel0 = _sounds.number0.play();[/COLOR]
** [COLOR=#b22222]// DOESN'T WORK
[/COLOR] **[COLOR=#b22222]**//_sounds.numberChannel[_objectsCounted] = _sounds._cardSounds[_objectsCounted].play();**
[/COLOR] _objectsCounted++;
_objectsRemaining--;
output.text = "" + _objectsCounted;
trace("You clicked on: " + event.currentTarget + " Objects counted: " + _objectsCounted + " " + _objectsRemaining);
event.target.visible = false;
if (_objectsRemaining == 0)
{
_gameWon = true;
endGame();
}
}
public function init():void
{
//directives inside the constructor method are automatically run when the class is instantiated
//this is to reinitialize (RESET) everything easily from Anywhere in the program!
//Let's Initialize variables!
trace("Game started!");
_startMessage = "Let's count!";
_objectsRemaining = Math.round(Math.random() * 10);
_objectsCounted = 0;
_gameWon = false;
//Initialize text fields
messageTextBox.text = _startMessage;
output.text = "";
output.backgroundColor = 0xffffff;
output.restrict = "0-9";
//Initialize the card sounds array
_cardSounds =
[
"number0",
"number1",
"number2",
"number3",
"number4",
"number5",
"number6",
"number7",
"number8",
"number9",
"number10"
];
for (var k:int = 0; k < _objectsRemaining; k++)
{
var newEnemy:Enemy = new Enemy ;
var randomValue:Number = Math.random() * 1 + .7;
newEnemy.x = 200 + Math.random() * 700;
newEnemy.y = 250 + Math.round(Math.random() * 400);
newEnemy.scaleX = newEnemy.scaleY = randomValue;
newEnemy.rotation = randomValue;
//newEnemy.alpha = 1 - randomValue;
this.addChild(newEnemy);
newEnemy.addEventListener(MouseEvent.CLICK, hideThisButton);
}
}
}
}