Loading Dynamic Image from Library - actionscript-3

In a Flash (Actionscript 3.0) project I'm working on, I'm trying to load a PNG file from the library, however which icon should load is not established until run-time.
The project is a weather bar that loads an icon associated to the data feed that's returned as a JSON object. I have tried the following:
var iconData:Class = getDefinitionByName("i"+image);
var i:* = new iconData(130, 130);
var bitmap:Bitmap = new Bitmap(i);
if(p==0){
today_mc.today_icon.addChild(bitmap);
}
There is a bunch of evaluation that happens to determine "image".
All of the imported files are included as Classes for use in Actionscript, as seen here:
The error I'm getting is:
Implicit coercion of a value with static type Object to a possibly
unrelated type Class.
So if the JSON response says the weather code is '33', and I determine in the script that code 33 = icon 4 (i4), I need to load i4.png to the stage.

In the example I read on the Adobe Site for the function getDefinitionByName, located here, they use the as operator to evaluate the expression before assigning the value to a variable.
So I would recommend trying this first:
var iconData:Class = getDefinitionByName("i"+image); as Class;
Also be sure that you have the proper import statements specified. In the linked example, they list the following; however, the first two I'm unsure if you need based on your application's needs:
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;

I would suggest keeping your images external to your Flash file. Otherwise, all 30 pngs will be be downloaded every time, even though the user only needs to see one. Then use the AS3 Loader class to load only the png needed at runtime.

Related

Actionscript 3, reading two files then triggering a function, best practice

I need to read a part of two files, and once both parts of both files are loaded to trigger a function that does some work.
What is the best way to approach this problem.
I am currently triggering the second file load once the first is loaded, however it seems poor coding style, I think this because my OO code starts looking procedural.
EDIT: So its an Air based app so using filestream.
In the end I found I actually needed to read each file in a different way. Because I need the same part of both files, and I dont know the file size, I need to read one file first and once I have fileStream.bytesAvailable and position, I can then look for the same data from the second file. I found I must handle files smaller than my read size and the end of files beyond multiples of my read size.
You don't specified what file and from where you wont to load the file but you can actually load multiples files in parallel.
If you want to read only part of file from local machine you can use AIR's FileStream class - very easy and you don't have to load whole few hundreds MB file:
import flash.filesystem.*;
var file:File = File.documentsDirectory;
file = file.resolvePath("Apollo Test/test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
trace(str);
fileStream.close();
Another option is to use URLStream and listen for ProgressEvent.PROGRESSevents to read data of partially loaded file.
You may also want to see NetStream class which is used to stream video.
there is many options, using File, FileStream is only available on air applications.
The File class extends the FileReference class. The FileReference
class, which is available in Flash® Player as well as Adobe® AIR®,
represents a pointer to a file, but the File class adds properties and
methods that are not exposed in Flash Player (in a SWF running in a
browser), due to security considerations.
as noted above, if you are creating a non-AIR application, FileReference should be used instead of FileStream and File classes, as you dont tagged AIR in your question.
FileReference does not provide any open("path") to you (due to security considerations), but a browse method will be available and ask's your client's for selecting a file. here is an example, which also explain how to trigger a function when opening is done:
var filereference:FileReference = new FileReference();
filereference.addEventListener(Event.SELECT, onFileSelected);
var text_files:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var all_files:FileFilter = new FileFilter("All Files (*.*)","*.*");
filereference.browse([text_files, all_files]);
// triggered when a file is selected by user
function onFileSelected(e:Event):void {
filereference.removeEventListener(Event.SELECT, onFileSelected);
filereference.addEventListener(Event.COMPLETE, onFileLoaded);
filereference.load();
}
// triggered when file loading is complete
function onFileSelected(e:Event):void {
var data:ByteArray = fileReference["data"];
filereference.removeEventListener(Event.COMPLETE, onFileSelected);
}
two more events to be listened for suddenly error's occurred and displaying a progress bar for loading progress (its sync):
IOErrorEvent.IO_ERROR and ProgressEvent.PROGRESS

patching actionscript without constantly rebuilding swf

How can I patch actionscript without constantly rebuilding sfw?
There is a fairly large actionscript project that I need to modify and resulting swf is used on a live site. The problem I have is that I need to make quick small updates to the swf and it's not acceptable to update the swf on live site ten time a day (I don't control that part, I need to ask another person to put the result on live site).
What options do I have to workaround that issue? I'm a complete noob when it comes to actionscript and all flash related stuff and I'm not even sure what is possible and what isn't. I'm thinking about the following approaches, which ones are possible/acceptable?
Imagine that live site is on www.livesite.com/game.html and this page loads www.livesite.com/flashgame.swf. In that flashgame.swf among many others there is a class com/livesite/Magic.as that gets instantiated and instance of that class has a member variable xxx123 of class com/livesite/MagicWork.as. I only need to modify this MagicWork class. Now, I simply modify it, build and ask to put updated flashgame.swf live. So, I want to avoid that manual step.
All my ideas can be split in two basic approaches: 1) keep flashgame.swf totally unmodified and then load flashgame.mod.swf that contains alternative implementation of that MagicWork class, then using javascript access internals of instance of that Magic class and update its xxx123 member to be an instance of MagicWork class from flashgame.mode.swf. I'd need to modify game.html to load my javascript so that my js file would load flashgame.mod.swf and patch code inside flashgame.swf. By patching I mean javascript-style overwriting of Magic.xxx123 to a new value. flashgame.mode.swf would ideally reside on my own host that I control. Is that kind of stuff possible, if not what's not possible?
2) I could make one-time change in flashgame.swf so that it would effectively load itself my own code at runtime and patch it's xxx123 member. Is that possible?
I had already written a note about loading runtime shared libraries previously. I'll put the most essential parts of the process here, and add a link to the full article at the end.
You need to tag your main application entry point in the following manner.
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
}
Then create a class called Preloader.
public class Preloader
{
public function Preloader()
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.loader_completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.loader_ioErrorHandler);
var request:URLRequest = new URLRequest("math.swf");
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.load(request, context);
}
private function loader_completeHandler(event:Event):void
{
var mainClass:Class = getDefinitionByName("Main") as Class;
var mainInstance:Main = new mainClass();
this.addChild(mainInstance);
}
}
The full implementation of the Main class is like this.
[Frame(factoryClass="Preloader")]
public function Main()
{
var integer:IntegerArithmetic = new IntegerArithmetic(); // Type declared in math.swf
var operand1:int = 10;
var operand2:int = 10;
var result:int = integer.add(operand1, operand2);
}
Deploying Runtime Shared Libraries
The confusing bit about using a runtime shared library is realizing that the SWF has to be extracted from the SWC at the time of deploying the application. This was not immediately obvious and I ended up spending days placing a compiled SWC file in various locations and wondering why the application was unable to load it at runtime. An obscure article on the Adobe website made explicit this particular step and set things straight.
The full article along with the same example is available at http://www.notadesigner.com/runtime-shared-libraries-with-plain-actionscript/.

AS3 text to file

I'm making a game in cs3 using as3. I've seen dozens of tutorials, but none of them are working for me.
I found the simplest code I could, and it still gives me an error "1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference."
here's the code I'm using:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")
Are you importing FileReference? If not, you'll need:
import flash.net.FileReference;
..inside your package declaration (or at the top of your block of code if you're coding on the time line)
With that import included, your code works for me.

Importing a Package in Adobe Flash CS3 - Pointless Messages from "Access of Undefined Property" to none at all

I inherited a Flash CS3 legacy app and I am trying to refactor it a little. Eventually everything is supposed to be moved over to JS, but for now I would like to start working with what I have rather than attempting a complete rewrite. First I would like to install a little regression test.
I am trying to setup the test in it's own package in order to reducing the seams it has with the original app. The app uses a lot of global variables and I wish not to interfere with those.
I can't get my regression test to work, since I can't figure out how to import the package properly. I am pretty certain that I am overseeing something obvious.
My folderstructure looks as follows:
+- ascripts
| +-- dependencies.as
|
+- root.fla
+- initialize.as
+- regressiontest.as
root.fla is my one and only fla file. It just contains:
stop();
include "initialize.as" // Let's go outside!
initialize.as contains all the magic. I am using this external file so I don't have to use the Flash IDE (since it is the worst IDE for coding). In there I have this:
// ...
import fl.controls.ComboBox;
include "ascripts/dependencies.as"
var t = new regressiontest.TestRunner(); // 1.
// ...
At 1. I am trying to instantiate my regression test class. It can be found in the file regressiontest.as and looks like this:
package regressiontest {
public class TestRunner {
public function TestRunner() {
trace('Hello');
// Actual Test Code Here ...
}
}
}
So now when I go to flash and debug the movie using Strg+Shift+Enter, I get the following error messages. I tried every way I could think of, so here is an overview of what I achieved so far:
var t = new TestRunner();
Message: 1180: Call to a possibly undefined method TestRunner.
var t = new regressiontest.TestRunner();
Messages:
root.as: 1120: Access of undefined property regressiontest.
regressiontest.as: 5001: The name of package 'regressiontest' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. folder\regressiontest.as
regressiontest.as: 5008: The name of definition 'TestRunner' does not reflect the location of this file. Please change the definition's name inside this file, or rename the file. folder\regressiontest.as
import regressiontest.TestRunner;
var t = new TestRunner();
root.as, Line 19: 1172: Definition regressiontest:TestRunner could not be found.
root.as, Line 20: 1180: Call to a possibly undefined method TestRunner.
What is most confusing to me is that Flash appears to be picking up the class definition in regressiontest.as somehow. When I put an obvious error, such as
public function TestRunner() {
shoelace('Hello');
}
and use this to instantiate an object of the class:
var t:TestRunner = new regressiontest.TestRunner();
then I get the Message:
regressiontest.as: 1180: Call to a possibly undefined method shoelace.
One might think now that the instantiation causes the problem. But when I set the code from shoelace to trace and leave the instantiation I get the following messages:
Messages
Scene 1, Layer 'AS', Frame 1: 1046: Type was not found or was not a compile-time constant: TestRunner.
root.as: 1120: Access of undefined property regressiontest.
regressiontest.as: 5001: The name of package 'regressiontest' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. folder\regressiontest.as
regressiontest.as: 5008: The name of definition 'TestRunner' does not reflect the location of this file. Please change the definition's name inside this file, or rename the file. folder\regressiontest.as
I tried renaming the file within the package, I tried renaming the package, I tried importing with import regressiontest.*; and so on. What am I missing?
What do I need to set my filename to? Does it need to match the PACKAGE NAME or the CLASS NAME?
Am I missing some crazy camel case rule?
Is there a maximum length to package names or something crazy like that?
Did I forget to configure flash or the FLA file correctly?
Am I missing some magic keyword?
Might there be sideeffects from the includes or imports at the beginning of my script?
I can reproduce these steps and I can provide the package via github.
Thank you in advance, with clueless greetings from Heidelberg, Germany
Johannes
As I see it, TestRunner is the class and regressiontest is the package, so you need import the class.
import regressiontest.TestRunner;
var t:TestRunner = TestRunner();
Also, you must do this changes.
The filename must be exactly the same of the class, replace regressiontest.as by TestRunner.as
Create a folder for the package and call it regressiontest, put inside TestRunner.as.

Custom AS3 Class not Updating

I've had a similar issue to this, but the means that I solved the last one are not working here.
I have a custom class that consists of 12 separate .as modules. They're declared in the document class as follows:
import trailcrest.v1.s3.averta;
import trailcrest.v1.s3.chronos;
import trailcrest.v1.s3.eripio;
import trailcrest.v1.s3.fabrilla;
import trailcrest.v1.s3.gradua;
import trailcrest.v1.s3.lingua;
import trailcrest.v1.s3.navigare;
import trailcrest.v1.s3.pedem;
import trailcrest.v1.s3.praeferre;
import trailcrest.v1.s3.scriba;
import trailcrest.v1.s3.securos;
import trailcrest.v1.s3.sonus;
public static var Averta:averta = new averta();
public static var Chronos:chronos = new chronos();
public static var Eripio:eripio = new eripio();
public static var Fabrilla:fabrilla = new fabrilla();
public static var Gradua:gradua = new gradua();
public static var Lingua:lingua = new lingua();
public static var Navigare:navigare = new navigare();
public static var Pedem:pedem = new pedem();
public static var Praeferre:praeferre = new praeferre();
public static var Scriba:scriba = new scriba();
public static var Securos:securos = new securos();
public static var Sonus:sonus = new sonus();
This is a new version of the code. I am able to successfully refer to all of these classes and the public variables and functions inside in the "osr.as" document class. I can also SEE one module from another (i.e. Sonus can see Scriba using "osr.Scriba."
Where I'm having trouble is that, while the various modules used to be able to access all of each other's public functions and variables perfectly, after I added some new modules and variables and removed some old ones, Flash Professional is still literally USING the old version. Inside of any module, the code hints are showing all of the old public functions and variables, and none of the new ones.
I am guessing that this has something to do with some sort of temporary file that I can't get to. I absolutely need this working this week!
My .fla is "Tester.fla," and the document class is "osr.as." They're both in the same directory. Also in the same directory is the folder structure "/trailcrest/v1/s3/" which contains all of the Trailcrest modules.
Help??
EDIT: Whenever I try to reference one Trailcrest class from another Trailcrest class (i.e. osr.Sonus.foo), I get...
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
I have confirmed beyond a shadow of a doubt all references.
This is the age old problem of what ultimately boils down to is the Verify Error. It happens when you embed "Class A" in one or more applications, modules, swfs, etc. Ultimately every output swf that uses "Class A" must be recompiled when "Class A" is changed. Otherwise you end up with a situation where 1 module has the newer implementation but others don't. This problem is compounded by the fact that the application domain is defined as a "first in wins" when it encounters a Class of the same name / package - meaning if the old one is referenced first, the newer one loaded later gets ignored.
The more permanent solution is to use class promotion to an RSL that ultimately allows the RSL to control the actual "Class A" reference in which it also implements an "IClassAImpl" interface that all modules use. This allows the compiler to cross link the reference with a signature it knows about without actually embedding the actual class itself.
Well, I finally figured it out. Here's the skinny on what was happening:
#1: Flash was apparently pulling an old version of the Trailcrest modules. To remedy this, I backed up everything and then removed all old instances of Trailcrest from my entire computer. Then, I put only the new modules back. That fixed the problem with Code Hints showing the old modules and variables.
#2: I had been experiencing Error #1009 whenever one Trailcrest class tried to access any component of another Trailcrest class, even though the references were all correct.
The cause was that I was calling functions on the various modules directly from the document class osr.as, outside of a function. This, of course, executes on the program start.
However, all the code within one Trailcrest class that called another Trailcrest class (i.e. osr.Sonus.foo) would not be able to access "foo" because osr.as for some reason or another hadn't finished initializing the classes before it ran the code that called them. This occurred, even though the problem code was well below the code that initialized the classes (see my question).
To fix this, I simply had to wrap the problem code into a public static function in the document class, and then call it from the Timeline. That ensured that all the classes were initialized before they tried referencing each other.
Needless to say, everything is running like a well-oiled machine now. How weird.
I'd welcome any explanation as to WHY this fixed the problem.