loading external as2 movies - actionscript-3

I need to load a swf file into a new swf movie. I need to check last frame to start a movie clip, etc. Everything works ok in the below code. I was using as3 and was loading also an external as3 swf movie. The problem started when I tried to load external as2 swf movies as I receive the message: TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::symbol_name to flash.display.MovieClip. Is there a way I can convert the following code to as2???? Is there another way? Please note I'm an absolute Flash beginner and I've tried my hardest to do this in as3 and now I don't see any alternative but to use as2!!! Many thanks!
var swfLoader:Loader = new Loader();
var swfFile:URLRequest = new URLRequest("file.swf");
var container:MovieClip= new MovieClip();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
var currentSWF:MovieClip = new MovieClip();
swfLoader.load(swfFile);
container.addChild(swfLoader);
addChild(container);
function swfLoadedHandler(e:Event):void {
currentSWF = MovieClip(swfLoader.content);
currentSWF.addEventListener(Event.ENTER_FRAME, checkLastFrame);
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
bob.play();
if (bob.currentFrame == 2) {
bob.stop();
}
}
}
}

There is a good answer to the question of loading AS2 content into AS3 here: Load AS2 SWF Into AS3 SWF and pass vars in URL. Basically, you're going to need to create a bridge loader in AS2 if you can't edit the AS2 content you're loading.
Edit: This (untested) code should do what you need it to in AS2:
import mx.utils.Delegate;
var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.onLoadComplete = Delegate.create(this, loadComplete);
var targetMC:MovieClip = createEmptyMovieClip("container",getNextHighestDepth();
mcLoader.loadClip("file.swf",targetMC);
function loadComplete(evt:Object):Void {
targetMC.onEnterFrame = Delegate.create(this,checkFrame);
}
function checkTargetFrame(evt:Object):Void {
if(targetMC._totalframes == targetMC._currentframe) {
targetMC.stop();
targetMC.onEnterFrame = null;
bob.onEnterFrame = Delegate.create(this,checkBobFrame);
bob.play();
}
}
function checkBobFrame(e:Object):Void {
if(bob._currentframe == 2) {
bob.onEnterFrame = null;
bob.stop();
}
}

Related

AS2 to AS3 conversions, loading multiple external swf's with same preloader

I'm new as a member here, but have found some very helpfull information here in the past, and cannot find a fix for my current issue. I've been trying to rewrite my flash AS2 website to AS3, and am getting roadblocked by all the major differences between these to actionscripts. I have a majority of it rewritten (successfully I think), but cannot seem to find the correct way to rewrite this AS2 code:
//AS2 ATTACH PRELOADER
function onLoadStart(target){
attachMovie("preloader anim", "preloader_mc", 500, {_x:447, _y:290});
}
function onLoadProgress(target, bytes_loaded, bytes_total){
target.stop();
target._visible = false;
preloader_mc.value = bytes_loaded/bytes_total;
}
function onLoadComplete(target){
trace("complete")
target.play();
target._visible = true;
preloader_mc.removeMovieClip();
}
function onLoadError(target, error_code){
preloader_mc.removeMovieClip();
trace(error_code);
}
//AS2 LOAD SWFS WITH ABOVE PRELOADER
var loader_mcl = new MovieClipLoader();
loader_mcl.addListener(this);
skullo_b.onRelease = function(){
startPreload("load/skullo.swf")
}
fruit_b.onRelease = function(){
startPreload("load/fruitbat.swf")
}
//...many more swfs left out to save space
function startPreload(url){
loader_mcl.loadClip(url, container_mc);
}
I know attachmovie is no longer for AS3, so from my research I've rewritten it as follows, but keep getting other errors that I'm having a loss on fixing. Basically, I have 30+ buttons, that when I click on each, it will load an external swf at the same location on the stage (container mc) and hide the previously loaded swf, and each swf will utilize the same preloader (preloader_anim). I've included the current errors I'm getting after finally clearing some others. If anyone can help me out, or point me to an online example of this I haven't been able to locate I would be very grateful. I've found some examples of loading external swfs with as3, but not multiples with the same preloader. I am also very new to as3, and haven't messed with classes yet, so all my code is on the timeline if that makes any difference.
//AS3 ATTACH PRELOADER
//ERROR 1046: Type was not found or was not a compile-time constant: preloader_mc.
//ERROR 1180: Call to a possibly undefined method preloader_mc.
var preloader_anim:preloader_mc = new preloader_mc();
preloader_anim.x = 458;
preloader_anim.y = 290;
addChild(preloader_anim);
function onLoadProgress(target, bytes_loaded, bytes_total){
target.stop();
target._visible = false;
var preloader_mc = bytes_loaded/bytes_total;
}
function onLoadComplete(target){
trace("complete")
target.play();
target._visible = true;
preloader_mc.removeMovieClip();
}
function onLoadError(target, error_code){
preloader_mc.removeMovieClip();
trace(error_code);
}
//AS3 LOAD SWFS WITH ABOVE PRELOADER
var imgLoader:Loader = new Loader();
//ERROR 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:Loader.
imgLoader.addListener(this);
skullo_b.addEventListener(MouseEvent.CLICK, skullo_bClick);
angel_b.addEventListener(MouseEvent.CLICK, angel_bClick);
function skullo_bClick(e:MouseEvent):void {
startPreload("load/skullo.swf")
}
function metal_bClick(e:MouseEvent):void {
startPreload("load/metal.swf");
}
function startPreload(url){
//ERROR 1061: Call to a possibly undefined method loadClip through a reference with static type flash.display:Loader.
imgLoader.loadClip(url, container_mc);
}
Let's go through this in order of your errors.
ERROR 1046: Type was not found or was not a compile-time constant: preloader_mc
&
ERROR 1180: Call to a possibly undefined method preloader_mc.
These errors are because the compiler can't find any class called preloader_mc
If you have an asset in your library called preloader_mc, that is not enough, you need to go it's properties and choose export for actionscript, then give it a class name (the class name can be the same as the library asset name, so: preloader_mc).
Just make sure though, that you don;t have any variable or function names that clash with your class names (this is currently your case with preloader_mc). Common practice, is to make all class names start with an Uppercase letter, and all function and vars start with a lowercase letter.
2.
ERROR 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:Loader.
In AS3, what you want is addEventListener. With the Loader class you need to listen for each event, instead of giving it a context that has pre-set methods. It takes a string event name, and a callback function. So you probably want this:
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaderComplete);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(e:ProgressEvent):void {
//this function will run whenever progress in the load is made
trace("progressHandler: bytesLoaded=" + e.bytesLoaded + " bytesTotal=" + e.bytesTotal);
}
function imgLoaderComplete(e:Event):void {
//this function will be called after the loader finishes loading
}
It's also a good idea to listen for IO_ERROR & SECURITY_ERROR events on the loader as well:
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
imgLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
ERROR 1061: Call to a possibly undefined method loadClip through a reference with static type flash.display:Loader.
There is not method called loadClip on the Loader class. What you want is the following (to start loading)
imgLoader.load(new URLRequest("yoururlhere"));
For more details on how to properly use the Loader class, read the documentation.
So, in the end, it should look more like this:
//take your preloader movie clip, and export it for actionscript with the class name "Preloader_MC"
//create vars for the pre loader and loader (don't create the objects yet though)
var preLoader:Preloader_MC;
var imgLoader:Loader;
skullo_b.addEventListener(MouseEvent.CLICK, skullo_bClick);
angel_b.addEventListener(MouseEvent.CLICK, angel_bClick);
function skullo_bClick(e:MouseEvent):void {
startPreload("load/skullo.swf")
}
function metal_bClick(e:MouseEvent):void {
startPreload("load/metal.swf");
}
function startPreload(url) {
//if the loader is currently populated, destroy it's content
if (imgLoader) {
imgLoader.unloadAndStop();
removeChild(imgLoader);
}else {
//it doesn't exist yet, so create it and add the listeners
imgLoader = new Loader();contentLoaderInfo
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaderComplete);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
imgLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
if (!preLoader) {
preLoader = new PreloaderMC();
addChild(preLoader);
}
imgLoader.load(new URLRequest(url));
addChild(imgLoader);
}
function removePreLoader():void {
removeChild(preLoader);
preLoader = null;
}
function progressHandler(e:ProgressEvent):void {
var percentLoaded:Number = e.bytesLoaded / e.bytesTotal; //number between 0 - 1
preLoader.value = percentLoaded;
}
function imgLoaderComplete(e:Event):void {
removePreLoader();
}
function ioErrorHander(e:IOErrorEvent):void {
//file not found, do something
removePreLoader();
}
function securityErrorHandler(e:SecurityErrorEvent):void {
//do something, file wasn't allowed to be loaded
removePreLoader();
}

AS3 browse and load video

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.

Go to specific frame after external swf has been loaded AS3

I have been looking for solutions around here but I can't seem to get it right.
Basically I am trying to load an external swf after clicking on a 'Next' button and it will automatically go to a specific frame eg. frame 8 instead of frame 1.
At first I've got an error of using of using MovieClip function in a Loader and such.
Here's my code
nextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_1);
var fl_Loader1:Loader;
var fl_ToLoad1:Boolean = true;
function fl_ClickToLoadUnloadSWF_1(event:MouseEvent):void
{
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
var fl_Loader1:MovieClip = event.target.content as MovieClip;
fl_Loader1.gotoAndStop(8);
}
else
{
fl_Loader1.unload();
removeChild(fl_Loader1);
fl_Loader1 = null;
}
fl_ToLoad1 = !fl_ToLoad1;
}
You can access content of loaded swf only after event. Complete was dispatched while loading swf file on to the stage.
Define event handler method to start from 8 th frame
function loaderCompleteHandler(evt:Event):void {
var loadedMovie:MovieClipp = evt.currentTarget.content as MovieClip;
loadedMovie.gotoAndStop(8);
}
Replace if block with below lines of code
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
}
Happy coding :)

How to load image to swf which is loaded into other swf? (AIR, AS3)

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().

as3 loaded swf accessing variables

I have some questions with sharing/using/accessing variables/functions between loaded swf files.
my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf.
my questions are:
1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf)
2.how can I call a function from 2.swf in 1.swf
here is the code I'm using to load the swf's:
var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();
startLoad();
function startLoad()
{
//var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("./_player/player.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
mLoader.load(mRequest);
addChild(mLoader);
//var m2Loader:Loader = new Loader();
var m2Request = new URLRequest("./_db/db.swf");
m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
m2Loader.load(m2Request);
addChild(m2Loader);
}
function loadMc(event:Event):void
{
if (! event.target)
{
return;
}
playerMc = event.target.content as MovieClip;
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}
function loadMc2(event:Event):void
{
if (! event.target)
{
return;
}
dbMc = event.target.content as MovieClip;
dbMc.x = -400;
m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}
You have to stick with application domain.
In most cases you should load another swf in another application domain, but it's not really related to your question.
From loader, you must access to applicationDomain and then getDefinition. From there, you can get classes and use them in your main swf. Yes, you can read static properties.
If you need instances you should access loader#content. It is pointing to a root of loaded SWF. Root of loaded is SWF – is the instance of main class of the loaded swf.
Create a variable with no definition such as
public var MyClass;
as you can see i didnt add
public var MyClass:Class;
then in another function write
this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class;
i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..?
also after your write the above code .. when you want to change a varaibles this usually works for me
this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1;
something like that..