Hello,
I have tried to make a kind of terminal application that does the following things (in the corresponding order):
- Dynamically makes two text boxes on the stage (output and input)
- Types the welcome message to the user in a typewriter-like effect
- A text field for input then moves to the end of the text field for output
- A cursor starts to blink in the input text field
I get 2 errors of the same code: 1120. It is because of my blinkRect function. (It adds and removes the rectangle from the stage, creating a cursor effect.) It uses the commands addChild(blink) and removeChild(blink) after the rectangle has been added to the stage. But, from the looks of the flash error, it seems as if the ActionScript engine parses this code before making the rectangle. I don’t understand this, as I have not programmed it this way.
Here’s my code (and there’s nothing on the stage, so just pasting it into a frame should exactly replicate my problem):
// IMPORT CLASSES
import fl.transitions.Tween;
import fl.transitions.easing.*;
// INITIAL VAR DECLARATIONS
var str:String = " term.0.01.3: ~ web#"; // command string
var count:Number = 0; // for counting text output
var i = 1; // should cursor be seen or not?
// GENERATE OUTPUT TEXT FIELD AND INPUT TEXT FIELD
// Start with making a universal format
var ocrFont:Font = new OCR();
var codeFormat:TextFormat = new TextFormat();
codeFormat.color = 0x00FF00;
codeFormat.size = 16;
codeFormat.font = ocrFont.fontName;
// Declare text fields
var output:TextField = new TextField();
var input:TextField = new TextField();
// Text field types
output.type = TextFieldType.DYNAMIC;
input.type = TextFieldType.INPUT;
// Size text fields
output.x = 10;
output.y = 10;
output.width = 530;
output.height = 380;
input.width = 180;
// Extra options
output.multiline = true;
output.border = true;
input.border = true;
input.multiline = false;
// Apply font styles
output.defaultTextFormat = codeFormat;
input.defaultTextFormat = codeFormat;
// Add to stage
addChild(output);
// WRITE TEXT
var sInt = setInterval(writeText, 3); // interval for outputting initial text
// function for writing output text
function writeText() {
// simulate typing
output.text = str.substring(0, count);
count++;
// when typing is done
if (count > str.length){
// stop timer
clearInterval(sInt);
timerStopped();
}
}
// function when timer stops
function timerStopped():void {
// make input text field
input.y = output.y;
input.x = 20 + output.textWidth;
addChild(input);
// switch focus
stage.focus = input;
makeRectangle();
// start blink loop
var blinkInt = setInterval(blinkRect, 500); // interval setup for blinking cursor
}
// function for making the blinking cursor
function makeRectangle():void {
var position:Rectangle = input.getCharBoundaries(input.caretIndex);
var blink:Sprite = new Sprite();
blink.graphics.beginFill(0x00FF00);
blink.graphics.drawRect(position.x+input.x, position.y+input.y, position.width, position.height);
blink.graphics.endFill();
addChild(blink);
}
// function for making caret blink
function blinkRect(){
if (i == 1){
removeChild(blink);
i = 0;
} else {
addChild(blink);
i = 1;
}
}
How can I stop flash from parsing it this way and giving errors?
All and any help is greatly appreciated.
Thanks,
Arduino910