Copy link to clipboard
Copied
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);
}
}
}
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:
...Copy link to clipboard
Copied
nezarov thanks for help
It works ![]()
Maby you have some advise for that?
Link to Discussion: https://forums.adobe.com/thread/2060248?sr=inbox
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Remove this line
answerA.splice(i,1);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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);
}
}
}
Copy link to clipboard
Copied
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 ![]()
Copy link to clipboard
Copied
You're welcome & good luck.
Copy link to clipboard
Copied
Thanks
Good luck for you too.
Copy link to clipboard
Copied
I remove the splice but I have same result.
Copy link to clipboard
Copied
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();
.
.
Copy link to clipboard
Copied
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(????);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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..
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more