Butterfly Effect
In the previous post I got a single wing flapping… now it’s time to construct the butterfly from two of them. This involves adding another wing, and starting it off with a Y rotation of 180 degrees (exactly opposite to the right wing, which we did first). It will also rotate up to a Y rotation of 90 degrees when flapping up. I refactored the ButterflyWing class, so that you can override the start (down) and end (up) positions. Here’s the result:
NB: you need to install the flash 10 player for this to work.
ButterflyRotate.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package {
import flash.display.Sprite;
import flash.events.*;
import com.bit101.components.*;
[SWF(backgroundColor="0x000000", width="700", height="500", frameRate="30")]
public class ButterflyRotate extends Sprite {
private var butterfly:Butterfly;
private var rxSlider:HSlider;
private var rySlider:HSlider;
private var rzSlider:HSlider;
public function ButterflyRotate() {
init();
}
private function init():void {
//add butterfly
butterfly = new Butterfly();
butterfly.x = stage.stageWidth/2 - butterfly.width/2;
butterfly.y = stage.stageHeight/2 ;//butterfly is already horizontally centered
addChild(butterfly);
//add controls for x,y,z rotation
rxSlider = new HSlider(this, 500, 20, function(event:Event):void {
butterfly.rotationX = event.target.value;
});
rxSlider.setSliderParams(0, 360, 0);
addChild(rxSlider);
//y rotation slider
rySlider = new HSlider(this, 500, 40, function(event:Event):void {
butterfly.rotationY = event.target.value;
});
rySlider.setSliderParams(0, 360, 0);
addChild(rySlider);
//z rotation slider
rzSlider = new HSlider(this, 500, 60, function(event:Event):void {
butterfly.rotationZ = event.target.value;
});
rzSlider.setSliderParams(0, 360, 0);
addChild(rzSlider);
}
}
}
// Copyright (c) 2008 David Wilhelm
// MIT license: http://www.opensource.org/licenses/mit-license.php
Here’s the Butterfly class itself..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package {
import flash.display.Sprite;
public class Butterfly extends Sprite {
private var leftWing:ButterflyWing;
private var rightWing:ButterflyWing;
public function Butterfly() {
//left wing starts with rotationY of 180
leftWing = new ButterflyWing(180);
//position so that center is at y=0
leftWing.y = -(leftWing.height/2);
addChild(leftWing);
leftWing.flapUp();
//right wing is default position
rightWing = new ButterflyWing();
rightWing.y = -(rightWing.height/2);
addChild(rightWing);
rightWing.flapUp();
}
}
}
// Copyright (c) 2008 David Wilhelm
// MIT license: http://www.opensource.org/licenses/mit-license.php