By Zevan | November 30, 2008
Actionscript:
-
stage.frameRate = 30;
-
-
for (var i:int = 0; i<100; i++){
-
makeBoxSegment(200, 200 - i, i * 2);
-
}
-
-
function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
-
var isoBox:Sprite = Sprite(addChild(new Sprite()));
-
with (isoBox) scaleY = .5, y = yp, x = xp;
-
var box:Shape = Shape(isoBox.addChild(new Shape()));
-
box.rotation = 45;
-
with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
-
isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
-
return isoBox;
-
}
-
-
function onRotate(evt:Event):void {
-
evt.currentTarget.getChildAt(0).rotation = mouseX;
-
}
An isometric box that rotates with the mouseX.
By Zevan | November 19, 2008
Actionscript:
-
// display object target, value - "center" or "upperLeft"
-
function setRegistration(dsp:DisplayObjectContainer, v:String):void {
-
var i:int;
-
var child:DisplayObject;
-
var b:Rectangle=getBounds(dsp);
-
for (i = 0; i <dsp.numChildren; i++) {
-
child=dsp.getChildAt(i);
-
child.x+=b.x*-1;
-
child.y+=b.y*-1;
-
}
-
if (v=="center") {
-
dsp.x+=dsp.width/2;
-
dsp.y+=dsp.height/2;
-
for (i = 0; i <dsp.numChildren; i++) {
-
child=dsp.getChildAt(i);
-
child.x-=dsp.width/2;
-
child.y-=dsp.height/2;
-
}
-
} else if (v == "upperLeft") {
-
if (dsp.parent) {
-
b=getBounds(dsp.parent);
-
dsp.x=b.left;
-
dsp.y=b.top;
-
}
-
}
-
}
-
-
// example:
-
// (make a MovieClip and fill it with, text, shapes etc...)
-
-
setRegistration(clip, "center");
-
// registration(clip, "upperLeft");
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
clip.rotation+=1;
-
}
Teaching beginner flash... I've noticed people always get confused about the registration point. They think they should be able to move it with the IDE or ActionScript. I always say "You can't change the registration point, you can only change the position of graphical elements in relationship to it." Anyway, as a sort of joke one day after class I wrote the above function... it changes the registration of any DisplayObjectContainer to either "center" or "upperLeft".
To test this code out, fill a MovieClip with a bunch of different things (text, graphics, scribbles)... give it a strange registration point and then run the above code on it. You'll be able to tell that the registration has changed by the way the clip rotates. Although I haven't been able to break this function, I think it's possible that it doesn't work under all circumstances.
Using reparenting is a more elegant solution to this issue. Maybe I'll post that in the next couple days.
By Zevan | November 18, 2008
Actionscript:
-
var box:Shape = Shape(addChild(new Shape()));
-
with (box.graphics) beginFill(0x006666), drawRect(0,0,50,50);
-
box.x = box.y = 100;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
-
var m:Matrix = box.transform.matrix;
-
// skew on the X
-
m.c = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth;
-
-
// skew on the Y
-
// m.b = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth
-
-
box.transform.matrix = m
-
}
This skews a box Shape using the c and b properties of the transformation matrix. Note that these values don't match those in the IDE's transform window. This is good further reading if your interested in this topic.
Also posted in motion | Tagged actionscript, flash, matrices |
By Zevan | November 3, 2008
Actionscript:
-
for each(var prop:String in describeType(DisplayObject).factory.accessor.@name){
-
trace(prop);
-
}
-
/* outputs
-
scaleY
-
mouseX
-
mouseY
-
mask
-
rotation
-
alpha
-
transform
-
blendMode
-
x
-
root
-
loaderInfo
-
width
-
z
-
rotationX
-
scale9Grid
-
filters
-
rotationY
-
y
-
stage
-
scaleZ
-
parent
-
accessibilityProperties
-
scrollRect
-
rotationZ
-
height
-
name
-
opaqueBackground
-
blendShader
-
cacheAsBitmap
-
visible
-
scaleX
-
*/
I First saw describeType() at senocular's AS3 Tip of the Day on Kirupa.