1.플래쉬에서 버블을 하나 그리고 BubbleClip라는 이름의 무비클립을 만든다.(Export to Actionscript 박스 체크!) 그리고 SWC로 export한다.(저장 위치는 현재 작업하고 있는 해당 project안의 libs 폴더)
2.Bubble.as class를 작성한다.
3. Document class인 BubbleTest.as를 작성한다.
Package timo.effect SuperClsss Sprite
package timo.effect
{
import flash.display.Sprite;
import flash.events.Event;
public class Bubble extends Sprite
{
private var clip: BubbleClip = new BubbleClip();
public var vx: Number = 1;
public var vy: Number = 1;
//public var topBound:Number=0;
//public var rightBound:Number=800;
//public var bottonBound:Number=400;
public static var topBound: Number = 0;
public static var rightBound: Number = 800;
public static var bottomBound: Number = 400;
public static var resistance: Number = 0.95;
public static var gravity: Number = 0.2 ;
public function Bubble()
{
super();
this.setLayout();
}
private function setLayout(): void
{
this.addChild( clip );
}
// 움직임 명령이 주어지면, 매 프레임마다 할 액션 등록하기
public function startMove(): void
{
this.addEventListener( Event.ENTER_FRAME, onEnter );
}
// 매 프레임마다... 할 일..
private function onEnter( e: Event ): void
{
this.x = this.x + this.vx;
this.y = this.y + this.vy;
//vx,vy가 변하지 않으면 등속도 운동...
//-를 이용하면 언젠가는 반대로 움직여야 한다.
//빙판과 같은 움직임 서서히 줄어드는 것은 곱하기로 한다.
this.vx = this.vx * Bubble.resistance; //공통으로 사용하는 것이므로 위에서 static처리 해준다.
//this.vx.=this.vx*0.98;
this.vy = this.vy + Bubble.gravity;
// 영역 바깥으로 나가면.. 사라지기
if( this.x > Bubble.rightBound || this.y > Bubble.bottomBound || this.y < Bubble.topBound )
{
this.removeEventListener( Event.ENTER_FRAME, onEnter );
parent.removeChild( this );
}
}
} // class
} // package
SuperClsss Sprite
package
{
import flash.display.Sprite;
import flash.events.Event;
import timo.effect.Bubble;
[ SWF( width="800", height="400", frameRate="36", backgroundColor="0x000000")]
public class BubbleTest extends Sprite
{
public function BubbleTest()
{
super();
this.addEventListener( Event.ENTER_FRAME, onEnter );
}
private function onEnter( e: Event ): void
{
var bubble: Bubble = new Bubble();
bubble.x = -20;
bubble.y = 200;
bubble.width = bubble.height = 5 + 30 * Math.random();
bubble.vx = 20 + 20 * Math.random(); // x 속도 : 20 ~ 40
bubble.vy = -3 + 6 * Math.random(); // y 속도 : -3 ~ 3
this.addChild( bubble );
bubble.startMove();
}
}
}