• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Pass Instance Name into function

Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Hi Everyone, sorry for posting so many beginners questions in the last few days...,

Whats wrong with this code?

function fadeOUT(e:String):void

{

  this.addEventListener(Event.ENTER_FRAME, fadeOUT);

  this.alpha -= 0.1;

  if (this.alpha <= 0)

  {

       this.removeEventListener(Event.ENTER_FRAME, fadeOUT);

       trace("fire")

  }

}

fadeOUT("McInstanceName");

If the code is like this i get an "

TypeError: Error #1010: A term is undefined and has no properties.

  at Test87_fla::MainTimeline/fadeOUT()"

Here is the Part i don't understand: The code does not produce Errors if i do one of the following:

  • Change this.alpha -= 0.1; to this.alpha -= 1;
  • Comment out this.addEventListener(Event.ENTER_FRAME, fadeOUT);

Is there maybe another way to pass a movieClip into a function?

I would appreciate any help, thanks

Views

655

Translate

Translate

Report

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 , May 22, 2017 May 22, 2017

You're calling fadeOut(needs a string) back to itself as fadeOut(needs an Event). Make it be two functions, say one is startFadeOut(e:String) that would apply the listener, and then fadeOut(e:Event) that does the enterframe alpha change.

Votes

Translate

Translate
LEGEND ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

You're calling fadeOut(needs a string) back to itself as fadeOut(needs an Event). Make it be two functions, say one is startFadeOut(e:String) that would apply the listener, and then fadeOut(e:Event) that does the enterframe alpha change.

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Thanks Colin! I LOVE this Community!

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Follow up question:

my code now produces the same error.

If i do not pass a String into fadeOUT(), how can i specify, which Movie Clip Instance i want the alpha change applied to?

I fear, i am too stupid for AS3...

function StartFadeOUT(e:String):void

{

  this.addEventListener(Event.ENTER_FRAME, fadeOUT);

}

function fadeOUT(e:Event):void

{

  this.alpha -= 0.1;

  if (this.alpha <= 0)

  {

  this.removeEventListener(Event.ENTER_FRAME, StartFadeOUT);

  }

}

StartFadeOUT("McInstanceName");

Votes

Translate

Translate

Report

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 ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

I feel sure there may be a better way to organize the code. But for now, try:

function fadeOUT(e:Event):void

{

  this.target.alpha -= 0.1;

Votes

Translate

Translate

Report

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 ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

That should have been:

function fadeOUT(e:Event):void

{

  e.target.alpha -= 0.1;

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Thanks Colin,

if i just change this.alpha -= 0.1; to e.target.alpha -= 0.1; i get the same error,

if i comment out the if statement, alpha goes instantly to 0, without fading or me ever seeing the specified MC...

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Colin Holgate

Scratch the last comment,

the following code seems to work:

function fadeOUT(e:Event):void

{

  e.target.alpha -= 0.01;

  if (e.target.alpha <= 0)

  {

  e.target..removeEventListener(Event.ENTER_FRAME, fadeOUT);

  trace("fire")

  }

}

notice, i changed 0.1 to 0.01? Any idea, why it would fade out much more quickly now?

Thanks again

Votes

Translate

Translate

Report

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 ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Sigh. Seriously, you're Doing It Wrong. Ditch the event listener entirely and use the Tween class instead. It's one line of code. ONE.

I realize you're emotionally invested in making your original approach work, but there's no room for sentiment in coding.

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

LATEST

ClayUUID

OK, i had a look at the documentation of the tween class. If you tell me which ONE line of code that would be, i'll use it. Say i have 3 MovieClips MC1, MC2 and MC3, and i want to fade in MC3 and fade the currently visible one out, no matter which MC is currently visible. What would that one line of code look like? Say, i want to add MC4,5,6,7, and 8 afterwards. If you cannot/ do not want to provide that ONE line of code, then i'll use the approach i worked out with the help of Colin Holgate​. You may be right that there is no room for sentiment in coding, but as a novice i learned very much about AS3 and coding in general by my own approach.

I'll even unmark Collins answer as correct and yours as correct if you provide that ONE line of code (provided it does work for me).

Thanks and kind regards

Votes

Translate

Translate

Report

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 ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

If all you want to do is fade something out, why aren't you just using the Tween class? It would be so much simpler.

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Hi, Clay, thanks for your reply,

if you would explain to me, what a "Tween Class" is and how i would use it, i would very much appreciate it. I just started using Animate a week ago...

Votes

Translate

Translate

Report

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 ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

Tween - Adobe ActionScript® 3 (AS3 Flash) API Reference

import fl.transitions.Tween;

import fl.transitions.easing.None;

new Tween(McInstanceName, "alpha", None.easeNone, 1, 0, 10);

Votes

Translate

Translate

Report

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
Contributor ,
May 22, 2017 May 22, 2017

Copy link to clipboard

Copied

ClayUUID​ Thanks i'll have a look at that.

In the End i want to have a method that Fades Every MC in an array out, while fading only the specified MC in...

Votes

Translate

Translate

Report

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