Maulid Nabi Mukhammad

Laman

Quote

Quote

Diberdayakan oleh Blogger.

Popular Posts

Labels List

Quote

banner ads banner ads banner adsbanner ads

Search

Rabu, 02 Januari 2013

Read variables from text file with AS3

As you all might have noticed, lately I have converted to actionscript 3.0 instead of the old actionscripting, and yes as a lot of people say, its quite tough to convert, so I have to start from rock bottom. So here will be a lot of experimenting tutorials for flash actionscript 3.0. In this Flash tutorial you will see how to load variables in to flash from a text file.
As you might know a variable is a container, we define a container to keep simple information, it can be text, numbers, true/false etc. In this case we just want to load in simple text variables from a text file we call content.txt (remember to keep the content file in the same directory as the flash file).
First we will look at how the text file is build, it really simple you write everything in one line, separating variables with a & and starting with the variable name then = to its value like this.
  1. var_1=first variable&var_2=second variable  
If you now want to extract var_1 the result will be "first variable"
Now for the flash file, here is first what we want to set up.
This small example will only contain two dynamic text fields, so go open a new flash document, and drag out two text objects, go to properties and change their state from static to dynamic and give the text boxes an instance name (I called mine content_1 and content_2) Look at the properties in the image below.
That was all we needed to do with the flash interface, of cause you can do a lot of visual stuff to make it look more interesting, but for this tutorial I will keep it simple and stick to the nerdy stuff.
As you know we are working with actionscript 3.0 so coding is done on the stage, so click somewhere on the stage, to deselect all, then go to the actionscript panel and type in the following code, lines with // are my comments on how the code works and what it does, you can delete them if you want.
  1. var loader:URLLoader = new URLLoader();  
  2.   
  3. //telling the loader that we are dealing with variables here.  
  4. loader.dataFormat = URLLoaderDataFormat.VARIABLES;  
  5.   
  6. //This is an eventlistener, these are used all the time in AS3  
  7. //learn to use them, this basically tells flash to listen to a specific event  
  8. //and then call a specific function.  
  9. //in Our case we listen for the even called COMPLETE which means it will active  
  10. //a function called "loading" when our flash movie has completed  
  11. loader.addEventListener(Event.COMPLETE, loading);  
  12.   
  13. //Here we tell our loading which file to extract from.  
  14. loader.load(new URLRequest("content.txt"));  
  15.   
  16. //This is the function that will happen when the eventlistener activates.  
  17. //basiclly it says that our text fields called content_1 and _2's text property  
  18. //should be equal to loader.data.var_1 and var_2 (as you might remember from the explanation above).  
  19. function loading (event:Event):void {  
  20.     content_1.text = loader.data.var_1  
  21.     content_2.text = loader.data.var_2  
sumber : http://blog.0tutor.com/post.aspx?id=110
Unknown flashKeren