Can Flash text and images be changed without modifying source FLA? - actionscript-3

Generally speaking, let's say I create a small, simple FLA animation that simply takes the text "Take a look at Tuesday's Deals!" and moves it across the screen. I then build and generate the SWF file.
Is it somehow possible to create this FLA file so that, once the SFW is built, I can change the text without having to open the FLA and recreating the SWF?
Can it be done with images? For example, I create a FLA with a 50x50px image that moves across the screen. Can I switch to another 50x50px image without having to rebuild the SWF?
The reason I ask is because I can create the initial FLA, but I won't be able to modify. The person that will deal with modifying the text and/or image doesn't know Flash.
Thanks.

Yes, you can load content dynamically at run-time. The design of your application should be such that it loads an xml file or something of that nature to specify what artwork/text to use.
So you need to load an XML file , or some other type of data.
Then dynamically load audio/artwork based on that data.
You can even make the app so that it can load other .swf files with new content.

You can use command line assembler/disassembler to do this. Here are the two command line assembler/disassembler I know.
http://www.nowrap.de/flasm
http://swfmill.org/
Disassemble the swf using either one of this, you will get text file.
In your case you can define a variable for text and find and replace the text.
And finally assemble it a swf.
Hope this helps.

you can pass parameters to the swf containing information to be used.
http://www.mysite.com/banner/banner.swf?text="my text"&image="image1"
var flashVars:Object = this.root.loaderInfo.parameters;
var text:String = flashVars["text"];
var imagetype:String = flashVars["image"];
In the code you treat the data to be received.

Related

Actionscript 3 - Accessing a variable on the main timeline

I'm trying to make a file that is easy for a non-flash user to use/reuse to easily display content. The key here is that this file is to be a template for a novice user to just copy/paste some very minimal code to create different "flash card" type swf files.
The file I am creating has multiple buttons on the main timeline which when clicked, attaches a movie clip which will display a dynamic text area with content specific for the button that was clicked. The content for the text area will be loaded from a separate text file.
For the sake of this example, I'm just going to refer to one button...
So, on the main timeline, in frame 1, I have a variable definition:
var myFilename1:String = "mySampleFile2.txt";
When the button on the main timeline, in frame 1 is pressed, a movie clip is loaded which contains a text area. The content for the text area is located in that file: mySampleFile2.txt.
If I hard-code the file name, it works like a dream:
myTextLoader.load(new URLRequest("mySampleFile2.txt"));
But I don't want to hard code the file name. I want to refer the variable in the main timeline. In AS 2, it would have been
myTextLoader.load(new URLRequest(_root.myFilename1));
In AS3 I thought it would be either:
myTextLoader.load(new URLRequest(root.myFilename1));
OR
myTextLoader.load(new URLRequest(MovieClip(this.parent.root).myFilename1));
When I run the code I get the following error and when I run a trace I get the file name is NULL.
TypeError: Error #2007: Parameter url must be non-null.
How to I access the file name stored in the variable on the main timeline?
*************************** UPDATE! *************************
So I just discovered that the issue is related to a button on the screen. The button is one from the buttons library. If I remove the button, everything works great. But as soon as that button is on the main timeline, it makes it to I cannot access the variables using MovieClip(root).variable_name;. Unfortunately I want that button to trigger the events within the MovieClip. Any thoughts?
Not a good idea
You are not able to achieve your goals with this bad practice approach.
to use/reuse
The code you want to provide is not very reusable. It heavily relies on one variable existing in a certain place. Therefore it cannot be use twice in a project.
But I don't want to hard code the file name.
And now you are hard coding the variable and its location. If you considered hard coding the file name to be a bad things, consider this a bad thing, too.
What the problem is
Basically speaking, the problem is that your component is reaching out to grab this variable from somewhere within your project. But it is not the concern of the component to find the content it should display. It is not well encapsulated.
Learning from existing things
You want to display text. Let's take a look at the TextField class to see how it displays text.
var tf:TextField = new TextField();
tf.text = "hello";
addChild(tf);
As you can see, the text that it should display is passed to the TextField object. There is not some arbitrary variable one has to set in order to modify the text, as you are planning to do:
var tf:TextField = new TextField();
var someArbitraryVariableThatModifiesATextField = "hello";
addChild(tf);
There is no obvious connection to the TextField object and if there's a second TextField, this doesn't work at all.
Applying that to the problem at hand
Just like the TextField, your "flash card" should receive the file as a parameter. Either pass it to the constructor as seen in the example below, or create a method that takes it as a parameter.
var card:Card = new Card("mySampleFile2.txt");
addChild(card);
Additional thoughts
Create additional methods to set the values individually. There's nothing worse than some code that does exactly what one wants, but only operates on files and one doesn't have a file. The goal is again, to make it easy to reuse the code
Use the [Inspectable] meta tag to allow the user of your code to modify the properties at author time. This can be used to the extend of modifying properties belonging to a physics engine, now this is what I would call easy to use for a novice user.
Instead of writing code and thus requiring to recompile the file again, take the information (either the path to the file or it content) from the flashars that are passed to the .swf file when it is embedded into an html page. This makes the single .swf file truely reusable and easiest to use, because there's no As3 coding required whatsoever.
I haven't tested this but I believe the "root"-stage would be:
this.parent
In the child swf.
check out this question: as3 access variable from parent swf
Good luck!
Accessing variables or movieclips at main timeline is as following:
AS2:
_root.variable_name;
AS3:
MovieClip(root).variable_name;

Single-swf preloader wrapper in Flash

I have a .swf that I'd like to create a preloader for. In the end, I want a single file which combines both the preloader and the content .swf.
Every tutorial I can find on Flash preloaders offers one of two options:
Using a URLRequest in the preloader wrapper to load an external content .swf. This will not work because I do not want two .swfs.
A "single-swf" solution like this one which works for assets which you have created in the Flash IDE. I assumed I would easily be able to go this route, but I'm having trouble. I cannot use the Flash IDE to import my content .swf. I can use an embed tag like so:
[Embed(source="content.swf")]
private var MyContent:Class;
But it seems like this embedded content is exported on the first frame, so my preloader only appears after the content .swf is loaded. I'm guessing it's something along these lines. Any thoughts?
But it seems like this embedded content is exported on the first frame
You are absolutely correct, all Ebmed do embedding in the first frame by default but mxmlc provides solution by creating two frames swf with preloader in the first frame via the metatag:
[Frame(factoryClass="Preloader")]
You can find step by step guide here http://www.bit-101.com/blog/?p=946.

Better way to include images in AS3 than embed?

I am putting together a Flash game (using Flixel) and I have a lot of sprites whose images (.png format, mostly) I need to include in my game. I'm used to using code like:
[Embed(source = "../../lib/ship-0001.png")]private var _ship0001Sprite:Class;
in order to include an image file. Is there a way I can do this other than embed? I'd like to use a for loop if possible, grabbing every needed file by number. This way instead if having several lines for each ship type (to grab its sprite, icon, store view, etc), I can just change a variable (eg NUM_SHIP_TYPES) and add the files by number for the new ship.
Is embed the only way to do this where the files are included in the .swf, or is there another way I can do this? Or is there a way I can use a for loop with embed?
This may not be a popular answer. But it depends on preferences. I'm not trying to start a best practices discussion, just listing a possibility.
If you have multiple images for each ship. And its the same number of images per each ships then the below could be a possibility. Of not, then completely ignore what I suggest.
Have a folder for each one of your ship types and then a class file within that folder and place your images within that folder along side the class files..
package shipimages.ship1 { class Ship1Images {
[Embed(source="0001.png")] public var img0001:Class;
[Embed(source="0002.png")] public var img0002:Class;
[Embed(source="0003.png")] public var img0003:Class;
} }
.
package shipimages.ship2 { class Ship2Images {
[Embed(source="0001.png")] public var img0001:Class;
[Embed(source="0002.png")] public var img0002:Class;
[Embed(source="0003.png")] public var img0003:Class;
} }
But as I said, this is only beneficial in a small case scenarios but it does mean everything is more sub divided and if you were to make an Interface it could be easily done for factory pattern.
If you have lots of assets, the best way is to leave them as external files and use Loader to load them.
That way you can display a progress bar and, as you said, it also makes it easier to manage your assets.
Do you want to have the objects readily available within the same swf or do you want to load them externally. Would be the first question, that comes to my mind.
If you want to embedd images inside the swf : Add them all in an fla, create a swc from it and use them in your project. When you want to change something, change the fla. Regenerate the swc. Recompile.
If you want the images to be loaded on runtime, use UrlLoader, personally I prefer using LoaderMax, which is part of the greensock package. It gives you more options to work with loading. Here is an example to help you out : http://www.greensock.com/loadermax-tips/

ActionScript not showing up in Actions - Frame window in Flash CS5

In an existing fla file, I'm not able to find strings within actionscript that is contained within certain frames (regardless of which frame it's in). So for example in frame 1 there is the following code:
stop();
But searching for 'stop' (without quotes) doesn't turn up any results.
If I create a new fla file, I'm able to find actionscript in all keyframes. Furthermore, I've noticed that the "Actions - Frame" window shows me a tree of all the ActionScript in the scene, organized by frame. This isn't happening in the first .fla file - I can open up and edit actionscript for a frame, but the tree/index never gets built.
Has anyone run into a situation like this? Is the first .fla corrupt in some way, or is there a setting to allow me to easily find actionscript within all frames? Thanks!
Have you checked the settings in the search box? Maybe the search is document based and the specific file you are having trouble with has different settings from the default one. Try to compare the working file with your "corrupt" file. Actionsctipt should be enabled.

compile .swf from .as file that defines a sprite

Someone else wrote and gave me an ActionScript source file that defines a subclass of Sprite. I am told that I need to, from this, generate a .swf file whose behavior is entirely defined by that one sprite. In other words, when the "movie" starts, one instance of that Sprite subclass should be created and set running, and it should keep running until the <object> is destroyed. The sprite doesn't define its own dimensions as far as I can tell, so I also need to know how to specify those (it's supposed to cover the entire area of the movie, and ideally that would not be hardwired into the .swf but rather taken from the <object> definition in the HTML).
I know basically nothing about Flash, and I'm much more of a command line guy than an IDE guy, so if there's a way to write the necessary wrapper in a text editor and then just use the Flash IDE to compile it (or better still, compile it using some sort of command line utility that either came with CS4 or I can install on my Linux box, without paying Adobe an additional arm and leg), detailed instructions on how to do that would be the ideal answer to this question. Failing that, please provide step by step instructions for how to create said wrapper in the IDE. I've got CS4.
The sprite tries to communicate with the containing HTML document, so if there's any glue needed inside the .swf but outside the .as code to achieve that, I need to know how to implement that too.
Since you already have CS4 you can compile your source file by using the DocumentClass.
http://www.heaveninteractive.com/weblog/2008/03/04/introduction-to-the-document-class-in-actionscript-30-tutorial/
Alternatively, for future reference , if you download the free Flex SDK , you could use the command line to compile your source file
mxmlc SourceFile.as
You can then use SWFObject to embed the resulting swf file in your HTML page
http://learnswfobject.com/