Can you pass a bitmap between two custom classes?
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;
}
