Skip to main content
Participating Frequently
March 23, 2010
Answered

Can you pass a bitmap between two custom classes?

  • March 23, 2010
  • 3 replies
  • 1209 views

I have two custom as3 classes, TriviaGame and TriviaQuestion, and I am   attempting to retrieve a bitmap image stored in TriviaQuestion and have   found it impossible thus far.

It seems the root of my problem is that bitmap is somehow null in my  GetImage() function, even though it is class data that has been set earlier in  ImageLoaded(). The only time I am able to access the bitmap's info (in order to add it   to the display) is inside ImageLoaded().

In TriviaQuestion.as, loading the image:

private function XMLLoadedHandler (e:Event):void
        {
            xml = new XML(e.target.data);
           
            // grab title
            titleFormat = new TextFormat( font, 25, color, true, null, null, null, null, "center" );
            titleText = CreateText( xml[0].attribute("title"), titleFormat, (stageProps.stageWidth/2), (0 + margins), (stageProps.stageWidth) );
           
            // get questions/answers
            var qList:XMLList = xml.item;
            totalQuestions = qList.length();
            questions = new Array();
           
            for ( var i:int = 0; i < totalQuestions; i++ )
            {
                var myItem:XML = xml.item;
                var temp:TriviaQuestion = new TriviaQuestion();
               
                // set question class data
                temp.SetQuestion( myItem.question );
                if ( "image" in myItem )
                {
                    temp.SetImageProps( myItem.image, myItem.image.attribute("position") );
                }
                var answers:XMLList = myItem.answer;
                temp.SetAnswers( answers );
                temp.SetHint( myItem.hint, xml[0].attribute("ptsPerHint") );
                temp.SetFact( myItem.fact );
                temp.SetPotentialPoints( xml[0].attribute("ptsPerQuestion") );
               
                questions = temp;
            }
           
            AssembleQuestion();
        }

In TriviaQuestion.as, setting the class data:

public function SetImageProps( url:String, attr:String ):void
         {
            imgLoader = new Loader;
            imgLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, ImageLoaded, false, 0, true );
            imgLoader.load( new URLRequest( url ) );
           
            position = attr;
        }
       
        public function ImageLoaded(e:Event):void
        {
            bitmap = LoaderInfo(e.target).content as Bitmap;
        }

In TriviaGame.as MakeQuestionSprite()

private function MakeQuestionSprite( s:Shape ):void
         {
            if ( "image" in xml.item[currentQuestion] )
            {
                var myPos:String = questions[currentQuestion].GetPosition();
                var myImg:Bitmap = questions[currentQuestion].GetImage();
                errorText.text = "Img: " + myImg.width;
            }
            var myQ:String = questions[currentQuestion].GetQuestion();
            questionText = new TextField();
            questionFormat = new TextFormat( font, 15, color, null, null, null, null, null, "center" );
           
            if ( "image" in xml.item[currentQuestion] )
            {
                if ( myPos == "right" )
                {
                    myImg.x = stageProps.stageWidth - margins - myImg.width;
                    //myImg.y = margins + s.y + (s.height / 2);
                    //myImg.visible = true;
                    //questionText = CreateText( myQ, questionFormat, margins, myImg.y, (stageProps.stageWidth - margins - myImg.width) );
                }
                //questionSprite.addChild( myImg );
            }
            else
            {
                questionText = CreateText( myQ, questionFormat, margins, (margins + s.y + s.height / 2), (stageProps.stageWidth - margins) );
            }
           
            questionSprite.addChild( questionText );
        }

In TriviaQuestion.as, GetImage

public function GetImage ():Bitmap
         {
            var copy:Bitmap = new Bitmap( bitmap.bitmapData.clone() );
            return copy;
        }
This topic has been closed for replies.
Correct answer waterlovinguy

Not that I know of; it displays perfectly on screen.


You might be trying to copy your bitmap before it's ready. When I made this test it kept failing with the same messages you are getting, even after many different syntax tries. But when I split the test TriviaQuestion class up and added an event listener (see bold text below) for when the loader was completed, then I was able to retrieve the new bitmap. Below are my two example classes based on your examples:

TriviaGame

package {
    import flash.display.Sprite;
    import TriviaQuestion;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.events.Event;


    public class TriviaGame extends Sprite {

        private var questions:Array;
        private var q1:TriviaQuestion;

    public function TriviaGame() {
            questions = new Array();
            q1 = new TriviaQuestion();
            questions.push(q1);

            MakeQuestionSprite();
        }

    private function MakeQuestionSprite():void {
           var myTriviaQuestion:TriviaQuestion=questions[0];
            myTriviaQuestion.addEventListener("Ready", addNewBitmap);

            this.addChild(myTriviaQuestion);
        }

    private function addNewBitmap(e:Event):void {
            var mybitmap:Bitmap=questions[0].GetImage();
            mybitmap.x=400;
            this.addChild(mybitmap);

        }


    }// end of class
}

----------------------------------------

TriviaQuestion

package{
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.BitmapData;
   
    public class TriviaQuestion extends Sprite{
   
    private var imgLoader:Loader;
    public var bitmap2:Bitmap;
    private var position:String;
    private var _copy:Bitmap;
   
    public function TriviaQuestion() {
        SetImageProps("frog4.jpg" );
    }   
   
    public function SetImageProps( url:String, attr:String="" ):void
         {
            imgLoader = new Loader;
            imgLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, ImageLoaded, false, 0, true );
            imgLoader.load( new URLRequest( url) );
       
            position = attr;

        }
       
    public function ImageLoaded(e:Event):void
        {
            bitmap2 = new Bitmap();
            bitmap2 = Bitmap(e.target.loader.content);
            addFrog();
            this.dispatchEvent(new Event("Ready"));
        }


    public function addFrog():void {
        this.addChild(bitmap2);


        var frog:Bitmap = GetImage();
        frog.x = 200;
        this.addChild(frog);
        }
       
    public function getBitmapData():BitmapData{
        return bitmap2.bitmapData.clone();
        }

    public function getLoader():Loader {
        return imgLoader;
        }

    public function GetImage():Bitmap
         {
            var copy:Bitmap = new Bitmap(bitmap2.bitmapData.clone());
            return copy;
        }


    }//end of class
    }

Message was edited by: waterlovinguy

3 replies

Participating Frequently
March 23, 2010

waterlovinguy: Yes, bitmap and the loader are both declared as members of the class.

Also, I have already tried that approach, and it yielded the same results as what I have above (I think it is essentially the same).

kglad: Yes, temp is declared as a TriviaQuestion in the first block of code and then placed in the questions array.

kglad
Community Expert
Community Expert
March 23, 2010

if there's a questions array that contains TriviaQuestion elements, you can retrieve the bitmap associated with each element by using:

var bmp:Bitmap=questions[elementIndex].GetImage();

kglad
Community Expert
Community Expert
March 23, 2010

addendum:  i see you have that code.  so, if that fails, questions array does not have TriviaQuestion instances when that code executes.

if you don't understand that, show the code that assigns elements to questions.

kglad
Community Expert
Community Expert
March 23, 2010

does TriviaGame have a TriviaQuestion reference so it can apply the GetImage() method to that reference and retrieve the bitmap?

Participating Frequently
March 23, 2010

You can definitely pass a bitmap between classes, I do it all the time.

OK, this is going to sound like a stupid question, but is 'bitmap' declared as a class variable inTriviaQuestion.as? Because if not, that's why you would get a null value returned. If it is a class variable, and 'loader' is also, try this instead:

 public function ImageLoaded(e:Event):void
        {
            bitmap =
Bitmap(loader.content);
        }

as per the example found here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html

If you're not manipulating the bitmap, why not just return it instead of copying it?