Copy link to clipboard
Copied
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!!!
try:
...
//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;
public function Main():vo
Copy link to clipboard
Copied
try:
//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;
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.contentLoaderInfo.addEventListener(Event.COMPLETE,resizeada);
picture.load(new URLRequest("FIN1.jpg"));
}
private function resizeada(e:Event):void{
_bg=new OffsetResize(picture,OffsetResize.MIN);
stage.addChildAt(_bg,0);
_bg.stage.align = StageAlign.TOP_LEFT;
_bg.offsetType=_bg.offsetType==OffsetResize.MAX?OffsetResize.MIN:Offs etResize.MAX;
}
}
}
Copy link to clipboard
Copied
I am getting 2 errors now:
1086 Syntax error: expecting semicolon before etResize
1084 Syntax error: expecting right brace before semicolon
Both of them in the last line of the resizeada function
Copy link to clipboard
Copied
never mind, it was a mispelling error...
Copy link to clipboard
Copied
Worked perfectly... I just had to change _bg=new OffsetResize(picture,OffsetResize.MIN); from the resizeada function into the init one, since picture was not recognized the other way around.
Thanks a lot K!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now