ADOBE/ ActionScript

ios swf 로드 관련 Flash Professional Help / Load external SWF into another SWF 관련

AlrepondTech 2020. 9. 22. 03:59
반응형

 

 

=================================

=================================

=================================

 

 

 

출처: http://helpx.adobe.com/flash/kb/load-external-swf-swf.html

What's covered

From within a Flash SWF file, you can load other external SWF files as assets. There are several reasons to load external SWF files:

  • Because a project can be broken up into multiple movies, the individual SWF files are smaller in size. Smaller files load faster, and manage memory more efficiently.  
  • It lets you load only the content you need based on what the user is doing in your movie.
  • Multiple SWF files can be played in succession without making the browser load another HTML page. The pages don't have the pause or refresh associated with reloading.
  • It gives you greater flexibility about the organization of your project assets.
  • A complex user interface can be created that does not rely on a single SWF file. Many SWF files can be combined to create the interface. Combining SWF files simplifies editing, because an individual, smaller SWF file can be revised without affecting the other parts of the interface.
  • Multiple authors can collaborate on the same project by working on separate FLA files.

Placing content in a separate SWF file is similar to placing content in its own movie clip symbol. The difference is that the content exists outside the main SWF file. You can also load image files, such as JPG, PNG, GIF.

To load an external SWF file from within another SWF file, use one of the following:

  • The ActionScript 3.0 Loader class
  • The ActionScript 2.0 loadMovie command
  • The ActionScript 2.0 MovieClipLoader class

To the top

Using the ActionScript 3.0 Loader class

The Loader class in ActionScript 3.0 is a subclass of DisplayObject that you use to load and display external content. To load the SWF file, you use the load method of the class. The load method has one required parameter, an URLRequest instance containing the URL address of the content to load.

The following code example creates a Loader instance and loads an external SWF file named "myExternalMovie.swf."

var myLoader:Loader = new Loader();                     // create a new instance of the Loader class
var url:URLRequest = new URLRequest("ExternalSWF.swf"); // in this case both SWFs are in the same folder
myLoader.load(url);                                     // load the SWF file
addChild(myLoader);                                     // add that instance to the display list, adding it to the Stage at 0,0


// (optional)
myLoader.x = 10;                                        // move the loaded SWF 10 pixels to the right (from the left edge)  
myLoader.y = 175;                                       // move the loaded SWF 175 pixels down from the top


// (optional) load a second external SWF file
var my2ndLoader:Loader = new Loader();
var url2:URLRequest = new URLRequest("ExternalSWF2.swf");
my2ndLoader.load(url2);
addChild(my2ndLoader);                                  // optionally, you could put the 2nd SWF beneath
                                                        // the 1st by using addChildAt(my2ndLoader, 1);
                                                        // displacing the 1st SWF from position 1 to 2 in the display list


// (optional) scaling of the 2nd SWF file
my2ndLoader.scaleX = 2;                                 // scale the SWF horizontally by 200%
my2ndLoader.scaleY = 2;                                 // scale the SWF vertically by 200%

The URL of the SWF file being loaded can be relative or absolute. See Relative Paths below for more information regarding how Flash Player handles URLs. For more information about the Loader class, see Loader in the Platform ActionScript Language Reference.

References to root, when available, always reflect the top-most display object in the portion of the display list's tree structure that SWF file represents. (For images, root references the Bitmap object.)

Note: In ActionScript 3.0, there is no equivalent to the ActionScript 2.0 _lockroot or _levelproperty. See Basics of display programming in the ActionScript 3.0 Developer's Guide.

To the top

Using the ActionScript 2.0 loadMovie command

The loadMovie command loads an external SWF file or image into a MovieClip or another level of the parent movie in ActionScript 2.0.

The loadMovie command has two different forms:

  • MovieClip.loadMovie method: The MovieClip method is used for loading external content into a specific movie clip instance.
  • Global loadMovie function: The global loadMovie function can be used to load content into movies or levels. The global version also has two variations, loadMovie andloadMovieNum. The first variation loads content into movies or levels and the second (loadMovieNum) loads specifically into levels.

MovieClip.loadMovie

When loading external content into movie clip instances, Adobe recommends that you use the MovieClip method version of loadMovie. This version is called directly from the movie clip that you want to load the content into and is passed the URL of the content.

myMovieClipInstance.loadMovie("myExternalMovie.swf");  // here only the filename is given, indicating the SWF file
                                                       // is in the same folder as the parent SWF.

The URL of the content being loaded can be relative or absolute. See Relative Paths below for more information regarding how the Flash player handles URLs.

When loaded, the content is displayed within the container movie clip. The location as well as other basic properties of the container movie clip are retained. However, any custom properties or functions defined within the container movie clip are no longer present. The new content replaces all previous content (including code and event handlers like onRelease). Therefore, any attempts at using a onLoad event handler for the movie clip can't work. In this case, use the MovieClipLoader class instead (see below). For more information onMovieClip.loadMovie, see MovieClip.loadMovie in the ActionScript 2.0 Language Reference.

Global loadMovie and loadMovieNum

The loadMovie command also exists as a global function. This function has two required parameters, the URL of the external content and the target in which the content is loaded. The target parameter can be either a string or a reference. The following lines are equivalent to loading "myExternalMovie.swf" into the movie clip instance called myContainer:

loadMovie("myExternalMovie.swf", myContainer);    // the target myContainer is an object reference
loadMovie("myExternalMovie.swf", "myContainer");  // the target "myContainer" is a string

loadMovie can also load content into different levels of the Flash player. Levels in Flash player are like player layers. Multiple movies can be played in the same instance of Flash player without being nested inside one another. Each level represents a unique root where movies can play independently of movies within other levels (using _lockroot is unnecessary).

You can reference levels in ActionScript using _level followed by a number representing the level number. The first movie loaded into Flash Player is on _level0. Additional levels can be added on top of that level. The following call to loadMovie loads "myExternalMovie.swf" into level 1 on top of the current movie playing in the player.

loadMovie("myExternalMovie.swf", "_level1");

A variation of the global loadMovie function is loadMovieNum. This method is just likeloadMovie except that it only targets levels and it targets them by number, not by name. To load an external SWF file into level 1 (_level1) for example, use the following:

loadMovieNum("myExternalMovie.swf", 1);

When loading into levels, Adobe recommends that you use loadMovieNum over loadMovie. For more information, see global loadMovie in the ActionScript 2.0 Language Reference.

Use _lockroot to prevent _root conflicts

When loading an external movie into another movie, the _root reference of the loaded movie clip changes from its main timeline to the timeline of the movie that loaded it. In other words,  _root always references to the top-most timeline in the hierarchy. If you don't want _root to reference the top-most timeline, set the _lockroot property of the main timeline of the loaded movie clip to true. This property tells all children of that timeline that when they reference _root, to reference that timeline.

this._lockroot = true;     // add this code in the main timeline of the SWF file that will be loaded into another SWF

Note: The _lockroot property is only available when publishing to Flash Player 7 or later.

To the top

 Using the ActionScript 2.0 MovieClipLoader class

The MovieClipLoader class in ActionScript 2.0 is designed to make the process of loading external content into MovieClip instances easier. As mentioned earlier, variables and functions defined in movie clips are removed when new content is loaded into those movie clips. Callbacks like onLoad aren't possible. However, the MovieClipLoader circumvents this restriction by working as a surrogate to such events. You create separate MovieClipLoaderinstances to manage the loading of content into another movie clip. Therefore, clearing of variables or functions within that movie clip doesn't occur.

When loading content into a movie clip through the MovieClipLoader class, first make a new instance of the class. Then use loadClip to load content into a target movie clip. In the following example, the new content is being loaded into the movie clip myContainer.

var myLoader:MovieClipLoader = new MovieClipLoader();
myLoader.loadClip("myExternalMovie.swf", myContainer);

If you want to know that the content is loaded, use an onLoadInit event handler with yourMovieClipLoader instance.

var myLoader:MovieClipLoader = new MovieClipLoader();
myLoader.addListener(this);
myLoader.loadClip("myExternalMovie.swf", myContainer);


function onLoadInit(mc:MovieClip)
    {
        trace("content has been loaded into "+mc);
    }

When you want to have more control over the information regarding the loading of content into a movie clip, use the MovieClipLoader class instead of MovieClip.loadMovie. (For example, use this event handler when you want to be able to check for loading progress.) For more information on the MovieClipLoader class, see MovieClipLoader in the ActionScript 2.0 Language Reference.

Note: MovieClipLoader class is only available when publishing to Flash Player 7 or later.

To the top

 Use relative paths to load content

Using relative paths with Loader and loadMovie can be confusing. Because the timeline of any SWF file or movie clip can perform a loadMovie action, ask "what timeline is the movie being loaded relative to"? Is it relative to the main timeline at _level0? Or is it relative to the timeline that performed the movie-loading action? The answer is simple: Loaded movies are always relative to the timeline that loaded them. See Relative URLs not referenced correctly | Flash (tn_04157) for a discussion of relative paths, which is relevant to loading external SWF files as well.

To the top

Frame rate considerations

In most cases, a loaded movie inherits the parent movie's frame rate. For example, a SWF file whose frame rate is 12 fps plays back at 24 fps when loaded into a movie whose frame rate is 24 fps. The only exception is if the movie being loaded contains a sound on the timeline whose sync is set to "stream". Only then does the main movie then inherit the frame rate of the loaded movie to assure the correct playback of that sound.

Note: ActionScript 3.0 allows you to change the frame rate dynamically using the Stage.frameRate property.

To the top

Keywords: Flash Player, load movie; ActionScript; scripting; levels; tell target; variables; target; instance; SWF; loadMovie; tn_14190

 

 

 

=================================

=================================

=================================

 

 

 

 

출처: http://blogs.adobe.com/airodynamics/2013/03/08/external-hosting-of-secondary-swfs-for-air-apps-on-ios/

 

External hosting of secondary SWFs for AIR apps on iOS

 

Starting with AIR 3.7, application developers will be able to host their secondary SWFs on an external server and load them on demand as per their application logic.

Till AIR 3.5, loading of only assets such as images, videos and SWFs without actionscript code, commonly referred as Actionscript Byte Code(ABC Code), from external server was supported. The workflow for the application developer for loading such assets from server resembles the diagram below :

 

 

 

 

With AIR 3.6, the feature for loading of locally packaged secondary SWFs containing ABC code was introduced. The detailed description about this feature and its usage can be found at this blog – “Packaging and loading of multiple SWFs for AIR apps on iOS“. The workflow for the application developer using this feature is described in the diagram below:

 

 

 

 

 

With AIR 3.7, the feature for external hosting of secondary SWFs has been introduced. The workflow for the application developers who want to use this feature would change slightly from AIR 3.6 and is shown below :

 

 

 

 

The developer needs to provide the url of the secondary stripped SWF in the URLRequest. The stripped SWF is obtained after the ADT packaging as described later in the blog post. A sample URL request for using this feature is :

private var externalSwfUrl:String= "http://www.xyz.com/ExternalSwf.swf";
private var urlRequest:URLRequest = new URLRequest(externalSwfUrl);

To use this feature, developer needs to specify a text file containing details of the SWF files to be stripped & externally hosted. This text file contains line separated paths of SWFs, which are to be externally hosted. A sample text file describing 3 SWFs to be externally hosted  would look like :

assets/Level1.swf
assets/Level2.swf
assets/Level3/asset/Level3.swf

The name of the text file needs to be mentioned in the <externalSwfs> tag within the <iPhone> tag in the application descriptor as shown below :

<iPhone>
       .
       .
       <externalSwfs>assets/SampleSWFInfoFile.txt</externalSwfs>
       .
       .
</iPhone>

During command line packaging using AIR Developer Tool(ADT), the developer needs to specify the text file  just like an asset along with the set of SWF’s mentioned in the text file. A sample ADT  command for using this feature is:

~/bin/adt -package -target ipa-app-store -provisioning-profile <Provisioning Profile> -storetype pkcs12 -keystore <Certificate> -storepass <Password> ResultantIPA.ipa SampleMainSwf-app.xml SampleMainSwf.swf assets/SampleSWFInfoFile.txt assets/Level1.swf assets/Level2.swf assets/Level3/asset/Level3.swf

Or another variation of the above command is where all the SWFs to be externally hosted and the text file are present in the assets folder is as follows :

~/bin/adt -package -target ipa-app-store -provisioning-profile <Provisioning Profile> -storetype pkcs12 -keystore <Certificate> -storepass <Password> ResultantIPA.ipa SampleMainSwf-app.xml SampleMainSwf.swf assets

During IPA packaging,  ADT extracts the ABC code from all child SWFs, adds it to the final executable and generates stripped SWFs in the “externalStrippedSwfs” folder created in the current working directory. The directory structure within the “externalStrippedSwfs” folder remains the same as specified within the text file. The generated stripped SWF’s should then be externally hosted on a web server of developer’s choice.

NOTE: The “externalStrippedSwfs” folder is not generated in the currently available release of Flash Builder 4.7/ Flash CS6. To enable generation of Stripped SWFs for external hosting, command line ADT packaging should be used.

A sample actionscript code which loads a secondary SWF  from an external server is described below :

package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class SampleMainSwf extends Sprite
	{	
		private var externalLoader:Loader;
		private var url:String= "http://www.xyz.com/Level1.swf";
		private var urlRequest:URLRequest = new URLRequest(url);
		private var ldrContext:LoaderContext;

		public function SampleMainSwf()
		{	
			externalLoader = new Loader();
			externalLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			externalLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			ldrContext=new LoaderContext(false,ApplicationDomain.currentDomain,null);

			try
			{
				externalLoader.load(urlRequest, ldrContext);	
			}
			catch(e:Error)
			{
				trace("Error ID : "+e.errorID+"\nError Message : "+e.message);
			}
		}

		private function completeHandler(e:Event):void
		{	
			addChild(externalLoader);	
		}

		private function errorHandler(e:IOErrorEvent):void
		{
			trace("In IO ErrorEvent Handler");
		}

	}
}

POINTS TO NOTE :

1. Conflicting symbols (classes and global functions) in all the packaged SWFs should be avoided, they might result into undesired behaviour.

2. The stripped SWFs generated using this feature are different from pure asset SWFs. It must be noted that while pure asset SWFs are valid SWFs, the stripped SWFs are invalid SWFs.

3. The SWF version of the ROOT.SWF must be >= 20 to use this feature. If the SWF version is set to <20, the stripped SWFs won’t be generated in the “externalStrippedSwfs” folder even if the SWF files have been specified in the text file.

4. It is recommended that this feature should not be used as a means for updating and displaying content in a dynamic manner. The externally hosted SWFs should not be replaced by any other stripped SWFs except the ones generated during IPA packaging. Any change in the secondary SWF requires a corresponding updation of stripped SWFs on external server.

5. All the SWFs specified in the text file should be passed during the ADT packaging as well.

KNOWN ISSUES :

1. External hosting of Flex SWF does not work and might cause the application to crash.

 

30 Responses to External hosting of secondary SWFs for AIR apps on iOS

  1. Colin Holgate says:The article doesn’t answer the main question that people have asked:
    When the externally hosted swf is loaded, does it run as if it had its original code, or is it just artwork assets?
    A couple of other questions:
    Although it’s not recommended to use the mechanism as a way to update a swf, would that work?
    Can the externally hosted swfs be located in the app’s document folder, on the device?
  2. March 21, 2013 at 5:16 pm
  3. Abhinav says:Hi Colin
    • Colin Holgate says:For number 3, my usage case was that you could perhaps move an online swf to the documents folder for offline use later. Like say you have a music player feature and you want the user to choose which large swf to load, the user would have to be online and do that large download every time the app is opened. If the swf could be cached in the documents folder it would only have to be downloaded once.
      • Abhinav says:Hi Colin
      • For such a use case you could write an application logic wherein you would first check the existence of the SWF in the application’s local storage, if it’s present there then you could load it from there itself.
        However if it doesn’t exist then you would download the SWF into the application & store a local copy of that SWF within the application for future use & then load it from the local storage itself. This way you will be able to use the external SWF hosting as well as reduce the users’ network usage as well.
      • March 22, 2013 at 7:18 pm
    • March 22, 2013 at 6:37 pm
  4. Below are the answers to your queries.
    1. The external SWF will work same as if it had the code in it. The only difference in the entire workflow is that the ABC code from all the secondary SWFs will be added in the final executable & just the assets part of the SWF remains in the stripped SWF after the packaging.
    2. It might work & might not work, please try it at your own risk.
    3. This would be same as the locally packaged multiple swf feature done in AIR 3.6. However, a point to note here is that if you try to load secondary SWF which has been specified in the text file for external hosting from the documents directory, then that file would be stripped and saved in the externalStrippedSwf folder and will not be present in the final executable file.
  5. March 21, 2013 at 5:35 pm
  6. CraigMurray says:Why is it not recommended to use this to dynamically update assets on the server and display them in an older version of the app? In Points to Note you state that but without any explanation as to why that is “not recommended”.
    • Abhinav says:Hi CraigMurray
      • CraigMurray says:Ok thank you for the response. There is a big difference between “it’s dangerous, you might break your app if you’re not careful ” and “Apple might not like it”.
        • Abhinav says:In this case the former point is relevant. The behavior and working of the application after the updation of external SWFs cannot be defined.
        • March 22, 2013 at 5:13 pm
      • March 22, 2013 at 11:43 am
    • The reason for the recommendation is that your application might or might not work after the assets have been dynamically updated with newer assets. Hence, if you want to update the assets in the secondary swf(s), it is recommended to recompile the IPA with new secondary swf(s), replace the stripped swf(s) on server and then try to run the newly packaged IPA.
    • March 21, 2013 at 11:35 pm
    • Sergi says:I was wondering myself : how’s loading updated and code stripped SWF is any different from loading new version of image from remote server. Does every SWF carry some kind of unique hashtag or something? So when you compile your app, all swfs hashtags are encoded in the main swf and checked during remote SWF loading ?
      • Abhinav says:Hi Sergi
      • Basically for every secondary SWF, during the IPA packaging the ABC code part of the SWF is moved to the final executable and assets part remain in the stripped SWF. However, when we try to load a secondary SWF which has been updated and then stripped in an IPA which just had the code part of the earlier version of SWF, if their is any difference from the code appended during the initial IPA packaging then the application won’t work. Even if it works, there is no guarantee that it would work seamlessly in the future.
      • April 9, 2013 at 5:08 pm
    • April 8, 2013 at 5:09 am
  7. March 21, 2013 at 8:54 pm
  8. Viviana Baldarelli says:This is my question: I have a symbol in the assets.swf library and the symbol has a linkage name “symbolname” and its base class is a custom class “CustomClass1″. When I download the stripped assets.swf from the server, will I be able to access the library symbol by calling:
    getDefinitionByName (“symbolname”)?
    • Abhinav says:Hi Viviana
    • Yes, you can use getDefinitionByName(“symbolname”) to access the library symbol.
    • March 22, 2013 at 8:28 pm
  9. OR is there another way to access symbols from the library of the assets.swf?
  10. March 22, 2013 at 6:01 am
  11. Leon says:Thank you. After 4 hours of tweaking, it worked!
  12. April 3, 2013 at 12:24 pm
  13. cyberprodigy says:Generated SWF files are much bigger then non-stripped and when opened throws error VerifyError: Error #1042: Not an ABC file. major_version=0 minor_version=0.
    Any Ideas what might be causing it? I’m using AIR SDK 3.7
    • Abhinav says:The reason for the bigger stripped SWFs is the uncompression of secondary SWFs during the packaging process.
      • cyberprodigy says:Thank you for explaining, Abhinav.
        Just to make it clear – should assets load fine in debuging mode, i.e. when passing -target ipa-debug to ADT?
        Do you also happen to know if it is needed also to compile swf file using 3.7, or only the packaging with 3.7 is what’s needed to load external swfs with this method?
        Regards.
        • Abhinav says:Yes, the assets should load fine in ipa-debug target.
          While the assets swf compilation is not restricted to any particular AIRSDK version, the Root SWF must have swf-version 20 for which you will need to use AIRSDK 3.7. Also, IPA needs to be packaged using AIRSDK 3.7 or later.
        • April 24, 2013 at 10:45 am
      • April 22, 2013 at 9:48 pm
    • As far as Error # 1042 is concerned, it is because the stripped SWFs are not a valid SWF files as the Actionscript Byte Code (ABC Code) is removed from these SWFs during ADT packaging. Hence these SWFs cannot be opened as it is in Flash Player. They would work fine when you try to load these SWFs within the iOS Application for which they are generated.
    • April 22, 2013 at 4:15 pm
  14. April 22, 2013 at 4:04 pm
  15. Nathan says:Can someone post a real example? Everything I have tried does not work on an ios device.
    • Abhinav says:Hi NathanHowever, if you are still facing issues with making this feature, please feel free to clear out the issues which you are facing with this feature ?
    • The sample code written in blog post is a fully working AS3 code. Just create a new project using this code, update the URL of URLRequest to the one where you would be actually hosting the secondary stripped SWFs, package the IPA using the method described above, host the stripped SWF to the server which you mentioned earlier in your application and this feature should work fine.
    • April 24, 2013 at 10:28 am
  16. April 22, 2013 at 10:06 pm
  17. bigger butt secrets says:I have read so many articles or reviews concerning the blogger
    lovers however this paragraph is actually a good piece
    of writing, keep it up.
  18. April 24, 2013 at 3:51 pm
  19. Shay Pierce says:Does this solve the “Reloading a SWF is not supported on this operating system” error when attempting to reload a SWF on iOS?
    I need to be able to load in remote SWFs (which I can generate using this “stripping” method), but I also need to be able to reload them.
    • Abhinav says:Reloading of SWFs is still not supported on iOS and the same error would be visible when you try to load remotely hosted SWFs as well.
    • May 7, 2013 at 12:25 pm
  20. May 6, 2013 at 10:19 pm
  21. Kenneth says:I would like to build an app using this type of packaging, but I would like to develop it for both iOS and Android. Does this work for android apps as well? Or is it even an issue for Android apps? Can Android apps load external SWFs with ABC?
    • Abhinav says:Hi Kenneth
      This feature is for iOS only. As far as Android apps are concerned, the loading of SWFs with ABC is supported for locally as well as externally. Hence you can simply load your secondary SWFs with ABC directly from external server, without any required stripping of SWFs.
    • May 23, 2013 at 11:56 am
  22. May 22, 2013 at 11:22 pm
  23. Maximus says:Can i call methods from loaded stripped SWF? For example I need pass init params to loaded SWF, that has public method initParams
  24. May 23, 2013 at 6:06 pm
  25. dan matei says:useless for me
  26. I want to load swf with code from server, and when I update the code on server to reflect in application, which probably will never work because of apple :((
  27. July 28, 2013 at 2:40 am
  28. Greg says:Hi
    In the sample text file you provided there are 3 swf’s – levels of a game. In the game I am working on right now I have 40 levels, each with quite lot of graphics in it, and code ofcourse. I need to load the swf when it is needed and then unload (free the memory , GC) and load the next one. If user come back to the previous screen, the previous swf should be loaded again and the current one unloaded. It’s the common scenario in most games I have ever made 
    • Abhinav says:Hi Greg
    • In your case you could code your game logic such that you save the stripped swfs locally on user’s device once they have been downloaded. So, if the user moves back from one level back to previous level, you can check if the file is available locally or not. If yes, then you can simply load that swf locally without downloading the same swf again which will help in saving both time and network bandwidth.
    • December 4, 2013 at 5:02 pm

  29. Is it possible with AIR 3.8 on iOS?
  30. August 6, 2013 at 3:23 pm
  31. daniel says:Top publish. I look forward to reading a lot more. Cheers
  32. August 19, 2013 at 2:41 pm
  33.  

 

 

=================================

=================================

=================================

 

 

반응형