Skip to main content
NefJack
Participating Frequently
Jun 9, 2022 3:52 PM UTC
Answered

Calling a function on enter frame frame (HTML5 canvas)

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 is closed to new replies. Start a new post to keep the conversation going.
    kglad
    kgladCorrect answer
    Community Expert
    Jun 9, 2022 4:42 PM UTC
    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);

    }

    4 replies

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    Jun 9, 2022 4:42 PM UTC
    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);

    }

    Translate
    kglad
    Community Expert
    Community Expert
    Jun 9, 2022 4:45 PM UTC

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

    }

    Translate