I have a problem about loading a SWF file(the SWF with a Main.as)
Here is my code:
bt1.addEventListener(MouseEvent.CLICK, f1);
var myloader:Loader = new Loader();
var myURL:URLRequest = new URLRequest("test.swf");
myloader.load(myURL);
function f1(Event:MouseEvent):void
{
addChild(myloader);
myloader.x=100;
myloader.y=200;
myloader.width =200;
myloader.height =60;
}
When I clicked load button, I can hear the sounds(test.swf), but I cant see anything.
Did you try with stage.addChild(myloader);?
Related
I want to unload a previously loaded swf file(welcome.swf) and load another swf file(topics.swf) when a user clicks a button that is located inside the first loaded swf file(i.e the button, is nested inside the welcome swf file that is to be unloaded).I have tried all manner of codes but none seems to be working and Im at my wits end.Kindly someone assist.Thanks in advance.Below is the code in my main file and the external swf that is to be unloaded.
Main file
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
box.addChild(loader);
External swf file
var loader = new Loader();`
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loader.unloadAndStop();
loader=null;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
loader.x = 0;
loader.y = 0;
addChild(loader);
}
I think you should load topics.swf at Main Loader.
So you should access Main Loader from welcome.swf.
Doesn't this code work?
External swf file
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
var target: Object = this;
// Search the Main Loader from child swf.
while (target.parent){
target = target.parent;
if (target is Loader){
var loader:Loader = target as Loader;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
break;
}
}
}
I would probably move all the loading/unloading responsibility in a main file, with inner swf just letting the main know about some events inside through loaderInfo.sharedEvents just like this:
main file:
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
function topicsRequestedHandler(event:Event):void {
loader.contentLoaderInfo.sharedEvents.removeEventListener(
"loadTopics", topicsRequestedHandler);
loader.unloadAndStop();
//load "Topics.swf" here
}
loader.contentLoaderInfo.sharedEvents.addEventListener(
"loadTopics", topicsRequestedHandler);
box.addChild(loader);
and in external file:
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loaderInfo.sharedEvents.dispatchEvent(new Event("loadTopics"));
}
(not tested but should work)
So using this scheme for all the loaded files, you will keep loading/unloading management in one place.
I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:
private function browseVideo():void {
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, videoFileSelected);
var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");
fileReference.browse([videoTypeFilter]);
}
private function videoFileSelected(e:Event):void {
fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
fileReference.load();
}
function onVideoFileLoaded(e:Event):void {
var fileReferenceTarget:FileReference = e.target as FileReference;
var data:ByteArray = fileReferenceTarget["data"];
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
var videoLoader:Loader = new Loader();
videoLoader.loadBytes(data);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);
trace("video file loaded");
}
function onVideoFileLoadError(e:Event):void {
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
trace("video file load failed");
}
function onVideoLoaderComplete(e:Event):void {
var loadedContent:DisplayObject = e.target.content;
var loader:Loader = e.target.loader as Loader;
scope.addChild(loader);
}
To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.
Take a look on this example :
function browseVideo():void {
var file:File = new File();
file.addEventListener(Event.SELECT, videoFileSelected);
file.browse([videoTypeFilter]);
}
function videoFileSelected(e:Event):void {
playVideo(e.currentTarget.nativePath);
}
function playVideo(video_path:String){
// using a Video + NetStream object
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
var video:Video = new Video();
video.attachNetStream(ns);
addChild(video);
ns.play(video_path);
// using an FLVPlayback component inserted on the Stage
flvplayback.load(video_path);
flvplayback.play();
}
For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).
Hope that can help.
I have an application in AIR, which is loading swf files and makes previews of them. When those swfs loads other content (eg. jpg) then i have an error, that url not found - i am convinced that is about jpg file. This problem shows only when i load swf by filereference using loadBytes(). When i load swf by Loader using load() - problem doesn`t exist. How can i fix this issue? Sorry for my english. I attach code fragment with loading swfs.
private static function loadFile(loadType:String, swfNum:Number = 0):void {
lc = new LoaderContext();
lc.allowCodeImport = true;
lc.allowLoadBytesCodeExecution = true;
currentSwfNum = swfNum;
var fRef:FileReference = DataBase.swfsList[currentSwfNum];
fRef.addEventListener(Event.COMPLETE, onFileReferenceLoad);
fRef.load();
}
private static function onFileReferenceLoad(e:Event):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ifComplete);
loader.loadBytes(e.target.data, lc);
}
private static function ifComplete(e:Event):void {
var mc:MovieClip = LoaderInfo(e.target).content as MovieClip;
mc.addEventListener("SHOOT", SwfSingleCapture.captureSingleShoot);
}
DataBase.swfList - it is an array of FileReference objects, which are selected by FileReferenceList and browse().
I am trying to create a ComboBox that loads and unloads an external SWF onto stage using ActionScript 3.0 using Flash CS5.
Currently there are 2 list items in the combo box: Home and About.
Upon selecting Home or About option from ComboBox it displays both Home and About SWF at once when selected.
I only want 1 SWF to be displayed only when selected, not all.
menuList.addItem({label:"Choose"});
menuList.addItem({label:"Home",path:"home_load.swf"});
menuList.addItem({label:"About",path:"about.swf"});
menuList.addEventListener(Event.CHANGE, Home);
menuList.addEventListener(Event.CHANGE, About);
var loader:Loader = new Loader();
loader.unloadAndStop();
function Home(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
var loader:Loader = new Loader();
//loader.unloadAndStop();
loader.load(new URLRequest("home_load.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
function About(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
//loader.unloadAndStop();
var loader:Loader = new Loader();
loader.load(new URLRequest("about.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
You may have to do addChild after the swf file has completed loading.
//These listeners detect when the file has finished loading, and if the
//correct file is loaded.
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
//The load method then loads the SWF file into the loader object.
swfLoader.load(my_url);
//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}
using AS3 and Flash CS5
I have a messageboard Adobe Air desktop App with a URLLoader making a URLRequest for a ".txt" file every 2 minutes. or however long I set the time to look. The App loads the txt file into a dynamic text field.
var loader:URLLoader = new URLLoader(new URLRequest("https://my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void {
var loadedText:URLLoader = URLLoader(event.target);
myText_txt.htmlText = loadedText.data;
}
Is it possible to have the URLRequest load the file only if its new? not every time?
then when the "new" file has been loaded fire
mainWindow.notifyUser(NotificationType.CRITICAL)
so the user knows there is a new message?
thanks in advance!!
var loader:URLLoader = new URLLoader(new URLRequest("https://my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(event:Event):void {
var loadedText:URLLoader = URLLoader(event.target);
if(myText_txt.htmlText!=loadedText.data){
myText_txt.htmlText = loadedText.data;
mainWindow.notifyUser(NotificationType.CRITICAL)
}else {
//do nothing
}
}
Since the application will still be running you should just compare the old text file to the new one to see if there have been any changes made.