AS3 If (child on screen){play child} (keyboard.event)
Maby someone can help me with this?:
I have this script:
var answerA:Array=[];
function answerF(answerS:String,x:int,y:int,space:int):void{
var raides:int = answerS.length;
var word:int;
if ((answerS.length % 2) == 0){
word = (((raides * 150) + ((raides - 1) * space))/2 - 150);
} else {
word = ((((raides -1) * 150) + ((raides - 1) * space))/2)-75;
}
var nextX:int = x - word;
var C:Class;
var letter:MovieClip;
for(var i:int=0;i<answerS.length;i++){
C=Class(getDefinitionByName(answerS.charAt(i)));
letter=new C();
addChild(letter);
answerA.push(letter);
letter.x=nextX;
letter.y=y;
nextX+=letter.width+space;
}
}
On library i have objects with names A,B,C,D,E,F... to Z
This script adds letters from library. With this script everything ok he works perfectly for me but I want to do Keyboard event for every letter to play but now i dont know how to do that.
I have a script:
stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);
function AKeyDown(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.A){
A.play();
}
}
This script works then object have instace name.
And this is that I want to do:
stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);
function AKeyDown(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.A){
if Child with class name A exist on screen {
play object with class name A
} else {
var errorR: Sound = new eror();
errorR.play(0, 1);
}
}
}
