Skip to content

Instantly share code, notes, and snippets.

@chiral
Created August 24, 2014 07:08
Show Gist options
  • Save chiral/9f9a8f2d0979678d6ff5 to your computer and use it in GitHub Desktop.
Save chiral/9f9a8f2d0979678d6ff5 to your computer and use it in GitHub Desktop.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.geom.Transform;
import flash.geom.Matrix;
import flash.events.*;
public class Butterfly extends Sprite
{
[Embed(source = './butterfly.png')] private var img:Class;
private var mright:Matrix;
private var mleft:Matrix;
private var step:int;
private var left:Bitmap;
private var right:Bitmap;
public function Butterfly(x0:int,y0:int)
{
left = new img();
right = new img();
left.visible = false;
right.visible = false;
addChild(left);
addChild(right);
x = x0;
y = y0;
mleft = new Matrix(0.5,0,0,0.4,x,y);
mright = new Matrix(-0.5,0,0,0.4,x+130,y);
step = 0;
addEventListener(Event.ENTER_FRAME, onFrame);
}
function onFrame(event:Event):void {
if (step == 0) {
mleft.b = 0;
mleft.a = 0.5;
mleft.tx = x;
mleft.ty = y;
mright.b = 0;
mright.a = -0.5;
mright.tx = x+130;
mright.ty = y;
} else if (step == 4) {
mleft.b = 0.2;
mleft.a = 0.4;
mleft.tx = x+12;
mleft.ty = y-40;
mright.b = 0.2;
mright.a = -0.4;
mright.tx = x+118;
mright.ty = y-40;
} else if (step == 8) {
mleft.b = 0.1;
mleft.a = 0.3;
mleft.tx = x+24;
mleft.ty = y-20;
mright.b = 0.1;
mright.a = -0.3;
mright.tx = x+104;
mright.ty = y-20;
}
var tleft:Transform = new Transform(left);
var tright:Transform = new Transform(right);
tleft.matrix = mleft;
tright.matrix = mright;
left.visible = true;
right.visible = true;
step++;
if (step == 12) step = 0;
}
}
}
package
{
import flash.display.Sprite;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.events.*;
public class Main extends Sprite {
private var butterflies:Vector.<Butterfly> = new Vector.<Butterfly>(10);
public function Main() {
var i:int;
for (i = 0; i < butterflies.length; i++) {
butterflies[i] = new MovingButterfly(150, 100, 300, 200);
this.addChild(butterflies[i]);
}
}
}
}
package
{
import flash.display.Sprite;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.events.*;
public class MovingButterfly extends Butterfly {
private var x2:int;
private var y2:int;
private var w1:int;
private var h1:int;
private var spd:int = 30;
private var counter:int;
public function MovingButterfly(x0:int, y0:int, w0:int, h0:int) {
super(x0,y0);
x2 = x0;
y2 = y0;
w1 = w0;
h1 = h0;
counter = 0;
this.addEventListener(Event.ENTER_FRAME, onFrame);
}
override function onFrame(event:Event):void {
counter++;
if (counter > 10) {
var rnd = Math.floor(Math.random()*30);
if (rnd == 0) {
x2 = Math.random()*w1+30;
y2 = Math.random()*h1+30;
counter = 0;
}
x += (x2 - x) / spd;
y += (y2 - y) / spd;
}
super.onFrame(event);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment