views:

34

answers:

2

I have created i class width I inherit from with u number of subclasses. Now i what to add different images to the subclasses. Since I'm new to Flash and AS3 i have some problems to complete this.

Blend.as

package com.jarsater.sthlmroast
{
    import flash.display.MovieClip;

    public class Blend extends MovieClip
    {  
        private var _width:int = 54;
        private var _height:int = 188;  

    public function Blend():void
    {
        this.width = this._width;
        this.height = this._height;
    }
}

Dark.as

package
{
    import com.jarsater.sthlmroast.Blend

    public class Dark extends Blend
    {
         public function Dark()
         {
             super();
             this.setBlend('Dark');
         }
    }
}

How can i add an image to the Dark.as object and then place the object on the stage?

A: 

You could load in the image, inside the Dark class, through XML. Once you've loaded in the image data you can add it to a MovieClip and add that to the stage. There are plenty of tutorials online which will help you with loading from XML (it's very straight forward), but here's one I found which should help:

http://www.republicofcode.com/tutorials/flash/as3xml/

Good luck!

debu

debu
+1  A: 

How to load an image:

var request:URLRequest = new URLRequest('path_to_image.png');
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoadComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImageLoadError);
loader.load(request);


function onImageLoadComplete(e:Event):void
{
    trace('onImageLoadComplete()');
    var loaderInfo:LoaderInfo = LoaderInfo(e.target);
    loaderInfo.removeEventListener(Event.COMPLETE, onImageLoadComplete);
    loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onImageLoadError);

    var bitmap:Bitmap = Bitmap(loaderInfo.content);
    addChild(bitmap)
}

function onImageLoadError(e:IOErrorEvent):void
{
    trace('onImageLoadError(): ' + e.text);
    var loaderInfo:LoaderInfo = LoaderInfo(e.target);
    loaderInfo.removeEventListener(Event.COMPLETE, onImageLoadComplete);
    loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onImageLoadError);
}

In this example I add the bitmap to the display list but you could just as easily add the Loader object depending on your needs.

heavilyinvolved