Search code examples
flashactionscript-2textinputplaintext

How to make actionscript 2 interpret code as plain text?


I'm trying to make a flash-based activity where the end-user will type in some Java as a writing exercise, and when it's done, they will hit enter and flash will check to see if they typed correctly.

The problem comes where I am writing the actionscript. When I test the movie, flash throws out lots of errors because it thinks the Java I typed in the input text box is just poorly formatted code. The code I am using has worked in the past for simple things like typing a name, or a number, but with the code-type input, it breaks. I know there are tags in HTML to display code as text, but I can't find anything about how to do this within actionscript 2. Here's what I have:

keyListener = new Object();
keyListener.onKeyDown = function() {
if(Key.getCode() == Key.ENTER){
if(allthecode.text == "
    // Import the required API classes.
    import java.util.Scanner;
 
    public class ShowByte 
    {
    public static void main(String[] args) 
    {
    // Create the scanner.
    Scanner GetByte = new Scanner(System.in);

    // Obtain a byte value.
    System.out.print("Type any number: ");
    byte MyByte = GetByte.nextByte();

    // Display the value on screen.
    System.out.println("The value of MyByte is: " + MyByte);
    }
    }
    ") {gotoAndPlay(150);
        }
    }
};
Key.addListener(keyListener);

EDIT:Lee figured out it was that the code was on multiple lines. As soon as I placed everything within ' ' marks and scrunched everything up to a single line, it worked!


Solution

  • i think youre getting errors because of the way you have formatted your string - you have " surrounding string but also within it - flash gets confused. Use either " or ' to surround string and then other type within string. eg:

    if (allthecode.text == '
        // Import the required API classes.
    import java.util.Scanner;
    
    public class ShowByte 
    {
    public static void main(String[] args) 
    {
    // Create the scanner.
    Scanner GetByte = new Scanner(System.in);
    
    // Obtain a byte value.
    System.out.print("Type any number: ");
    byte MyByte = GetByte.nextByte();
    
    // Display the value on screen.
    System.out.println("The value of MyByte is: " + MyByte);
    }
    }
    ') {gotoAndPlay(150);
    

    Or post the error message you get and prove me wrong ;)