0
votes

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:

icon as classes

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.

2

2 Answers

0
votes

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;
0
votes

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.