Search This Blog

Sunday, September 4, 2011

Saving the Input Text - with Save Button

// Creates a fileReference which you will use later to save your text
var fr:FileReference = new FileReference();

// Creates a textField, sets the x,y positions and width, adds it to the stage.
// You can skip this if you have already created a textField on your stage.
var myTextBox:TextField = new TextField();
myTextBox.type = TextFieldType.INPUT;
myTextBox.background = true;
myTextBox.x = 10;
myTextBox.y = 10;
myTextBox.width = 530;
addChild(myTextBox);
myTextBox.text = "Type your text here.";

/* Add code to create a button or draw one on stage */

// Rename "score_btn1" to whatever, I'm just assuming that's what you've called your button
score_btn1.addEventListener(MouseEvent.CLICK, saveFile);

// This is the function that will take text that you type and save it to a .txt file
function saveFile(e:MouseEvent):void {
   fr.save(myTextBox.text,"NewFileName.txt");
}

Here is an example that loads in a file and traces its contents:---- from JTD FLASh from gotoAndLearn()

var btn:Sprite = new Sprite();
btn.graphics.beginFill(0);
btn.graphics.drawRect(0,0,100,20);
btn.graphics.endFill();
addChild(btn);

var fr:FileReference = new FileReference();
fr.addEventListener(Event.COMPLETE, completeHandler);
fr.addEventListener(Event.SELECT, selectHandler);

btn.addEventListener(MouseEvent.CLICK, onBtnClicked);

function onBtnClicked(ev:MouseEvent):void {
   fr.browse(); //Use arguments to restrict file types. See Lang Ref.
}

function completeHandler(ev:Event):void {
   trace(fr.data);
}

function selectHandler(ev:Event):void {
   fr.load();
}