By Zevan | November 4, 2009
Are you working on something right now that makes use of interfaces? Copy and paste an interface into the comments of this post (no need for code tags or anything)....
Actionscript:
-
package app{
-
-
import flash.events.IEventDispatcher;
-
-
public interface IBaseModel extends IEventDispatcher{
-
-
function set decimal(val:int):void;
-
-
function get decimal():int ;
-
-
function get stringValue():String;
-
-
function get errorMessage():String;
-
-
function set base(val:int):void;
-
-
function get base():int;
-
-
function startCounting(interval:Number, changeBy:Number):void;
-
-
function stopCounting():void;
-
}
-
}
Posted in OOP, misc | Also tagged actionscript, as3 |
By Zevan | October 29, 2009
Actionscript:
-
[SWF(width = 100, height = 100)]
-
var circle:Shape = new Shape();
-
with(circle.graphics) beginFill(0x000000), drawCircle(20,20,20);
-
-
var currFrame:Frame;
-
-
// populate the linked list
-
generateAnimation();
-
-
var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);
-
addChild(new Bitmap(canvas));
-
var loc:Point = new Point(20, 20);
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
// clear the canvas
-
canvas.fillRect(canvas.rect, 0x000000);
-
// draw the current frame
-
canvas.copyPixels(currFrame.bitmap, currFrame.bitmap.rect, loc, null, null, true);
-
// get the next frame of the animation
-
currFrame = currFrame.next;
-
}
-
-
// generate and capture 40 bitmaps by altering the colorTransform of the circle shape
-
function generateAnimation():void{
-
var channel:uint = 0;
-
var ct:ColorTransform = new ColorTransform();
-
var increase:Boolean = true;
-
var firstFrame:Frame;
-
var pFrame:Frame;
-
for (var i:int = 0; i<40; i++){
-
if (increase){
-
channel += 10;
-
if (channel == 200){
-
increase = false;
-
}
-
}else{
-
channel -= 10;
-
}
-
ct.color = channel <<16 | channel <<8 | channel;
-
circle.transform.colorTransform = ct;
-
// populate linked list
-
currFrame = capture(circle);
-
if (pFrame){
-
pFrame.next = currFrame;
-
}
-
if (i == 0){
-
firstFrame = currFrame;
-
}
-
pFrame = currFrame;
-
}
-
// close the list
-
currFrame.next = firstFrame;
-
currFrame = firstFrame;
-
}
-
-
// create the Frame instance and draw the circle to it
-
// preserving the colorTransform information
-
function capture(target:Shape):Frame{
-
var frame:Frame = new Frame();
-
frame.bitmap = new BitmapData(target.width, target.height, true, 0x00000000);
-
frame.bitmap.draw(target, null, target.transform.colorTransform);
-
return frame;
-
}
Requires this little Frame class
Actionscript:
-
package {
-
import flash.display.*;
-
final public class Frame{
-
public var bitmap:BitmapData;
-
public var next:Frame;
-
}
-
}
This is a small test I did today to see how easy it would be to use a circular linked list to loop an animation of bitmaps. I did this because I was thinking about using some animated sprites in conjunction with Utils3D.projectVectors() to do an orthographic 3D demo with lots of animating sprites. In the past I've had up to 7,000 animated sprites running nicely using arrays and copyPixels... figured it would be interesting to try and do the same with a circular linked list.
When compiled, this test simply draws a circle that fades from black to gray and back again... Pretty boring, but I threw it up over at wonderfl anyway... check it out.
I recently saw a few tweets (forget who from) about using the final keyword on linked list nodes... haven't tested it myself but it's supposed to be faster...
Actionscript:
-
/*
-
* Petri Leskinen, Finland
-
* 25 October 2009
-
* pixelero.wordpress.com
-
*
-
* Actionscript 3.0, Flash CS3, Flash Player 10
-
*
-
* threeColorTriangle
-
* draws a triangle with a gradient fill of three colors for each vertex
-
* using graphics.drawTriangles and a BitmapData of size 2x2
-
*/
-
function threeColorTriangle(
-
point0:Point, color0:uint,
-
point1:Point, color1:uint,
-
point2:Point, color2:uint):void {
-
-
// create a bitmap of size 2x2 pixels
-
var bmd:BitmapData = new BitmapData(2,2,true);
-
// copy colors to bitmap
-
// the fourth color is average of color1 and color2
-
bmd.setVector(bmd.rect,
-
Vector.<uint>([color0,color1,color2,
-
(color1+color2)>>1
-
]));
-
-
// draw triangle
-
this.graphics.beginBitmapFill(bmd,null,false,true /* =smooth */ );
-
this.graphics.drawTriangles(
-
// x,y -coordinates
-
Vector.<Number>([
-
point0.x,point0.y,
-
point1.x,point1.y,
-
point2.x,point2.y]),
-
// indices
-
Vector.<int>([0,1,2]),
-
// texture coordinates
-
Vector.<Number>([0,0, 1,0, 0,1])
-
);
-
}
-
-
-
// demo, let's draw some of these on the stage
-
randomize();
-
function randomize():void {
-
this.graphics.clear();
-
-
for (var i:int = 0;i<128;i++) {
-
// pick some random colors
-
var color0:uint = 0xFFFFFFFF;
-
var color1:uint = 0xFFFFFF*Math.random() | 0xFF000000;
-
var color2:uint = 0xFFFFFF*Math.random() | 0xFF000000;
-
-
// random points
-
var point0:Point = new Point(Math.random()*stage.stageWidth,
-
Math.random()*stage.stageHeight);
-
var point1:Point = new Point(point0.x+200*(Math.random()-Math.random()),
-
point0.y+200*(Math.random()-Math.random()));
-
var point2:Point = new Point(point0.x+200*(Math.random()-Math.random()),
-
point0.y+200*(Math.random()-Math.random()));
-
-
threeColorTriangle(point0, color0, point1, color1, point2, color2);
-
}
-
}
This snippet is by Petri Leskinen (pixelero). It draws a triangle with a gradient fill of three colors for each vertex using Graphics.drawTriangles and a BitmapData of size 2x2. This is the simplest case, it can easily be extended to four colors or maybe to use a larger 3x3, 4x4 etc... bitmap.
Here's a still...
By Zevan | October 20, 2009
Actionscript:
-
[SWF(width = 500, height = 500)]
-
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.defaultTextFormat = new TextFormat("_sans", 4);
-
txt.width = stage.stageWidth;
-
txt.height = stage.stageHeight+4;
-
txt.z = -1;
-
txt.x = stage.stageWidth
-
txt.rotation = 90;
-
var count:int = 0;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void{
-
count++;
-
txt.appendText(triangular(count).toString(2) + "\n");
-
txt.scrollV= txt.maxScrollV;
-
}
-
function triangular(n:int):int{
-
return (n * (n + 1)) / 2;
-
}
Calculate some triangular numbers... (n * (n + 1)) / 2....
Check out the swf on wonderfl
Posted in Math, misc | Also tagged actionscript, as3 |