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.
I am attaching the full AS files/SWF/etc in a zip file, but here are the parts I believe are important.
In TriviaQuestion.as, loading the image:
[LEFT]
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();
}
[/LEFT]
In TriviaQuestion.as, setting the class data:
public function SetImageProps( url:String, attr:String ):void
[LEFT] {
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;
}[/LEFT]
[LEFT]
[/LEFT]
In TriviaGame.as MakeQuestionSprite()
[COLOR=#993300]private[/COLOR] [COLOR=#993300]function[/COLOR] MakeQuestionSprite[COLOR=#000000]([/COLOR] s:Shape [COLOR=#000000])[/COLOR]:[COLOR=#993300]void[/COLOR]
[LEFT] [COLOR=#000000]{[/COLOR]
[COLOR=#993300]if[/COLOR] [COLOR=#000000]([/COLOR] [COLOR=#0000ff]"image"[/COLOR] [COLOR=#993300]in[/COLOR] [COLOR=#993300]xml[/COLOR].[COLOR=#000000]item[/COLOR][COLOR=#000000][[/COLOR]currentQuestion[COLOR=#000000]][/COLOR] [COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#993300]var[/COLOR] myPos:[COLOR=#993300]String[/COLOR] = questions[COLOR=#000000][[/COLOR]currentQuestion[COLOR=#000000]][/COLOR].[COLOR=#000000]GetPosition[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#993300]var[/COLOR] myImg:Bitmap = questions[COLOR=#000000][[/COLOR]currentQuestion[COLOR=#000000]][/COLOR].[COLOR=#000000]GetImage[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
errorText.[COLOR=#993300]text[/COLOR] = [COLOR=#0000ff]"Img: "[/COLOR] + myImg.[COLOR=#993300]width[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#993300]var[/COLOR] myQ:[COLOR=#993300]String[/COLOR] = questions[COLOR=#000000][[/COLOR]currentQuestion[COLOR=#000000]][/COLOR].[COLOR=#000000]GetQuestion[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
questionText = [COLOR=#993300]new[/COLOR] [COLOR=#993300]TextField[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
questionFormat = [COLOR=#993300]new[/COLOR] [COLOR=#993300]TextFormat[/COLOR][COLOR=#000000]([/COLOR] [COLOR=#993300]font[/COLOR], [COLOR=#000000]15[/COLOR], [COLOR=#993300]color[/COLOR], [COLOR=#993300]null[/COLOR], [COLOR=#993300]null[/COLOR], [COLOR=#993300]null[/COLOR], [COLOR=#993300]null[/COLOR], [COLOR=#993300]null[/COLOR], [COLOR=#0000ff]"center"[/COLOR] [COLOR=#000000])[/COLOR];
[COLOR=#993300]if[/COLOR] [COLOR=#000000]([/COLOR] [COLOR=#0000ff]"image"[/COLOR] [COLOR=#993300]in[/COLOR] [COLOR=#993300]xml[/COLOR].[COLOR=#000000]item[/COLOR][COLOR=#000000][[/COLOR]currentQuestion[COLOR=#000000]][/COLOR] [COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
[COLOR=#993300]if[/COLOR] [COLOR=#000000]([/COLOR] myPos == [COLOR=#0000ff]"right"[/COLOR] [COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
myImg.[COLOR=#000000]x[/COLOR] = stageProps.[COLOR=#000000]stageWidth[/COLOR] - margins - myImg.[COLOR=#993300]width[/COLOR];
[COLOR=#f000f0]*//myImg.y = margins + s.y + (s.height / 2);*[/COLOR]
[COLOR=#f000f0]*//myImg.visible = true;*[/COLOR]
[COLOR=#f000f0]*//questionText = CreateText( myQ, questionFormat, margins, myImg.y, (stageProps.stageWidth - margins - myImg.width) );*[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#f000f0]*//questionSprite.addChild( myImg );*[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#993300]else[/COLOR]
[COLOR=#000000]{[/COLOR]
questionText = CreateText[COLOR=#000000]([/COLOR] myQ, questionFormat, margins, [COLOR=#000000]([/COLOR]margins + s.[COLOR=#000000]y[/COLOR] + s.[COLOR=#993300]height[/COLOR] / [COLOR=#000000]2[/COLOR][COLOR=#000000])[/COLOR], [COLOR=#000000]([/COLOR]stageProps.[COLOR=#000000]stageWidth[/COLOR] - margins[COLOR=#000000])[/COLOR] [COLOR=#000000])[/COLOR];
[COLOR=#000000]}[/COLOR]
questionSprite.[COLOR=#000000]addChild[/COLOR][COLOR=#000000]([/COLOR] questionText [COLOR=#000000])[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]
In TriviaQuestion.as, GetImage
public function GetImage ():Bitmap
[LEFT] {
var copy:Bitmap = new Bitmap( bitmap.bitmapData.clone() );
return copy;
}[/LEFT]
The only time I am able to access the bitmap’s info (in order to add it to the display) is inside ImageLoaded().