Skip to main content
NefJack
Participating Frequently
June 9, 2022
Answered

Calling a function on enter frame frame (HTML5 canvas)

  • June 9, 2022
  • 1 reply
  • 344 views

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?

 

    This topic has been closed for replies.
    Correct answer kglad
    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);

    }

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    June 9, 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);

    }

    kglad
    Community Expert
    Community Expert
    June 9, 2022

    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);

    }