I am trying to call in images dynamically from my Library in a more efficient manner.
With an bitmap image in the Library set up for actionscript with the class name of 'itemFromLib', i tried:
Code:
function placeImage():void {
var _img:itemFromLib = new itemFromLib();
var _bitName:Bitmap = new Bitmap(_img);
addChild(_bitName);
}
This works fine if i want to bring in one image, but i want to have a method that can bring in a number of different images on demand
I've tried something like this:
Code:
placeImage("itemFromLib");
function placeImage(className:String):void {
var _cls:Class = getDefinitionByName(className) as Class;
var _img:_cls = new _cls();
var _bitName:Bitmap = new Bitmap(_img);
addChild(_bitName);
}
AND:
Code:
placeImage("itemFromLib");
function placeImage(className:String):void {
var _cls:Class = getDefinitionByName(className) as Class;
var _img:Object = new _cls();
var _bitName:Bitmap = new Bitmap(_img);
addChild(_bitName);
}
Both with the results of an error that "1067: Implicit coercion of a value of type Class to an unrelated type flash.display:BitmapData."
That's not true. _cls is a Class. An instance of that Class will be something else, not a class. Object covers it, but something more specific would be better.
I did some more messing around... I tried Object and Class, but still not working all the way but i did get something that didnt throw errors its just not displaying anything.
I have attached the file below (saved to CS4). You can see the three different results/setups that i am having issues with.
But here is the code in the .as :
(there is an bitmap in the library with the class reference of 'ImgFromLibrary')
Code:
package {
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.getDefinitionByName;
import flash.display.Stage;
public class DynamicLoader extends MovieClip {
public var _bitmapImage:ImgFromLibrary; // bitmap item from library for reference
public var _mcImage:WaterDrop; // mc item from library for reference
public function DynamicLoader() {
inti();
}
private function inti():void {
// I need to pass the library objects Class name from
// several different methods throughout the program at different times
placeNewElementOnBoard("ImgFromLibrary");
}
private function placeNewElementOnBoard( className:String ):void {
trace("ready to place the image : " + className);
// ----->// ### THIS WORKS, BUT NOT DYNAMIC ###//
var _img:ImgFromLibrary = new ImgFromLibrary();
var _bitName:Bitmap = new Bitmap(_img);
_bitName.width = 40;
_bitName.height = 70;
_bitName.x = 0;
_bitName.y = 0;
addChild(_bitName);
// ----->// ### THIS WORKS, KINDA... NO ERRORS... BUT NOTHING SHOWS UP ###//
// var _img:Class = getDefinitionByName(className) as Class;
// var _bitName:Bitmap = new Bitmap(_img.bitmapData);
// _bitName.width = 40;
// _bitName.height = 70;
// _bitName.x = 0;
// _bitName.y = 0;
// addChild(_bitName);
// ----->// ### THIS THROWS AN ERROR in the compiler, ..., Line 58 1067: Implicit coercion of a value of type Class to an unrelated type flash.display:BitmapData. ###//
// var _cls:Class = getDefinitionByName(className) as Class;
// var _img:Class = new _cls();
// var _bitName:Bitmap = new Bitmap(_img); // var _bitName:Bitmap = new Bitmap(_img.bitmapData); <--- if i add 'bitmapData' i get a different error in the output window
// _bitName.width = 40;
// _bitName.height = 70;
// _bitName.x = 0;
// _bitName.y = 0;
// addChild(_bitName);
}
//this is something that i was trying to do to streamline the code... not sure where its going yet
private function getClass( className:String ):Class {
return getDefinitionByName( className ) as Class;
}
}
}
Last edited by willCrain_514; 06-08-2011 at 04:52 PM.
Probably bitmapData Flax, I embed all my bitmaps in the class itself so I forget how the library works.
If the symbols are being exported as BitmapData, an instance of that type should work.
var mySymbol:BitmapData = new SomeSymbol();
var myBmp:Bitmap = new Bitmap(mySymbol);
Yeah a few kinds of resources get special treatment when using the embed directive: BitmapData, Sound, XML. But you can embed a class as a raw bytearray and make it a Class. But that's neither here nor there.
getDefinitionByName returns a Class, such as Sprite. An instance of the class Sprite is a Sprite, not a Class. Well, technically getDefinitionByName returns an Object, but that Object is a Class, for the most part.
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
public class GetDefinitionByNameExample extends Sprite {
private var bgColor:uint = 0xFFCC00;
private var size:uint = 80;
public function GetDefinitionByNameExample() {
var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
var instance:Object = new ClassReference();
instance.graphics.beginFill(bgColor);
instance.graphics.drawRect(0, 0, size, size);
instance.graphics.endFill();
addChild(DisplayObject(instance));
}
}
}