00:00
00:00
Newgrounds Background Image Theme

Splatstreamslim just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 referring to MC's with strings

286 Views | 4 Replies
New Topic Respond to this Topic

I'd like to hide the MC I've clicked. I can get the string name of the MC I clicked with

e.target.name

I want to do

e.target.name.visible=false;

Of course this doesn't work because e.target.name is a string.

I know there is this

MovieClip(e.target.name).visible=false;

But that's for MC's. Is there anything like this but for Buttons?

I can do this

MovieClip(root)[ev.target.name].visible=false;

And it works, but I shouldn't need to get the MovieClip root because it's not in an MC and...

[ev.target.name]

doesn't work alone, what can I use instead? stage in front doesn't work either.

If possible, please answer both of the questions.


If e.target.name is the name of the object you clicked, then

stage.getChildByName(e.target.name).visible = false;

should work; that is, if it belongs to stage. I'm not sure on what would happen if it belongs to one of stage's descendants, although it should still work in theory.

Edit: You don't really need to do all of that on second thought.

e.target

IS the object you clicked, or if you want to be sure (if you've got a listener on the button, but you clicked a textfield inside the button, for example) then you could use

e.currentTarget

to get the object processing the listener (that is, the button you clicked, provided it has a direct CLICK/etc. listener) and cast it to Button.

In effect, something like this:

(e.target as Button).visible = false

or

(e.currentTarget as Button).visible = false

[Tip: using the "as" cast, shown above, is faster than using the constructor-style cast - (a as Button) is faster than Button(a) and if a is not a Button, then (a as Button) returns null instead of throwing an error like Button(a) does]


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to AS3 referring to MC's with strings 2017-09-09 13:06:11


At 9/8/17 10:39 PM, Gimmick wrote: If e.target.name is the name of the object you clicked, then

stage.getChildByName(e.target.name).visible = false;

should work; that is, if it belongs to stage. I'm not sure on what would happen if it belongs to one of stage's descendants, although it should still work in theory.

Edit: You don't really need to do all of that on second thought.

e.target

IS the object you clicked, or if you want to be sure (if you've got a listener on the button, but you clicked a textfield inside the button, for example) then you could use

e.currentTarget

to get the object processing the listener (that is, the button you clicked, provided it has a direct CLICK/etc. listener) and cast it to Button.

In effect, something like this:

(e.target as Button).visible = false

or

(e.currentTarget as Button).visible = false

[Tip: using the "as" cast, shown above, is faster than using the constructor-style cast - (a as Button) is faster than Button(a) and if a is not a Button, then (a as Button) returns null instead of throwing an error like Button(a) does]

Thanks Gimmick, that's very helpful!
I never thought about putting code in parentheses like this.

Is there no equivalent of MovieClip(e.target.name).visible=false; ?
Of course the ones you've stated will work, I am just curious.

Cheers!

Response to AS3 referring to MC's with strings 2017-09-09 23:14:12


At 9/9/17 01:06 PM, Aprime wrote: Is there no equivalent of MovieClip(e.target.name).visible=false; ?

I dunno, I haven't really worked with movieclips enough to know. I usually use Sprites instead, and if it's the same as movieclip, then it wouldn't work, since you're converting a String to a Movieclip which may or may not work (usually, it should not).

That said, you could try

MovieClip(e.target).visible = false

, which is slightly different, but should achieve the same result.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to AS3 referring to MC's with strings 2017-09-10 08:43:57


At 9/9/17 11:14 PM, Gimmick wrote: That said, you could try

MovieClip(e.target).visible = false

, which is slightly different, but should achieve the same result.

Ah wow, I should have known. Haven't tried it yet, but I'm sure i would work. Thanks!