Skip to main content
Participant
March 27, 2016
Question

indexof alvays -1

  • March 27, 2016
  • 7 replies
  • 841 views

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

This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

robdillon
Participating Frequently
March 27, 2016

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.

Participant
March 28, 2016

So why when i add to code

trace(MyOBJ[1].name)

Flash always shows Name of the Movieclip. Even if i remove movieclip


robdillon
Participating Frequently
March 28, 2016

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.