Actionscript:
-
// make a complex poly and position it randomly
-
var poly:Sprite = Sprite(addChild(new Sprite()));
-
poly.graphics.lineStyle(3, 0xFF0000);
-
poly.graphics.beginFill(0x00FF00);
-
for (var i:int = 0; i<10; i++) {
-
poly.graphics.lineTo(Math.random()*100 - 50, Math.random()*100 - 50);
-
}
-
poly.x=Math.random()*stage.stageWidth;
-
poly.y=Math.random()*stage.stageHeight;
-
-
// get bound information:
-
-
// is in pixels (whole numbers)
-
trace("pixelBounds: ", poly.transform.pixelBounds);
-
// doesn't include stroke width
-
trace("getBounds: ", poly.getBounds(this));
-
// includes stroke width
-
trace("getRect: ", poly.getRect(this));
Three different ways to get the boundaries of a DisplayObject.
Posted in DisplayObject | Also tagged actionscript |
Actionscript:
-
var size:Number = 800;
-
var canvas:BitmapData = new BitmapData(size,size,false, 0x000000);
-
addChild(new Bitmap(canvas, "auto", true));
-
-
scaleX = scaleY = .5;
-
var pix:Number = size * size;
-
var scale:Number = 1/(size/3);
-
-
for (var i:Number = 0; i<pix; i++){
-
var xp:Number = (i % size);
-
var yp:Number = int(i / size);
-
var xt:Number = xp * scale;
-
var yt:Number = yp * scale;
-
var ca:Number = (Math.abs(Math.tan(yt) * Math.pow(Math.sin(xt),3)) * 100 ) % 155;
-
var cb:Number = (Math.abs(Math.tan(xt) * Math.pow(Math.sin(yt),3)) * 100) % 155;
-
ca|= cb;
-
canvas.setPixel(xp, yp, ca <<16 | ca <<8 | ca);
-
}
Another messy code snippet that I e-mailed to myself at some point...
Try replacing line 16 with some of these variations:
ca &= cb;
ca += cb;
ca -= cb;
ca ^= cb;
ca %= cb
I finally finished the QuickBox2D documentation. Check it out here.
You can download QuickBox2D and see a few example code snippets on the QuickBox2D page. Should have a tutorial on that page in the next couple of days.
Actionscript:
-
function create(obj:Class, props:Object):*{
-
var o:* = new obj();
-
for (var p:String in props){
-
o[p] = props[p];
-
}
-
return o;
-
}
-
-
// test out the function
-
-
var txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:"hello there", textColor:0xFF0000, defaultTextFormat:new TextFormat("_sans", 20)});
-
addChild(txt);
-
-
var s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5})));
-
-
with (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40);
-
-
var blur:BlurFilter = create(BlurFilter, {blurX:2, blurY:8, quality:1});
-
-
s.filters = [blur];
This snippet shows a function called create() that takes two arguments. The first argument is the name of a class to instantiate. The second is an Object with a list of properties to set on a newly created instance of the class (referenced in the first argument).
This could be particularly useful for TextFields which for some reason have no arguments in their constructor.
This will currently only work for classes that have either all optional constructor arguments or no constructor arguments.