By Zevan | August 31, 2011
So here is a javascript inspired snippet. It's also in javascript style with no typing really. To port to javascript would take less than 4 minutes:
Actionscript:
-
// 1D
-
function _(a, b){
-
var c = {};
-
for (var i in b){
-
trace(i);
-
a[i] = b[i];
-
}
-
return a;
-
}
-
-
// douglas crockfords create
-
function create(o){
-
var F = function(){};
-
F.prototype = o;
-
return new F();
-
}
-
-
// 1D
-
function obj(o){
-
for (var i in o){
-
trace(o[i]);
-
}
-
}
-
-
var Mover = {
-
ox : 0, oy : 0,
-
x : 100, y : 100, radius: 10, t : 0, speed : 0.2,
-
run : function(){
-
//namespace Mover;
-
this.x = this.ox + this.radius * Math.cos(this.t);
-
this.y = this.oy + this.radius * Math.sin(this.t);
-
this.t += this.speed;
-
}
-
};
-
var SubMover = _(Mover,
-
{ox : 200, oy: 200,
-
draw : function(){
-
this.run();
-
graphics.beginFill(0);
-
graphics.drawCircle(this.x, this.y, 10);
-
}
-
});
-
-
var m = create(SubMover);
-
-
setInterval(function(){
-
m.radius += 1;
-
m.draw();
-
}, 30)
-
-
for (var i in m){
-
trace(i);
-
}
This makes use of Douglas Crockford's Object.create method. Just one of the many ways to do Object Oriented programming with flash or js. I use a combo of many methods depending on the project:
Actionscript:
-
function Thing(){
-
var x = 0;
-
return function(){
-
x += 1;
-
// do stuff
-
}
-
}
-
-
is a nice one that comes to mind - just a simple closure can be used like an object.
-
-
Almost forgot, in AS small projects ONLY. Here is more info about Object.create() :
-
-
<a href="http://javascript.crockford.com/prototypal.html">http://javascript.crockford.com/prototypal.html</a>
By Zevan | August 30, 2011
Cross your eyes to see 3D third image:

move your mouse up and down on the interactive demo... click ->
Source is a wreck out of pure laziness (also kind of obvious how this works) so if you want it anyway just comment and I'll post it. I'm not embarrassed
Posted in 3D | Tagged papervision |
By Zevan | August 28, 2011
Who would like a zip of the folder I used when making this site fla files and all? If you want one, post a comment and I'll send it to you.
Actually have a little new content for the site coming, at least one new post.
If you don't get yours within a day of posting... just let me know.
By Zevan | January 17, 2011
Actionsnippet has been pretty inactive for the last few months. I took a short break from blogging, but I'm starting up again on a new site... go check it out: zReference
Posted in misc | Tagged zReference |
Found this today, not related to actionscript but rather nice. It allows you to take a screen shot of your website in IE... if your on a mac without windows this is a quick way to test in a pinch:
http://ipinfo.info/netrenderer/index.php
Actionscript:
-
var circs:Array = []
-
var circNum:int = 600;
-
addEventListener(Event.ENTER_FRAME, onAdd);
-
function onAdd(evt:Event):void {
-
if (circs.length <circNum){
-
makeGrowable();
-
}
-
}
-
-
function makeGrowable(){
-
-
var s:MovieClip = MovieClip(addChild(new MovieClip()));
-
s.x = Math.random() * stage.stageWidth;
-
s.y = Math.random() * stage.stageHeight;
-
with(s.graphics){
-
lineStyle(0,0);
-
drawCircle(0,0,10);
-
}
-
s.scaleX = s.scaleY = 0;
-
circs.push(s);
-
s.addEventListener(Event.ENTER_FRAME, onScaleUp);
-
}
-
-
function onScaleUp(evt:Event):void {
-
var c:MovieClip = MovieClip(evt.currentTarget);
-
c.scaleX = c.scaleY += 0.05;
-
for (var i:int = 0; i<circs.length; i++){
-
var circ:MovieClip = circs[i];
-
if (circ != c){
-
var amt:Number = circ.width/2 + c.width/2;
-
var dx:Number = circ.x - c.x;
-
var dy:Number = circ.y - c.y;
-
var dist:Number = Math.sqrt(dx * dx + dy * dy);
-
if (amt> dist){
-
c.removeEventListener(Event.ENTER_FRAME, onScaleUp);
-
if (c.scaleX <0.1){
-
if (contains(c)){
-
removeChild(c);
-
}
-
}
-
}
-
}
-
-
}
-
}
Circle fitting is one of those things I've never bothered to do... today I figured I'd give it a try and this is what I came up with. I posted it on wonderfl:
Actionscript:
-
var story:String = "Fill in the _____.";
-
-
-
var txt:TextField = new TextField();
-
txt.defaultTextFormat = new TextFormat("Georgia", 20);
-
txt.width = stage.stageWidth;
-
txt.multiline = true;
-
txt.wordWrap = true;
-
txt.text = story;
-
addChild(txt);
-
-
var alph:Array = "abcdefghijklmnopqrstuvwxyz".split("");
-
var keys:Object = {};
-
for (var i:int = 0; i<alph.length; i++){
-
keys[65 + i] = alph[i];
-
}
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
-
function onKeyPressed(evt:KeyboardEvent):void{
-
-
if (evt.keyCode == Keyboard.ENTER){
-
story = "Fill in the _____.";
-
txt.text = story;
-
}
-
-
for (var i:int = 0; i<story.length; i++){
-
if (story.charAt(i) == "_"){
-
var head:String = story.substr(0, i);
-
var tail:String = story.substr(i + 1);
-
var letter:String = keys[evt.keyCode];
-
if (!letter) return;
-
story = head + letter + tail;
-
-
txt.text = story;
-
-
break;
-
}
-
}
-
}
I needed to do a fill in the blank for a personal project that I'm working on and this is what I came up with. Have a look at the swf here:
(you need to click first so you can type with the keyboard):
Fill in the blank
Actionscript:
-
var xp:Number = 0;
-
var yp:Number = 0;
-
var t:Number = 0;
-
var a:Number = 100;
-
var b:Number = 10;
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
-
graphics.lineStyle(0,0x000000);
-
addEventListener(Event.ENTER_FRAME, onRun);
-
function onRun(evt:Event):void {
-
var p:Number = ((a + b)/b)*t
-
xp = (a + b) * Math.cos(t) - b * Math.cos(p);
-
yp = (a + b) * Math.sin(t) - b * Math.sin(p);
-
if (t == 0){
-
graphics.moveTo(xp, yp);
-
}else{
-
graphics.lineTo(xp, yp);
-
}
-
t += 0.05;
-
}
I've messed with Epicycloids in the past - browsing mathworld I decided to create this snippet. It will draw a curve like this:

Actionscript:
-
var xp:Number = 0;
-
var yp:Number = 0;
-
var t:Number = 0;
-
var a:Number = 200;
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
-
graphics.lineStyle(0,0x000000);
-
addEventListener(Event.ENTER_FRAME, onRun);
-
function onRun(evt:Event):void {
-
xp = a * Math.cos(t) - (a * Math.pow(Math.sin(t),2))/Math.sqrt(2);
-
yp = a * Math.cos(t) * Math.sin(t);
-
if (t == 0){
-
graphics.moveTo(xp, yp);
-
}else{
-
graphics.lineTo(xp, yp);
-
}
-
t += 0.05;
-
}
While surfing mathworld I stumbled upon the equation for something called the Fish Curve. This snippet will draw something like this:

Posted in Math, misc | Tagged actionscript, as3, flash |
Actionscript:
-
var xp:Number = 0;
-
var yp:Number = 0;
-
var t:Number = 0;
-
var r:Number = 200;
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
-
graphics.lineStyle(0,0x000000);
-
addEventListener(Event.ENTER_FRAME, onRun);
-
function onRun(evt:Event):void {
-
r = 200 * Math.cos(t / 10);
-
xp = r * Math.pow(Math.cos(t), 3);
-
yp = r * Math.pow(Math.sin(t), 3);
-
if (t == 0){
-
graphics.moveTo(xp, yp);
-
}else{
-
graphics.lineTo(xp, yp);
-
}
-
t += 0.1;
-
}
While browsing mathworld I decided to do a variation on this curve . The above snippet will draw something like this:

Posted in Math, misc | Tagged actionscript, as3, flash |