Skip to main content
dalvydasv27776233
Inspiring
January 9, 2016
Answered

AS3 If (child on screen){play child} (keyboard.event)

  • January 9, 2016
  • 2 replies
  • 3276 views

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);

  }

}

}

This topic has been closed for replies.
Correct answer Ned Murphy

What you should do is assign each child a name as you load them.  Then you can target them using their name property.

var C:Class;

var letter:MovieClip;

for(var i:int=0;i<answerS.length;i++){

C=Class(getDefinitionByName(answerS.charAt(i)));

letter=new C();

letter.name = answerS.charAt(i);

addChild(letter);

answerA.push(letter);

letter.x=nextX;

letter.y=y;

nextX+=letter.width+space;

}

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

    trace("Key Pressed: " + String.fromCharCode(event.charCode));
   var ltr:MovieClip = MovieClip(getChildByName(String.fromCharCode(event.charCode)));
   ltr.play();

}

You will probably need to convert the String.fromCharCode(event.charCode) to uppercase if you are naming them with uppercase.

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
January 9, 2016

What you should do is assign each child a name as you load them.  Then you can target them using their name property.

var C:Class;

var letter:MovieClip;

for(var i:int=0;i<answerS.length;i++){

C=Class(getDefinitionByName(answerS.charAt(i)));

letter=new C();

letter.name = answerS.charAt(i);

addChild(letter);

answerA.push(letter);

letter.x=nextX;

letter.y=y;

nextX+=letter.width+space;

}

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

    trace("Key Pressed: " + String.fromCharCode(event.charCode));
   var ltr:MovieClip = MovieClip(getChildByName(String.fromCharCode(event.charCode)));
   ltr.play();

}

You will probably need to convert the String.fromCharCode(event.charCode) to uppercase if you are naming them with uppercase.

dalvydasv27776233
Inspiring
January 9, 2016

TypeError: Error #1009: Cannot access a property or method of a null object reference.

function AKeyDown(event:KeyboardEvent):void{

   trace("Key Pressed: " + String.fromCharCode(event.charCode));

   var ltr:MovieClip = MovieClip(getChildByName(String.fromCharCode(event.charCode)));

   ltr.play();

}

With this code i have sound on any keyboard key:

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

if (event.keyCode == Keyboard.A){

   trace("Key Pressed: " + String.fromCharCode(event.charCode));

   var ltr:MovieClip = MovieClip(getChildByName(String.fromCharCode(event.charCode)));

   ltr.play();

} else {

    var errorR: Sound = new eror();

    errorR.play(0, 1);

    }

}

Im going crazzy..

Ned Murphy
Legend
January 9, 2016

As I mentioned in my original response, you might need to change the keyboard character to be uppercase if you are naming the objects using uppercase.

For the 1009 error:

The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

- is declared but not instantiated

- doesn't have an instance name (or the instance name is mispelled)

- does not exist in the frame where that code is trying to talk to it

- is animated into place but is not assigned instance names in every keyframe for it

- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

Inspiring
January 9, 2016

if (childName.stage)

{

     childName.play();

     trace("Exist");

}

else

{

// your code.

}

dalvydasv27776233
Inspiring
January 9, 2016

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

if (event.keyCode == Keyboard.A){

    if (A.stage){

        A.play();

     trace("Exist"); 

    } else {

    var errorR: Sound = new eror();

    errorR.play(0, 1);

  }

}

}

dalvydasv27776233
Inspiring
January 9, 2016

This function will check the both cases lowercase & uppercase:

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

    trace("Key Pressed: " + String.fromCharCode(event.charCode));

    try

    {

        var lowerCaseltr:MovieClip = MovieClip(getChildByName(String.fromCharCode(event.charCode)));

        lowerCaseltr.play();

    }

    catch (error)

    {

        try

        {

            var upperCaseltr:MovieClip = MovieClip(getChildByName(String(String.fromCharCode(event.charCode)).toUpperCase()));

            upperCaseltr.play();

        }

      

        catch (error)

        {

        trace("Wrong Char");

        //var errorR: Sound = new eror();

        //errorR.play(0, 1);

        }

    }

}


It works

BUT

If in stage script add 2 same objecta.. key event play just one.

For example:

If I have letter childs

ABCDEFA

tha last A didnt play.

And if where is no letter the script didt play the error sound. :/