Skip to main content
hollym69824027
Participating Frequently
May 11, 2017
Answered

Converting an Actinscript code to Javascript for HTML 5 canvas.

  • May 11, 2017
  • 3 replies
  • 828 views

I'm working on an animation on action script through Animate CC and need to convert it to HTML 5 canvas which I have done. Issue being the only code format I am having trouble converting to java script is the following :

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);

function keyDownListener(e:KeyboardEvent){

if (e.keyCode == Keyboard.RIGHT) {

nextFrame();

}

if (e.keyCode == Keyboard.LEFT) {

prevFrame();

}

}

If anyone can help me if would be much appreciated as this is mainly needed for a web browser. TIA.

    This topic has been closed for replies.
    Correct answer kglad

    window.onkeydown = keyDownListener.bind(this);

    function keyDownListener(e){

    if (e.keyCode == 39) {

    this.gotoAndStop(this.currentFrame+1);  // you should be doing frame limit checking

    }

    if (e.keyCode == 37) {

    this.gotoAndStop(this.currentFrame-1); // you should be doing frame limit checking

    }

    }

    3 replies

    hollym69824027
    Participating Frequently
    May 13, 2017

    if (e.keyCode == 39) {

    this.gotoAndStop(this.currentFrame+1);  // you should be doing frame limit checking

    In that section of the code,  am I supposed to add in how many frames in total my animation is since my movie clip moves by each frame ?

    kglad
    Community Expert
    Community Expert
    May 13, 2017

    yes.  i would do this:

    if (e.keyCode == 39 && this.currentFrame<this.totalFrames) {

    this.gotoAndStop(this.currentFrame+1);

    hollym69824027
    Participating Frequently
    May 13, 2017

    only after adjusting the code on the HTML5 canvas and it works! thank you very much for your help, its taken a load of my mind for this project.

    hollym69824027
    Participating Frequently
    May 12, 2017

    so what does your first message mean exactly? please bare with me as this is my first time using flash. does your code suggest to change certain formats on the HTML 5 canvas?

    kglad
    Community Expert
    Community Expert
    May 12, 2017

    there's nothing related to formatting or formats.

    this is the code if you want to start with your timeline stopped.  is there any line of the code that you do not understand?

    this.stop()

    window.onkeydown = keyDownListener.bind(this);

    function keyDownListener(e){

    if (e.keyCode == 39) {

    this.gotoAndStop(this.currentFrame+1);  // you should be doing frame limit checking

    }

    if (e.keyCode == 37) {

    this.gotoAndStop(this.currentFrame-1); // you should be doing frame limit checking

    }

    }

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    May 11, 2017

    window.onkeydown = keyDownListener.bind(this);

    function keyDownListener(e){

    if (e.keyCode == 39) {

    this.gotoAndStop(this.currentFrame+1);  // you should be doing frame limit checking

    }

    if (e.keyCode == 37) {

    this.gotoAndStop(this.currentFrame-1); // you should be doing frame limit checking

    }

    }

    hollym69824027
    Participating Frequently
    May 12, 2017

    Thank you for your response kglad but I have tried to copy your code and use it on a web browser and it doesn't allow me to move my characters at all.

    I'll show you a screen shot of my action script code on my other file.

    If you are able to send me through another type of code or give me advice on maybe how to format it properly then it would be much appreciated.

    kglad
    Community Expert
    Community Expert
    May 12, 2017

    that's not the code i suggested.  reread message 1.

    in addition,

    stop()

    should be

    this.stop()