Copy link to clipboard
Copied
Ok I have problem with indexof . I have 3 named MovieClips(R,G,B) on 1 layer and this code in main timeline
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
var MyOBJ:Array = [R,G,B]
stage.addEventListener(MouseEvent.CLICK , fclick)
stage.addEventListener(KeyboardEvent.KEY_DOWN , NextStage)
function fclick(e:MouseEvent):void
{
trace("Name___"+e.target.name)
trace("Index__"+MyOBJ.indexOf(e.target))
}
function NextStage(e:KeyboardEvent):void
{
if(e.keyCode==Keyboard.LEFT)
prevFrame();
if(e.keyCode==Keyboard.RIGHT)
nextFrame();
}
stop();
in second frame I delete MovieClip 'R' in third frame I delete MovieClip 'G' but in forth frame I add MovieClip 'R' and MovieClip 'G'.
So why in last frame when I click on MovieClip 'R' or 'G'
MyOBJ.indexOf(e.target) always is -1
http://www.newgrounds.com/dump/item/fb5844ecb53e1f62100f3cd7ee748c8b
Copy link to clipboard
Copied
If you delete the object on the stage then the reference to that object in the array is also broken. When you add a new instance of R and G, those new instances are not the same as the instances that are linked to the object names in the array. When you add or remove objects on the stage, update the array to reflect those changes. When you remove an object from the stage, remove it from the array, and then, when you add a new copy of one of those objects, add it to the array again.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I have no idea. You are showing me small bits of your file. I can only tell you how Flash works based on what you tell me.
Copy link to clipboard
Copied
Here you go fla file
http://www.newgrounds.com/dump/download/320c362ab48ef48b85cb0f53454aaec9
Copy link to clipboard
Copied
OK, as I said above, if you remove an object from the stage, then any reference to that object in the code is lost. You have two boxes named G and B. In frame 1 those objects relate to the references in the array MyOBJ. When you remove the box with the instance name of G in frame two, you lose the connection to the array. When you put a new object on the stage in frame 5, and name it G. It is named G but it is not related to the G in the array. To keep your array relevent, you'll need to remove the reference in the array when you remove the object on the stage and then put a new reference back in to the array when you put a new G object on the stage.
A much simpler solution is to NOT remove the object from the stage, but to move it off the visible area of the stage and then move it back. A variation on that is to change the alpha property or the visible property as needed to show and hide an object.
Copy link to clipboard
Copied
But why
trace(MyOBJ[1].name)
always work even if i remove this movieclip ?
A much simpler solution is to NOT remove the object from the stage, but to move it off the visible area of the stage and then move it back. A variation on that is to change the alpha property or the visible property as needed to show and hide an object.
-how this solution work with computer memory , if I have 100000 movieclips off the screen or alpha set to 0 ?
Copy link to clipboard
Copied
When you place the object on the stage, you are giving it an instance name. When you ask for that name, you will get a reply. As far as Flash is concerned those two instances of that object are two unique things, even though they have the same name.
I don't know how much memory you are working with, or how large or small each of these movieClips are, so I can't really tell you how well or poorly your file might work in any case. I'm trying to offer you options that may help you to achieve the end result that you are after. You are free to use any method that you like.