Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Jan 09, 2016 Jan 09, 2016

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

  }

}

}

TOPICS
ActionScript
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 09, 2016 Jan 09, 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:

...
Translate
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

if (childName.stage)

{

     childName.play();

     trace("Exist");

}

else

{

// your code.

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

1119: Access of possibly undefined property stage through a reference with static type Class.

1061: Call to a possibly undefined method play through a reference with static type Class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 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);

  }

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Try this:

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

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

letter=new C();

//give in a name:

letter.name = (answerS.charAt(i)) + "_mc"; // A_mc

addChild(letter);

answerA.push(letter);

letter.x=nextX;

letter.y=y;

nextX+=letter.width+space;

}

Then:

if(answerA.A_mc.stage) // The character in the string must be A not a

{

     answerA.A_mc.play();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

#1010: A term is undefined and has no properties.

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

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

   if(answerA.A_mc.stage){

     answerA.A_mc.play(1);

} else {

    var errorR: Sound = new eror();

    errorR.play(0, 1);

  }

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Sorry it was wrong to use answerA. it's an array not movie clip.. remove it and try

if(A_mc.stage){

     A_mc.play(1);

}

Note that Mr. Ned Murphy's answer is more professional try to understand it to fix the error.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

Thanks for help.

But im "GREEN" in as3. I understand simple things and can do it but with childs I still dont undertand how it works.

I dont know how to change the keyboard character to uppercase.

NOW I HAVE THIS:

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

letter.name = (answerS.charAt(i)) + "_mc"; // A_mc

addChild(letter);

answerA.push(letter);

letter.x=nextX;

letter.y=y;

nextX+=letter.width+space;

}

}

And for keyboard events i have this:


stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

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

   if(answerA.A_mc.Stage){

     answerA.A_mc.play();

} else {

    var errorR: Sound = new eror();

    errorR.play(0, 1);

  }

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Use this code and mark the reply number 4 as the correct answer please

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

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

    try

    {

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

        ltr.play();

    }

    catch (error)

    {

        trace("Wrong Char");

        var errorR: Sound = new eror();

        errorR.play(0, 1);

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 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);

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

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. 😕

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Do you want to play the similar characters together in one key press?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

Yes.

Example:

AABCDA

If I pres A key play all 3 letters movie clips on stage.

BADFEBB

If I pres B key play all B letters movie clips on stage.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

//first thing edit this line in the answerF() function:

letter.name = (answerS.charAt(i) +i); // the names will be "A0, A1 etc..

//create this variable:

var lettersChecked:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void{

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

    for(var i:int = 0; i< 4; i++) // change number 4 to answerString.length (use the same string you're sending to the answerF() function)

    {

        try

        {

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

            upperCaseltr.play();

            lettersChecked += 1;

        }

        catch (error)

        {

            false;

        }

    };

    checkError();

};

function checkError():void

{

    if(lettersChecked == 0) // if still 0 that means the for-loop didn't count any movie clip and all the attempts were failed. that means the pressed key was wrong

    {

        var errorR:Sound = new eror();

        errorR.play(0, 1);

        lettersChecked = 0; // reset

    }

    else

    {

        lettersChecked = 0; // reset

    }

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

for(var i:int = 0; i< 4; i++) // change number 4 to answerString.length (use the same string you're sending to the answerF() function)


Like this:

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


I have error:

1120: Access of undefined property answerS.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

No, answerS string is not visible to the other functions.. How you're sending the answerS to the answerF?

like this?

function answerF("THE ANSWER",10,10)

or like this?

function answerF(someString,10,10)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

answerF(answersArray[randomArray[index]],960,540,5);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Ok you can create one more string:

var currentWord:String = '';

then when you send a new word to the answerF , update the currentWord string:

function answerF(answerS:String,x:int,y:int,space:int):void{

currentWord = answerS;

var raides:int = answerS.length;

var word:int;

.

.

.

now you can use the currentWord.length in the for-loop:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Or you can also use the raides variable instead but make it visible to the other functions by creating it in the main timeline not inside the function:

var raides:int = 0;

function answerF(answerS:String,x:int,y:int,space:int):void{

raides = answerS.length; //update it and use it anywhere

var word:int;

.

.

for(var i:int = 0; i< raides; i++)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

Thank you very much Everything is working.

I have some problems with Lithuanian language. In lithuanian language is some special letters like: Ą,Č,Ę,Ė,Į,Š,Ų,Ū,Ž. Then script uploads answer from txt file these litnuanian letters he understand like À,È,Æ,Ë,Á,Ð,Ø,Û,Þ and I dont know why.. For that reason for lithuanian letters move clips I add class names with other symbols. Letter Ą class name is À, letter Č class name is È, letter Ę = Æ, letter Ė = Ë, Į = Á, Š = Ð, Ų = Ø, Ū = Û, Ž = Þ.  So how i can do that then I press the A key the flash plays all childs with class name A and À... Key C all childs with clas name C and È.. Key E plays E, Æ and Ë.and etc.

What need to write that cript understands that A and À this is same letter. C and È the same.. E and  E, Æ same letters and etc.

Maby you know solution for that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

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

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

        letter = new C();

       var char:String = answerS.charAt(i);

     // Change the letter before you naming the movie clip:

        switch (char) {

            case "À": // you must change all the Lithuanian letters in the string

                char = "A"

                break;

           

            case "È":

                char = "C"

                break;

         

               // put all the cases in the same way

        }

        letter.name = (char +i);

        addChild(letter);

        answerA.push(letter);

        letter.x = nextX;

        letter.y = y;

        nextX += letter.width + space;

        //trace(letter.name);

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

Thank you very much You my hero!!! Thanks! Everything works perfect.

Just some problems with sound. Becouse "error" sound plays then I press any key I mean numbers, shift, ctrl, esc, alt and others. Maby there is simple way to split just letter keys? (QWERTYUIOPASDFGHJKLZXCVBNM)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

I think this is the only way to do that:

stage.addEventListener(KeyboardEvent.KEY_DOWN, AKeyDown);

function AKeyDown(event:KeyboardEvent):void
if(event.keyCode == 65 || event.keyCode == 66) //from 65 to 90 a to z, you have to put them all in the if statemet

{

  //put all the code here.

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2016 Jan 09, 2016

I have just to write keyCodes and use just one if? I mean like this:?


if(event.keyCode == 65 || event.keyCode == 66 || event.keyCode == 67 || event.keyCode == 68|| event.keyCode == 69 || event.keyCode == 70 || event.keyCode == 61 || event.keyCode == 62 || event.keyCode == 63 || event.keyCode == 64 || event.keyCode == 65 || event.keyCode == 66 || event.keyCode == 67 || .......)

{

//// This is all code for childs playing.

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2016 Jan 09, 2016

Yes

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines