Skip to main content
Known Participant
April 17, 2021
Question

Converting Flash Actionscript to Animate Javascript

  • April 17, 2021
  • 2 replies
  • 1079 views

Hi, I'm starting a somewhat daunting project of converting a Flash file to HTML5 Canvas.  I'm taking it line by line, but I've run into my first issue I can't figure out.

 

It's pretty simple.  On my main timeline, I set a variable called startX:

    var startX = 10;

Then I bring in a movieclip from the library:

    var arrow1 = new lib.arrow();
    this.addChild(arrow1);

Works just fine.  arrow1 is there, and the code inside it runs.  I just can't reference startX:

    this.parent.tracer.text = this.parent.startX;

The tracer is a text box, and that line works fine with something like this.parent.tracer.text = "arrow clip"; so that connection is fine.

 

Why can't I reference a variable I set in the main timeline from within the movieclip?

    This topic has been closed for replies.

    2 replies

    Long ConAuthor
    Known Participant
    April 17, 2021

    I'm working on replacing the old LoadVars that I used to load a text file.  I'm trying XMLHttpRequest, but the onreadystatechange function never fires.

     

    this.loadDoc = function() {
      this.tracer.text = "loadDoc";
    
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        this.tracer.text = "onreadystatechange";
        if (this.readyState == 4 && this.status == 200) {
          this.tracer.text = this.responseText;
        }
      };
      xhttp.open("GET", "mymaze.txt", true);
      xhttp.send();
    }
    this.loadDoc();

    The tracer text reads "loadDoc, but never changes to "onreadystatechange".

    mymaze.txt is in the same local folder as the animate file.

    Colin Holgate
    Inspiring
    April 17, 2021

    'this' in an HTML5 function is the function you are in at the time, not the level at which the code is placed. You can get around that by using bind, or you can set a variable that all of the functions can see. Like:

    var tracer = this.tracer;
    this.loadDoc = function() {
      tracer.text "loadDoc";
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        tracer.text = "onreadystatechange";
        if (this.readyState == 4 && this.status == 200) {
          tracer.text = this.responseText;
        }
      };
      xhttp.open("GET", "mymaze.txt", true);
      xhttp.send();
    }
    this.loadDoc();

    I don't know for sure if that's why it's failing, but if the function works, the text should become visible.

    Long ConAuthor
    Known Participant
    April 18, 2021

    Thank you again, that worked, I don't understand yet why my first problem was because I was using 'var', and my second problem was solved by using 'var'.

     

    So 'var' is available in the same movieclip or on the root, but is not available from within other movieclips?

    Colin Holgate
    Inspiring
    April 17, 2021

    startX doesn't really belong to anything. Try this instead:

    this.startX = 10;

    Long ConAuthor
    Known Participant
    April 17, 2021

    Thank you, that did it.  I'll get my head out of Actionscript one change at a time!