How to Create a New File With AS3

Creating Flash-based social networks, video games and office automation applications can require that you create new files to store data in. In a social network you may need to create a new file with AS3 (ActionScript 3, Flash's programming language) to store text information that your users save from their Flash office applications. Letting your members save the graphic files they create with your network's Flash online drawing software will give your members even more reason to come back. With AS3 code you can easily save text files as well as any other file format you need.

Things You'll Need

  • Adobe Flash Professional (Versions CS3, CS4, CS5)
Show More

Instructions

    • 1

      Start the Flash program. Click "New" from the File menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box that appears. Click the "OK" button to close the dialog box.

    • 2

      Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type in the following code to create an input TextField that will be used to type text that you want to save in a new file:

      var saveText:TextField = new TextField();

      saveText.type =TextFieldType.INPUT;

    • 3

      Type the following code, starting at the next line in the Actions editor, to draw a border around the TextField, position the TextField on the flash stage at coordinates (30,100), set the width of the TextField to 200 pixels, allow carriage returns when typing in the TextField and place the TextField on the Flash stage when the Flash program is played:

      saveText.border = true;

      saveText.x =30;

      saveText.y =100;

      saveText.width = 200;

      saveText.multiline = true;

      addChild(saveText);

    • 4

      Type the following code, starting at the next line in the Actions editor, to place a button, drawn with a black pixel border of 4 pixels, filled with the color red, positioned on the Flash stage at coordinates (350, 200) and with a height of 50 pixels and a width of 20 pixels:

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(350, 200, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

    • 5

      Type the following code, starting at the next line in the Actions editor, to declare a FileReference object that will be used to access the file saving and loading methods of the FileReference class:

      var MyFile:FileReference = new FileReference();

    • 6

      Type the following code, starting at the next line in the Actions editor, to add an event listener to myButton so that when the button is clicked with the Mouse the save file dialog box appears on the stage and the file saved will save the text contents of the text typed into the TextField to a new file:

      myButton.addEventListener(MouseEvent.CLICK, saveFile_fn);

      function saveFile_fn(e:MouseEvent): void {

      MyFile.save(saveText.text);

      };

    • 7

      Copy and paste the following code into your Actions editor if you want to ensure that the save new file feature of this Flash word processing program is error free.

      var saveText:TextField = new TextField();

      saveText.type =TextFieldType.INPUT;

      saveText.border = true;

      saveText.x =30;

      saveText.y =100;

      saveText.width = 200;

      saveText.multiline = true;

      addChild(saveText);

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(350, 200, 50, 20);

      myButton.graphics.endFill();

      var MyFile:FileReference = new FileReference();

      addChild(myButton);

      myButton.addEventListener(MouseEvent.CLICK, saveFile_fn);

      function saveFile_fn(e:MouseEvent): void {

      MyFile.save(saveText.text);

      };

    • 8

      Click the "TestMovie" option in the Control menu on the main Flash menu bar. Type some text into the TextField and click the red button on the stage to save the text in the TextField to a new file. Type in the name of your new file in the "file name" text box in the Save dialog box that appears, then press the "OK" button to save the file. Open up the file with your text editor if you want to see the text you typed in the actual saved file.

Learnify Hub © www.0685.com All Rights Reserved