-
[RESOLVED] Using Load vars
What I want to do:
I have a flash quiz (projector file) that students can take home and practice.
I want students to practice at home but take the quiz at school. In the past I used flash 8 and placed a watermark on the printed answer page based on a file found on the school server. If flash loaded the file a watermark (mc) appeared lightly behind the answers saying 'printed at school'. If the file couldn't load the watermark would say 'printed at home'.
I'm trying to learn AS3 using CS4 and am trying to use the same concept.
Here is the old flash 8 code that worked great.
Code:
var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean) {
if (success) {
tryagain = 0;
school._alpha = 15;
home._alpha = 0;
} else {
tryagain = ++tryagain;
if (tryagain<=4) {
my_lv.load("file:///G:/Tests/Spanish/Spanish/ok.txt");
}
if (tryagain>4) {
school._alpha = 0;
home._alpha = 15;
}
}
};
my_lv.load("file:///G:/Tests/Spanish/Spanish/ok.txt");
Here is what I have come up with for CS4. I'd appreciate any advice. I'm a hobbyist programmer.
Code:
school.alpha=0;
home.alpha=0;
var watermark="home";
var myLoad:Loader=new Loader();
var url:URLRequest=new URLRequest("file:///G:/Tests/Spanish/Spanish/ok.txt");
myLoad.load(url);
myLoad.addEventListener(Event.COMPLETE,myFile);
function myFile(evt:Event):void {
watermark="school";
}
if (watermark=="home") {
school.alpha=0;
home.alpha=.15;
} else if (watermark=="school") {
school.alpha=.15;
home.alpha=0;
} else {
school.alpha=0;
home.alpha=.15;
}
Is it a bad thing if I leave the URLRequest unable to complete because students don't have access to the school server from home?
-
You want to be listening to the loader's "contentLoaderInfo" object (the Loader is actually just a wrapper that handles the download) - you also should catch a failed download - here's how I'd set it up:
PHP Code:
school.alpha = .15;
school.visible = false;
home.alpha = .15;
var myLoad:Loader = new Loader();
var url:URLRequest = new URLRequest('file:///G:/Tests/Spanish/Spanish/ok.txt');
myLoad.contentLoaderInfo.addEventListener(Event.COMPLETE, downloadSuccess);
myLoad.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, downloadFail)
myLoad.load(url);
function downloadSuccess(e:Event):void{
home.visible = false;
school.visible = true;
}
function downloadFail(e:IOErrorEvent):void{
school.visible = false;
}
Please use [php] or [code] tags, and mark your threads resolved 8)
-
Originally Posted by neznein9
You want to be listening to the loader's "contentLoaderInfo" object (the Loader is actually just a wrapper that handles the download) - you also should catch a failed download - here's how I'd set it up:
PHP Code:
school.alpha = .15;
school.visible = false;
home.alpha = .15;
var myLoad:Loader = new Loader();
var url:URLRequest = new URLRequest('file:///G:/Tests/Spanish/Spanish/ok.txt');
myLoad.contentLoaderInfo.addEventListener(Event.COMPLETE, downloadSuccess);
myLoad.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, downloadFail)
myLoad.load(url);
function downloadSuccess(e:Event):void{
home.visible = false;
school.visible = true;
}
function downloadFail(e:IOErrorEvent):void{
school.visible = false;
}
Thanks for the help. I tried this and the error message is gone but the code doesn't seem to work. The function downloadSuccess never triggers. I assume the syntax to my file on the server in AS3 might be different than the syntax to the file in flash 8? I can't see any other reason why your code wouldn't work. The flash 8 files still work fine so I know the path to the file is correct, at least in flash 8.
-
Senior Member
Perhaps the problem is this. When loading a text file you need to use a URLLoader instead of a Loader like so:
Code:
school.alpha = .15;
school.visible = false;
home.alpha = .15;
var myLoad:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("list.txt");
myLoad.addEventListener(Event.COMPLETE, downloadSuccess);
myLoad.addEventListener(IOErrorEvent.IO_ERROR, downloadFail)
myLoad.load(url);
function downloadSuccess(e:Event):void{
trace("Done");
home.visible = false;
school.visible = true;
}
function downloadFail(e:IOErrorEvent):void{
school.visible = false;
}
Hope this helps.
-
Originally Posted by jweeks123
Perhaps the problem is this. When loading a text file you need to use a URLLoader instead of a Loader like so:
Code:
school.alpha = .15;
school.visible = false;
home.alpha = .15;
var myLoad:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("list.txt");
myLoad.addEventListener(Event.COMPLETE, downloadSuccess);
myLoad.addEventListener(IOErrorEvent.IO_ERROR, downloadFail)
myLoad.load(url);
function downloadSuccess(e:Event):void{
trace("Done");
home.visible = false;
school.visible = true;
}
function downloadFail(e:IOErrorEvent):void{
school.visible = false;
}
Hope this helps.
This worked like a charm. Thanks for your help!!! And thanks to neznein9 as well.
-
Senior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|