Help with function to read external file

This is a discussion on "Help with function to read external file" within the Flash & Multimedia Forum section. This forum, and the thread "Help with function to read external file are both part of the Design Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Design Your Website > Flash & Multimedia Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jul 6th, 2007, 07:37
New Member
Join Date: Jul 2007
Location: Bulgaria
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Help with function to read external file

I have a code segment which randomly picks a quote from an external file (shown below). But I want to make it so that I can call this function from anywhere and return the value it obtains.

Here is the code segment:

Code: Select all
ranQuote = new LoadVars();
ranQuote.onLoad = function(success){
var ran:String;
if (success) { 
   RanNum = Math.ceil(Math.random()*11); 
   ran = this["quote"+RanNum]; 
}
else { 
ran = "The text failed to load due to an error";
}
}
ranQuote.load("Quotes.txt");
So basically, it takes a random quote string from a text file called Quotes.txt.

What I want to do is something like this (from the calling area):

Code: Select all
abc.text = ranQuote.load("Quotes.txt");
So that it can randomly assign a quote to abc.text (text string).
I will use this function from many places, and was planning to define the function in frame 1.

But I dont know actionscript well enough to make the first code segment a fully encapsulated function, that can achieve this. Any advice welcome.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jul 6th, 2007, 14:22
Multimedia Specialist
Join Date: Apr 2007
Location: Arizona
Age: 25
Posts: 666
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with function to read external file

So what if we turned this into a fuction like this:

Code: Select all
 
function RandomQuote() {
ranQuote = new LoadVars();
ranQuote.onLoad = function(success){
var ran:String;
if (success) { 
   RanNum = Math.ceil(Math.random()*11); 
   ran = this["quote"+RanNum]; 
}
else { 
ran = "The text failed to load due to an error";
}
}
ranQuote.load("Quotes.txt");
}
Then, say - on release of a button run the fuction and in a text field display the "run" value.

Code: Select all
 
on (release) 
{
RandomQuote(); 
abc.text = run;
{
This would runt he random quote fuction on release of the button and populate the text field in theory. (I didn't test it so there might be some bugs, I just wrote this off the top of my grog filled, coffee lacking head....) Hope this helps or gets you going in the right direction.

Time for some Coffee.

Cheers,

Scott
Last Blog Entry: Yay!? (Oct 8th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jul 6th, 2007, 14:33
New Member
Join Date: Jul 2007
Location: Bulgaria
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with function to read external file

Thanks for the advice. But wouldnt the 'ran' variable only have local scope in the function?

What I was trying to do was return the quote from the function - but couldnt get that to work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jul 6th, 2007, 15:14
Multimedia Specialist
Join Date: Apr 2007
Location: Arizona
Age: 25
Posts: 666
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with function to read external file

hmm, yeah - you could use a global variable and have it set the _root.variablename = run; and then use it that way I would think.

I can take a look at this a little more over the weekend I think...really busy today here at the good ole' job.

Does this at least get your gears turning?

Cheers,

Scott
Last Blog Entry: Yay!? (Oct 8th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jul 6th, 2007, 15:18
New Member
Join Date: Jul 2007
Location: Bulgaria
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with function to read external file

I just think that because of the onload event, the assignment is only made when its finished loading the quotes.txt file. So at the point you perform the abc.text assignment, its not necessarily set the global variable that you're refering to yet.

I think this is why I was having problems with it as a function - due to the nature of this onload, and only getting the value set asynchronously.

Thanks for your thoughts though.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
external file, function

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
file read alexgeek PHP Forum 18 Aug 19th, 2007 19:49
Load External .JS File Before Anything Else tox0tes JavaScript Forum 1 Jul 10th, 2007 14:03
Is it possible to use an external text file for a variable AdRock JavaScript Forum 2 Sep 15th, 2006 17:25
Read only from File..... courtjester JavaScript Forum 7 Nov 23rd, 2003 16:21


All times are GMT. The time now is 03:34.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42