By Zevan | January 2, 2009
Actionscript:
-
var centerX:Number=stage.stageWidth/2;
-
var centerY:Number=stage.stageHeight/2;
-
// 1 to 2 ratio
-
// try others 1 / 1.5 etc...
-
var theta:Number = Math.atan(1 / 2);
-
-
var cosX:Number=Math.cos(theta);
-
var sinX:Number=Math.sin(theta);
-
var pnt:Point = new Point();
-
-
function iso3D(x:Number, y:Number, z:Number):Point {
-
pnt.x = centerX + (x-z) * cosX;
-
pnt.y = centerY - (x+z) * sinX - y;
-
return pnt;
-
}
-
-
var p:Point = iso3D(0,0,0);
-
-
graphics.beginFill(0x000000);
-
graphics.drawCircle(p.x, p.y, 2);
-
-
// x axis positive
-
trace("x = red");
-
for (var i:int = 1; i<10; i++){
-
graphics.beginFill(0xFF0000);
-
p = iso3D(i*10, 0, 0);
-
graphics.drawCircle(p.x, p.y, 2);
-
}
-
-
// y axis positive
-
trace("y = green");
-
for (i= 1; i<10; i++){
-
graphics.beginFill(0x00FF00);
-
p = iso3D(0, i * 10, 0);
-
graphics.drawCircle(p.x, p.y, 2);
-
}
-
-
// z axis positive
-
trace("z = blue");
-
for (i= 1; i<10; i++){
-
graphics.beginFill(0x0000FF);
-
p = iso3D(0, 0, i * 10);
-
graphics.drawCircle(p.x, p.y, 2);
-
}
The above code demos a conversion from isometric coordinates to cartesian coordinates. It draws out part of the x, y and z axis using the iso3D() function.
I've always faked isometric conversion by just tweaking numbers. A few years ago I created some strange isometric engines (here's another example). The other day when I wrote the isometric voxels snippet I just created the iso3D() function with a little trial and error. As you'll see it's not exactly the same as the one here. This new conversion is the result of some googling. The updated conversion is pretty close to the one that I wrote for the voxel post, but has one less multiplication... and a clearer place to control the scale ratio ...
By Zevan | January 1, 2009
Actionscript:
-
var circle:Shape = Shape(addChild(new Shape()));
-
with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
-
-
var point:Point = new Point();
-
var trans:Matrix = new Matrix();
-
trans.rotate(Math.PI);
-
trans.scale(.5, .5);
-
trans.tx = stage.stageWidth / 2;
-
trans.ty = stage.stageHeight / 2;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
point.x = mouseX - trans.tx;
-
point.y = mouseY - trans.ty;
-
-
point = trans.transformPoint(point);
-
-
circle.x = point.x;
-
circle.y = point.y;
-
}
If you don't feel like rolling your own transformations Matrix.transformPoint() is a very powerful method. It simply applies the tranformations of a given matrix to a Point. The above example scales rotates and translates a point which is then used to position a circle Shape.
Matrix.transformPoint() is well suited for creating orbiting behavior:
Actionscript:
-
var circle:Shape = Shape(addChild(new Shape()));
-
with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
-
-
var point:Point = new Point(100,0);
-
var trans:Matrix = new Matrix();
-
trans.rotate(.1);
-
trans.scale(.99,.99);
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
-
point = trans.transformPoint(point);
-
-
circle.x = point.x + stage.stageWidth / 2;
-
circle.y = point.y + stage.stageHeight / 2;
-
}
This code will cause the circle Shape to move in a spiral formation.
By Zevan | December 31, 2008
Actionscript:
-
var canvas:BitmapData = new BitmapData(400,400,false, 0xCC0000);
-
addChild(new Bitmap(canvas));
-
-
var s:Shape = new Shape();
-
s.graphics.beginFill(0x00CCCC);
-
s.graphics.moveTo(Math.random()*400,Math.random()*400);
-
for(var i:int = 0; i<10; i++){
-
s.graphics.lineTo(Math.random()*400,Math.random()*400);
-
}
-
canvas.draw(s);
-
-
stage.addEventListener(MouseEvent.CLICK, onDown);
-
function onDown(evt:MouseEvent):void {
-
canvas.floodFill(mouseX, mouseY, 0x000000);
-
}
floodFill() is basically the paint bucket (fill) tool in any bitmap drawing program. The above example draws an arbitrary blue polygon to a red background. Click anywhere on the resulting image to see floodFill() in action.
floodFill() takes 3 arguments... x, y and color.
By Zevan | December 30, 2008
Actionscript:
-
var backgroundColor:uint = 0xEFEFEF;
-
var borderColor:uint = 0xFF0000;
-
var buttonOverColor:uint = 0x0000FF;
-
var buttonOutColor:uint = 0x00CCCC;
-
-
var uiColors:XML = <ui>
-
<default color="0xCCCCCC" />
-
<background color = { backgroundColor } />
-
-
<!-- note hexidecimal formatting code -->
-
<border color={ ("0x" + borderColor.toString(16)) } />
-
-
<button overColor={ buttonOverColor} outColor={ buttonOutColor } />
-
</ui>
-
-
trace(uiColors.toXMLString());
-
-
/*outputs:
-
<ui>
-
<default color="0xCCCCCC"/>
-
<background color="15724527"/>
-
<border color="0xff0000"/>
-
<button overColor="255" outColor="52428"/>
-
</ui>
-
*/
The first time I needed to use an ActionScript variable within inline XML I was stumped. I couldn't figure it out and I wasn't able to easily find it on google. I eventually found it somewhere (don't remember where... possibly hidden away in the docs).
Now a search for "insert actionscript variables into e4x" on google gives plenty of results. But I figure it's worth a post.
I use actionscript to generate XML all the time so this comes in handy. I also store color values in automatically generated XML all the time. If I'm feeling organized I'll use something like what you see on line 10:
Actionscript:
-
<border color={ ("0x" + borderColor.toString(16)) } />
If you look at the output you'll see this formats the uint so that it's readable as a hex number. By default (as you can see in the output) uints will show up in decimal notation. This really doesn't make any difference if you don't care about XML readability ... or if you just don't care about the readability of the colors stored in your XML.....