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
3.5K
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
Explorer ,
Jan 09, 2016 Jan 09, 2016

nezarov thanks for help It works

Maby you have some advise for that?

Link to Discussion: https://forums.adobe.com/thread/2060248?sr=inbox

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 10, 2016 Jan 10, 2016

Remove children question already answered in that post

you only need to create a new layer with one key frame only and create the array variable in the frame 1:

var answerA:Array=[];  //now it'll be visible to the other key frames

then when you want to add a new word:

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

answerA = new Array(); // this line will remove the old elements, (clear)

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

var word:int;

.

.

.

then you can use that removeF() function on any frame to remove the movie clips

that's all.

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 10, 2016 Jan 10, 2016

I dont know what Im doing wrong but now first time removeF() removes 2 childs, second time 1 child, third and other times remove just 1 child like before.

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

I haven't seen the removeF function, but taking a guess, when you loop through the number of children, removing them, you have to count backwards:

for(var i = numChildren-1;i>=0;i--){

removeChild(i);

}

Another approach, if you're trying to remove all children, is to use While:

while(numChildren>0){

removeChildAt(0);

}

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 10, 2016 Jan 10, 2016

This is the function.

function removeF():void{

for(var i:int=answerA.numChildren-1;i>=0;i--){

if(answerA.stage){

removeChild(answerA);

}

answerA.splice(i,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
LEGEND ,
Jan 10, 2016 Jan 10, 2016

There are a few things that could go wrong with the way you work. For example, a displayobject item might have a stage, but not be a child of the current displayobject. The bigger problem is that you're splicing the array even if you didn't remove the child. Try putting the splice into the if statement.

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 10, 2016 Jan 10, 2016

Remove this line

answerA.splice(i,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
LEGEND ,
Jan 10, 2016 Jan 10, 2016

The array splicing technique may not be the best way to work, but if the plan is to have an array that lists the current children of interest, then it's an ok way to work. As I mentioned, splicing from the array even if you don't removeChild, may be the problem.

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 10, 2016 Jan 10, 2016

Add childs:

var currentWord:String = '';

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

    answerA = new Array();

    currentWord = answerS;

    raides = 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 < raides; i++) {

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

        letter = new C();

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

        letter.name = (char +i);

        addChild(letter);

        answerA.push(letter);

        letter.x = nextX;

        letter.y = y;

        nextX += letter.width + space;

    }

}

Function to remove:

function removeF():void{

for(var i:int=answerA.numChildren-1;i>=0;i--){

if(answerA.stage){

removeChild(answerA);

}

}

}

Call to function;

removeF();

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 10, 2016 Jan 10, 2016

I didn't see that: for(var i:int=answerA.numChildren-1;i>=0;i--)

use this function:

function removeF():void{

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

if(answerA.stage){

removeChild(answerA);

}

}

}

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 10, 2016 Jan 10, 2016

4 Days just for finding that ..beeep.. line

for(var i:int=answerA.length-1;i>=0;i--){

Everything works. Thanks for all who help me

I think someday to someone like me "green"  will be the easier to find answer to remove question.


Thanks!!! Haha Im happy like a girl after the first kiss

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 10, 2016 Jan 10, 2016

You're welcome & good luck.

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 10, 2016 Jan 10, 2016
LATEST

Thanks Good luck for you too.

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 10, 2016 Jan 10, 2016

I remove the splice but I have same result.

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 10, 2016 Jan 10, 2016

Make sure to put this code in a separate layer and don't use more than one key frame on the layer:

var answerA:Array=[];

function removeF():void{

for(var i:int=answerA.numChildren-1;i>=0;i--){

if(answerA.stage){

removeChild(answerA);

}

}

/////////////////////

also make sure to clear the answerA array when you want to add a new word:

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

answerA = new Array();

.

.

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 10, 2016 Jan 10, 2016

removeF(); if I add on key frame this 5 times then removes 5 childs if I add 8 times then removes 8 childs

I think here need somethig?

removeF(????);

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
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: " + 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.

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

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

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

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