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

Call a simple function in Animate CC

Explorer ,
Feb 02, 2017 Feb 02, 2017

Hi,

i would like to call a simple function in Animate CC.

The function should start when the movie is loading.

A MovieClip on the timeline (cycle) should do something..

.YES. there ist the right INSTANCE name for cycle.

Code:

function Hallo() {

  console.log("Hallo");//this works

  this.cycle.x=300; //does not work

}

Hallo();

message Console:Uncaught TypeError: Cannot set property 'x' of undefined

3.5K
Translate
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

Community Expert , Feb 02, 2017 Feb 02, 2017

you're losing context.  try:

function Hallo(tl) {

console.log("Hallo");//this works

tl.cycle.x=300; //does not work

}

Hallo(this);

Translate
Community Expert ,
Feb 02, 2017 Feb 02, 2017

you're losing context.  try:

function Hallo(tl) {

console.log("Hallo");//this works

tl.cycle.x=300; //does not work

}

Hallo(this);

Translate
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
Explorer ,
Feb 02, 2017 Feb 02, 2017

Thank you.

And what should i write to call this function from/in another function?

function fl_MouseClickHandler()

{

  console.log("clicked");

  //how to call the Hallo(tl) function

}

Translate
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
Community Expert ,
Feb 02, 2017 Feb 02, 2017

with event listeners you can use the function.bind proxy:

this.whatever.addEventListener('click',fl_xxx.bind(this));

function fl_xxx(){

this.cycle.x+=30;

}

Translate
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
Explorer ,
Feb 02, 2017 Feb 02, 2017

Yes, thank you. My question was related to the first question.
How can i call now the Hello function from the timeline also on click?

this.whatever.addEventListener('click',fl_xxx.bind(this));

function fl_xxx(){

  //how to call from here the Hallo(tl) function?????????????

this.cycle.x+=30;

}

Translate
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
Community Expert ,
Feb 02, 2017 Feb 02, 2017

there are several ways to do that, but the most concise (afaik) is

var tl = this;

tl.whatever.addEventListener('click',f_xxx);

f_xxx()

function f_xxx(){

tl.whatever.x += 33;

}

Translate
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
Explorer ,
Feb 07, 2017 Feb 07, 2017

OK, thanks- solved it and it's easier for me to use the scope keyword this.

this.moveCycle = function(){

        this.cycle.x=300;

          };

this.moveCycle(); //call function from Timeline from any frame

this.button_left.addEventListener("click", fl_MouseClickHandler.bind(this));

     function fl_MouseClickHandler()

          {

         this.moveCycle();//call function on click

          }

Translate
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
Community Expert ,
Feb 07, 2017 Feb 07, 2017
LATEST

you're welcome.

Translate
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