Copy link to clipboard
Copied
Hello,
I created an ActionScript project in Flash Builder 4.7... It runs on my desktop with Adobe Air, how can I add a background image?
Thank you in advance for your help,
Carlos
Copy link to clipboard
Copied
I found a solution, here is the code just in case:
Create the folders "helpers\utils" and save the code below as FullScreenBackground.as
Or remove helpers.utils from the code and place anywhere you want.
to use it:
package
{
import flash.display.Sprite;
import helpers.utils.FullScreenBackground;
public class AS3BackgroundTest extends Sprite
{
private var appBackground:FullScreenBackground = new FullScreenBackground("images/RedBG.png", 800, 600);
public function AS3BackgroundTest()
{
stage.addChild(appBackground);
}
}
}
package helpers.utils
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
public class FullScreenBackground extends Sprite
{
private var ldr:Loader;
private var ldrReq:URLRequest;
private var bmp:Bitmap;
private var minW:Number;
private var minH:Number;
private var stageW:Number;
private var stageH:Number;
private var scaler:Number;
public function FullScreenBackground(_imageURL:String, _minW:Number, _minH:Number)
{
super();
minW = _minW;
minH = _minH;
ldr = new Loader;
ldrReq = new URLRequest(_imageURL);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onBGimgLoaded);
ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
this.addEventListener(Event.ADDED_TO_STAGE, onBGaddedToStage);
}
protected function onIOError(event:IOErrorEvent):void
{
trace("Error loading background image: " + event);
}
protected function onBGimgLoaded(event:Event):void
{
bmp = Bitmap(ldr.content);
bmp.smoothing = true;
stage.addChild(bmp);
trace("onBGimgLoaded");
resizeImage();
}
private function resizeImage():void
{
stageW = stage.stageWidth;
stageH = stage.stageHeight;
trace(stageW);
trace(stageH);
if (stageH / stageW > this.height / this.width) {
scaler = this.width / this.height;
this.width = stageH * scaler;
this.height = stageH;
} else {
scaler = this.height / this.width;
this.height = stageW * scaler;
this.width = stageW;
}
this.x = (stageW / 2) - (this.width / 2);
this.y = 0;
}
protected function onBGaddedToStage(event:Event):void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onStageResize);
ldr.load(ldrReq);
}
protected function onStageResize(event:Event):void
{
resizeImage();
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now