Skip to main content
aleksis63820686
Inspiring
January 13, 2017
Answered

I want to create html canvas movie clip button

  • January 13, 2017
  • 1 reply
  • 1352 views

Hello

I created movie clip buttons with as3 and then converted to html canvas. I created buttons that worked perfectly in actionscript. But I can't get them to work in JavaScript. I'm using very simple code. Tell me what am I doing wrong?

/* Mouse Over Event

Mousing over the symbol instance executes a function in which you can add your own custom code.

Instructions:

1. Add your custom code on a new line after the line that says "// Start your custom code" below.

The code will execute when the symbol instance is moused over.

frequency is the number of the times event should be triggered.

*/

var frequency = 3;

stage.enableMouseOver(frequency);

this.btn1.addEventListener("mouseover", fl_MouseOverHandler_5);

function fl_MouseOverHandler_5()

{

  this.gotoAndPlay("over");// Start your custom code

  // This example code displays the words "Moused over" in the Output panel.

  alert("Moused over");

  // End your custom code

}

    This topic has been closed for replies.
    Correct answer kglad

    html5/canvas loses scope (or context) in functions.  so you must explicitly define 'this' inside function bodies.

    var frequency = 3;

    stage.enableMouseOver(frequency);

    this.btn1.addEventListener("mouseover", fl_MouseOverHandler_5.bind(this));

    function fl_MouseOverHandler_5()

    {

    this.gotoAndPlay("over");// Start your custom code

    // This example code displays the words "Moused over" in the Output panel.

    alert("Moused over");

    // End your custom code

    }

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    January 13, 2017

    html5/canvas loses scope (or context) in functions.  so you must explicitly define 'this' inside function bodies.

    var frequency = 3;

    stage.enableMouseOver(frequency);

    this.btn1.addEventListener("mouseover", fl_MouseOverHandler_5.bind(this));

    function fl_MouseOverHandler_5()

    {

    this.gotoAndPlay("over");// Start your custom code

    // This example code displays the words "Moused over" in the Output panel.

    alert("Moused over");

    // End your custom code

    }

    Legend
    January 13, 2017

    Well you don't have to use bind. You can also use the .target property of the MouseEvent object.

    Either way he'll definitely want to set mouseChildren = false on the movieclip, or things will get weird.

    aleksis63820686
    Inspiring
    January 15, 2017

    Well I still can't get it to work...

    What do you mean .target property of the MouseEvent object? (can you show me what you mean in code)

    my code looks like this and all my movie clips are looping. Only thing that works is that I get message when I roll over.

    mouseChildren = false

    /* Mouse Over Event

    Mousing over the symbol instance executes a function in which you can add your own custom code.

    Instructions:

    1. Add your custom code on a new line after the line that says "// Start your custom code" below.

    The code will execute when the symbol instance is moused over.

    frequency is the number of the times event should be triggered.

    */

    var frequency = 3;

    stage.enableMouseOver(frequency);

    this.btn1.addEventListener("mouseover", fl_MouseOverHandler_2.bind(this));

    function fl_MouseOverHandler_2()

    {

      this.gotoAndPlay("over");// Start your custom code

      // This example code displays the words "Moused over" in the Output panel.

      alert("Moused over");

      // End your custom code

    }