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

Calling a function on enter frame frame (HTML5 canvas)

Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Hi,

I have a newbie question.

 

I want to define and execute a function and apply that code it to a frame in the timeline.

 

Previously, I've used the same function on a button click event:

 

this.button_name.on('click', function ()
{
	var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	_this.movieclip.gotoAndStop(randomframe);
});

 

How do I modify the above so that it executes upon entering a frame instead of a click event?

 

Views

206

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

Community Expert , Jun 09, 2022 Jun 09, 2022
var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	_this.movieclip.gotoAndStop(randomframe);

 

will work.  but if you really want to execute a function, you can do it more verbosely with:

 

gotoF();

function gotoF(){

var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	_this.movieclip.gotoAndStop(randomframe);

}

Votes

Translate

Translate
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	_this.movieclip.gotoAndStop(randomframe);

 

will work.  but if you really want to execute a function, you can do it more verbosely with:

 

gotoF();

function gotoF(){

var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	_this.movieclip.gotoAndStop(randomframe);

}

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
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

p.s. if you want to eliminate the need to use:

 

var _this=this;

 

you can use:

 

gotoF.bind(this)();

function gotoF(){

var randomframe = Math.floor(Math.random() * _this.movieclip.totalFrames);
	this.movieclip.gotoAndStop(randomframe);

}

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
Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Thannk you. 

Works perfectly.

 

And thanks for the extra tips!

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
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

LATEST

you're welcome.

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