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.
package{importflash.display.Sprite;importflash.events.*;importcom.bit101.components.*;[SWF(backgroundColor="0x000000",width="700",height="500",frameRate="30")]publicclassButterflyRotateextendsSprite{privatevarbutterfly:Butterfly;privatevarrxSlider:HSlider;privatevarrySlider:HSlider;privatevarrzSlider:HSlider;publicfunctionButterflyRotate(){init();}privatefunctioninit():void{//add butterflybutterfly=newButterfly();butterfly.x=stage.stageWidth/2 - butterfly.width/2;butterfly.y=stage.stageHeight/2 ;//butterflyisalreadyhorizontallycenteredaddChild(butterfly);//add controls for x,y,z rotationrxSlider=newHSlider(this,500,20,function(event:Event):void{butterfly.rotationX=event.target.value;});rxSlider.setSliderParams(0,360,0);addChild(rxSlider);//y rotation sliderrySlider=newHSlider(this,500,40,function(event:Event):void{butterfly.rotationY=event.target.value;});rySlider.setSliderParams(0,360,0);addChild(rySlider);//z rotation sliderrzSlider=newHSlider(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..
1234567891011121314151617181920212223242526
package{importflash.display.Sprite;publicclassButterflyextendsSprite{privatevarleftWing:ButterflyWing;privatevarrightWing:ButterflyWing;publicfunctionButterfly(){//left wing starts with rotationY of 180leftWing=newButterflyWing(180);//position so that center is at y=0leftWing.y=-(leftWing.height/2);addChild(leftWing);leftWing.flapUp();//right wing is default positionrightWing=newButterflyWing();rightWing.y=-(rightWing.height/2);addChild(rightWing);rightWing.flapUp();}}}// Copyright (c) 2008 David Wilhelm// MIT license: http://www.opensource.org/licenses/mit-license.php