Cant place item according to device's screen height - actionscript-3

I am trying to add a child at the bottom of the screen.
I am using stage.stageHeight, but it doesn't seem to be able to get the height of the device properly.
It is leaving a very wide gap between the child (s6) and the bottom of the screen.
Code:
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void{
addChild(s6);
s6.y = stage.stageHeight - s6.height;
s6.width = width - 20;
s6.x = stage.x+10;
}
Edit:
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void{
addChild(s7);
s7.width = stage.stageWidth;
stage.align = StageAlign.TOP_LEFT;
addChild(s6);
trace(stage.fullScreenWidth);
trace(stage.fullScreenHeight);
s6.y = stage.fullScreenHeight - s6.height;
s6.width = width - 20;
s6.x = stage.x+10;
s6.account.addEventListener(MouseEvent.CLICK, fl_ClickToGoTomyaccount);
s6.automatch.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAAA);
s6.search.addEventListener(MouseEvent.CLICK, fl_ClickToGoTosearchpage);
s7.myaccount.addEventListener(MouseEvent.CLICK, fl_ClickToGoTomyaccount);
s7.autmatch.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAAA);
s7.sssearch.addEventListener(MouseEvent.CLICK, fl_ClickToGoTosearchpage);
}

you can use the screen resolution property and use them in your code.
I have done one example like this and it varies on different screen resolutions.
var screenWidth:int = Capabilities.screenResolutionX;
var screenHeight:int = Capabilities.screenResolutionY;
this.width= screenWidth - (screenWidth / 2);
this.height=screenHeight - 200;
Hope it will work for you

I think you should use fullScreenHeight instead of stageHeight: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#fullScreenHeight
This is very true especially when working with mobile devices. stageHeight and stageWidth are not very reliable then.
You may also want to wait for RESIZE event which will occur shot after your app initialized, and then you can again use stageHeight instead.

Related

Keep item at same monitor position regardless of window size/position

I am trying to create a game with fullscreen.
When I add an object to the stage in full screen mode, I would like it to stay at the same coordinates (for example 1000 pixels) relative to the monitor when I exit fullscreen mode.
How can I make the object move to the same location when leaving fullscreen mode?
To get your started:
Something along these lines is what you'll need to do:
stage.align = StageAlign.TOP_LEFT; //you'll need to running a top-left no-scale swf for this to work
stage.scaleMode = StageScaleMode.NO_SCALE;
var itemPoint:Point = new Point(150,150); //the point on the monitor the object should reside
//call this anytime the item needs to be redrawn (eg when the window changes size or position)
function updatePos(e:Event = null){
//We need to also account for the chrome of the window
var windowMargin:Number = (stage.nativeWindow.bounds.width - stage.stageWidth) * .5; //this is the margin or padding that between the window and the content of the window
var windowBarHeight:Number = stage.nativeWindow.bounds.height - stage.stageHeight - windowMargin; //we have to assume equal margin on the left/right/bottom of the window
item.x = itemPoint.x - stage.nativeWindow.x - windowMargin;
item.y = itemPoint.y - stage.nativeWindow.y - windowBarHeight;
}
stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVE, updatePos); //we need to listen for changes in the window position
stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, updatePos); //and changes in the window size
//a click listener to test with
stage.addEventListener(MouseEvent.CLICK, function(e:Event):void {
if(stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE){
stage.displayState = StageDisplayState.NORMAL;
}else{
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
});
updatePos();

Zooming and translate in as3 AIR Android

I am developing an android app with Adobe Flash cs6 and actionscript3. I have multiple moviclips at various locations on stage. Now I need to add a zoom feature that will zoom all movieclips as one movieclip. I cannot combine all movieclips together into one movieclip. My zoom feature works but it does not translate the movieclips to a new position. (Meaning they zoom at their original positions only) How can I accomplish this? Following is my zooming code :
/* ZOOM FEATURE */
Multitouch.inputMode = MultitouchInputMode.GESTURE;
zoomer.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
function onZoom (e:TransformGestureEvent):void{
mc1.scaleX *= (e.scaleX+e.scaleY)/2;
mc1.scaleY *= (e.scaleX+e.scaleY)/2;
mc2.scaleX *= (e.scaleX+e.scaleY)/2;
mc2.scaleY *= (e.scaleX+e.scaleY)/2;
mc3.scaleX *= (e.scaleX+e.scaleY)/2;
mc3.scaleY *= (e.scaleX+e.scaleY)/2;
}
Not the cleanest code I've written, but it should do the trick. scaleInPlace() accepts 4 arguments, 2 required, two optional:
obj: the display object you want to scale
scaleFactor: just as it sounds, how big/small do you want it?
fromX: the X coordinate where you want to scale from. Left, middle, right? If omitted, it operates from the middle of the compiled stage size.
fromY: same as the previous, but for top, middle, bottom.
This will preserve your DisplayList hierarchy while allowing you to scale to your heart's desire. If you had a lot of objects to scale, I'd probably parent all of them to the container first, and then run the scale and reparenting operations. While it works, this is the reason I feel it's not the "cleanest" solution. In my own classes, I've written a purely mathematical solution that doesn't include adding/removing DisplayObjects, but it's fairly tied up in my own classes I couldn't pull it out here and have it work.
Cheers!
// Let's scale myObject to 50% of its original size.
scaleInPlace(myObject, 0.5);
function scaleInPlace(obj:DisplayObject, scaleFactor:Number, fromX:Number = NaN, fromY:Number = NaN):void {
// If no coordinates from where to scale the image are provided, start at the middle of the screen
if (isNaN(fromX)) { fromX = loaderInfo.width/2; }
if (isNaN(fromY)) { fromY = loaderInfo.height/2; }
var father:DisplayObjectContainer = obj.parent;
var rect:Rectangle; // Coordinates for tracking our object
var index:int = getChildIndex(obj) // Where this object should go when we put it back
// Create the container
var container:Sprite = new Sprite();
father.addChild(container);
// Place the origin of the scale operation
container.x = fromX;
container.y = fromY;
// Get the coordinates of our object relative to our container
rect = obj.getRect(container);
// Parent and move into place
container.addChild(obj);
obj.x = rect.x;
obj.y = rect.y;
// Scale
container.scaleX = container.scaleY = scaleFactor;
// Get the coordinates and size of our scaled object relative to our father
rect = obj.getRect(father);
// Cleanup the display list
father.addChildAt(obj, index);
father.removeChild(container)
// Apply the new coordinates and size
obj.x = rect.x;
obj.y = rect.y;
obj.width = rect.width;
obj.height = rect.height;
}

as3 Using resized information to scale my movieclip

I am working on an air mobile app in Flash CS5.5 and am fairly new at this.
So here is my problem: I placed all of my content inside of a movieClip called base. I have written code to resize my movieClip based on the device it is running on and the devices orientation. That part works fine, the problem is when I tried to create a zoom function.
The base movieClip scales but I am having trouble getting the minimum boundary to work so that my content never scales less than the width of the device. I believe it is referring to the original base width instead of the resized base width. Any ideas on how to call on the new base width after the resize function? Refer to my code below and thank you in advance for any help :)
import flash.geom.Rectangle;
stop();
base.play();
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var guiSize:Rectangle = new Rectangle(0, 0, 480, 800);
var appScale:Number = 1;
stage.addEventListener(Event.RESIZE, handleResize);
function handleResize(e:Event):void {
var deviceSize:Rectangle = new Rectangle(0, 0, stage.stageWidth,
stage.stageHeight);
appScale = deviceSize.width / guiSize.width;
base.width = appScale * guiSize.width;
base.height = appScale * guiSize.height;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
base.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
function onZoom (e:TransformGestureEvent):void{
if (base.scaleX * e.scaleX < 1)
{}
else if (base.scaleX * e.scaleX > 5)
{}
else {
base.scaleX *= (e.scaleX+e.scaleY)/2;
base.scaleY *= (e.scaleX+e.scaleY)/2;}
}
}
After playing around with the code and trying many different solutions I came up with the code below and it seems to work fine. The problem was that my base movieClip was scaled up after the resize function which threw off the base.scaleX variable for my zoom function. This is because base.scaleX equals more than one. You can see the problem here:
if (base.scaleX * e.scaleX < 1)
The code was causing my base movieClip to scale down less than my device window. My solution is to use the new base.width instead to determine minimum and maximum scaleing for my zoom function. As noted below.
function onZoom (e:TransformGestureEvent):void{
if (base.width * e.scaleX < deviceSize.width )
{}
else if (base.width * e.scaleX > deviceSize.width * 2 )
{}
else {
base.scaleX *= (e.scaleX+e.scaleY)/2;
base.scaleY *= (e.scaleX+e.scaleY)/2;}
}
}

AS3 MovieClip resize

I am trying to find AS3 code for a MovieClip on my stage.
My stage resizes proportionally on different screens (different size monitors), but the MC gets too big for smaller screen laptops and some parts of it get cut off. Would appreciate for any help
You could restrict any scaling, and handle the sizing yourself:
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
Have a look at the docs for the scale mode and align properties available.
Put this before anything else. This code find's the scale size from your original layout to the re-sized. (NOTE: It's best if you use this when Event.RESIZE is happening)
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var guiSize:Rectangle = new Rectangle(0, 0, 1024, 600); //original stage size, substitute this with your orginal size
var deviceSize:Rectangle = new Rectangle(0, 0,
Math.max(stage.fullScreenWidth, stage.fullScreenHeight),
Math.min(stage.fullScreenWidth, stage.fullScreenHeight));
var appScale:Number = 1;
if ((deviceSize.width/deviceSize.height) > (guiSize.width/guiSize.height)) {
appScale = deviceSize.height / guiSize.height;
}
else {
appScale = deviceSize.width / guiSize.width;
}
Than, use appScale to scale every MovieClip/Sprite you have, e.g. _mc.scaleX = _mc.scaleY = appScale.
Using this way, every time the stage get re-sized, the right and down border are moved. That means if you want your footer to be always 50px from bottom, you should use something like :
_footer.y = stage.stageHeight - (50 * appCale);

how to set width and height for stage during resize event in as3

I want to set width and height of the stage during resize of that swf..
I do it in resize event handling..but doesn't work..any other way to achieve this?
stage.addEventListener (Event.RESIZE, resizeListener);
function resizeListener (e:Event):void {
stage.stageWidth=500;
stage.stageHeight=300;
}
Thanks
by the sounds of it you just need
stage.align = "";
stage.scaleMode = StageScaleMode.NO_SCALE;
this will keep the content the same size no matter how much you stretch the window
Updated:
you need is to compare the ratios of the width and height scales of the content and the target. To make the loaded image fit within the area, scaling so that everything is inside you can do something like this:
var scale:Number = Math.min( _holder.width / _loader.content.width,
_holder.height / _loader.content.height );
_loader.content.scaleX = _loader.content.scaleY = scale;
This will make sure that you can see everything. If you change Math.min to Math.max, you will get a different result if the dimensions don't match.
public function loaderComplete(event:Event):void
{
var content:MovieClip = MovieClip(event.currentTarget.content );
//the dimensions of the external SWF
var _width:int = content.width;
var _height:int = content.height;
// you have several options here , assuming you have a container Sprite
// you can directly set to the dimensions of your container, if any
if( container.width < _width )
_width = container.width // and do the same for height
// or you could scale your content to fit , here's a simple version
// but you can check the height too or keep checking both value
// until it fits
if( container.width < _width )
{
var scale:Number = container.width / _width;
content.scaleX = content.scaleY = scale;
}
}
This answer gives better understanding of previous code given by swati singh.See this link
How to resize an external SWF to fit into a container?
Thanks to all