Skip to content

Instantly share code, notes, and snippets.

@ain
Created February 23, 2012 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ain/1894840 to your computer and use it in GitHub Desktop.
Save ain/1894840 to your computer and use it in GitHub Desktop.
AS3 visible bounds of DisplayObject
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.geom.Rectangle;
import flash.geom.PerspectiveProjection;
import flash.display.BitmapData;
public class PPPlane extends Sprite {
public function PPPlane():void {
var Plane:Sprite = drawPlane();
Plane.x = (stage.stageWidth - Plane.width) * 0.5;
Plane.y = (stage.stageHeight - Plane.height) * 0.5;
Plane.z = 100;
Plane.rotationY = 70;
Plane.rotationX = 20;
Plane.rotationZ = 30;
addPerspective();
addChild(Plane);
trace("Plane size: "+Plane.width+"x"+Plane.height);
var bounds:Rectangle = Plane.getBounds(stage);
trace("Plane bounds: "+bounds);
var visibleBounds:Rectangle = visibleBitmapBounds();
trace("Visible, on-screen bounds of the Plane: "+visibleBounds);
}
private function addPerspective():void {
var pp:PerspectiveProjection = new PerspectiveProjection();
pp.fieldOfView = 120;
this.transform.perspectiveProjection = pp;
}
private function drawPlane():Sprite {
var Plane:Sprite = new Sprite();
Plane.graphics.beginFill(0x0000FF);
Plane.graphics.drawRect(0, 0, 200, 200);
Plane.graphics.endFill();
return Plane;
}
private function visibleBitmapBounds():Rectangle {
var w:int = stage.stageWidth;
var h:int = stage.stageHeight;
var bitmapData:BitmapData = new BitmapData(w, h, true, 0);
bitmapData.draw(this)
var bounds:Rectangle = bitmapData.getColorBoundsRect(0xFF000000, 0x00000000, false);
bitmapData.dispose();
return bounds;
}
}
}
@ain
Copy link
Author

ain commented Feb 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment