a question about a resizeable loaded object
Guys...
I was searching in the web on how to make an automatic resizeable screen, so it feets on many devices regardless of their size and I found a tutorial that adds the picture to the Flash library and resizes it depending on the screen size...
Now, I have deleted the library object and changed the "bitmap" var from the tutorial for a "Loader" one, so the picture isnt in the library...
My only problem is that when you try the swf, the picture is anywhere in the screen and only fits on it after you expand or decrease the its size...
So how can the picture be on its right spot in the first time?
(The tutorial had as an event trigger a Mouse Click... I have changed it for a timer event and while my pic was in the library it worked fine, but now I have to resize the windows to make it work)
Here is the code:
//Main Class
package{
import org.display.OffsetResize;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Loader;
import flash.net.URLRequest;
public class Main extends Sprite{
private var _bg:OffsetResize;
private var t:Timer = new Timer(0,0);
public function Main():void{
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
var picture:Loader = new Loader();
picture.load(new URLRequest("FIN1.jpg"));
_bg=new OffsetResize(picture,OffsetResize.MIN);
stage.addChildAt(_bg,0);
_bg.stage.align = StageAlign.TOP_LEFT;
t.addEventListener(TimerEvent.TIMER, resizeada);
t.start();
}
private function resizeada(TimerEvent):void{
_bg.offsetType=_bg.offsetType==OffsetResize.MAX?OffsetResize.MIN:OffsetResize.MAX;
t.stop();
}
}
}
//OffsetResize class
package org.display{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
public class OffsetResize extends Sprite{
public static const MAX:String="max";
public static const MIN:String="min";
private var _offsetType:String;
public function OffsetResize($child:DisplayObject,$offsetType:String="max"):void{
_offsetType=$offsetType;
addChild($child);
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
addEventListener(Event.REMOVED_FROM_STAGE,end);
}
private function init(e:Event=null):void{
stage.addEventListener(Event.RESIZE,stageResize);
stageResize();
}
private function stageResize(e:Event=null):void{
var px:Number=stage.stageWidth/width;
var py:Number=stage.stageHeight/height;
var div:Number=_offsetType=="max"?Math.max(px,py):Math.min(px,py);
width*=div;
height*=div;
x=(stage.stageWidth/2)-(width/2);
y=(stage.stageHeight/2)-(height/2);
}
private function end(e:Event):void{
stage.removeEventListener(Event.RESIZE,stageResize);
}
public function set offsetType(type:String):void{
_offsetType=type;
if(stage) stageResize();
}
public function get offsetType():String{ return _offsetType; }
}
}
Thanks in advance!!!
