How to create a new file with as3

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to create a new file with as3
Create Flash applications that let your members save their files online. (Jupiterimages/Comstock/Getty Images)

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.

Skill level:
Moderate

Other People Are Reading

Things you need

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

Show MoreHide

Instructions

  1. 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 dialogue box that appears. Click the "OK" button to close the dialogue box.

  2. 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 save Text:TextField = new TextField();

    saveText.type =TextFieldType.INPUT;

  3. 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;

    add Child(saveText);

  4. 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 colour red, positioned on the Flash stage at coordinates (350, 200) and with a height of 50 pixels and a width of 20 pixels:

    var my Button:MovieClip = new MovieClip();

    myButton.graphics.lineStyle(4);

    myButton.graphics.beginFill(0xFF0000);

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

    myButton.graphics.endFill();

    add Child(myButton);

  5. 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. 6

    Type the following code, starting at the next line in the Actions editor, to add an event listener to my Button so that when the button is clicked with the Mouse the save file dialogue 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. 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 save Text:TextField = new TextField();

    saveText.type =TextFieldType.INPUT;

    saveText.border = true;

    saveText.x =30;

    saveText.y =100;

    saveText.width = 200;

    saveText.multiline = true;

    add Child(saveText);

    var my Button: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();

    add Child(myButton);

    myButton.addEventListener(MouseEvent.CLICK, saveFile_fn);

    function saveFile_fn(e:MouseEvent): void {

    MyFile.save(saveText.text);
    

    };

  8. 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 dialogue 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.

Tips and warnings

  • You can save files with AS3 in many different ways besides using the save method of the AS3 FileReference class. You can also use the shared Object method to save files directly on the user's computer without a dialogue box. The shared Object method is often used to store information that needs to be used to continue an online application such at a later point in time. Saving graphics that are generated in the Flash program requires that you capture the image with Flash's draw method as discussed in the article titled "Saving Flash Graphics as Image Files at Runtime" listed in the Resources section.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.