티스토리 뷰
포물선 운동과 공
step11 : 클래스로 만들기
step09, 10까지 걸쳐서 이미 충분히 완성도 있게 완료는 되었지만
혹시 클래스로 이를 사용할 경우 우리는 단순히 new Ball()로 만들어서 쓰면 되므로 여러가지 잇점이 있다.
Ball( $stage:Stage, $mc:MovieClip, $bottom:int, $top:int) 으로 생성자 함수에서
기본적으로 스테이지, 무비클립객체, 바닥한계, 높이한계를 인자로 받으며 두번째 인자인 $mc에 꼭 공이 아니더라도
무비클립객체 이기만 하면 그림자를 생성하고 공처럼 튕겨줄 것이다.
소스)
package
{
import caurina.transitions.Tweener;
import flash.display.Stage;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.display.MovieClip;
import flash.filters.GlowFilter;
import flash.filters.BlurFilter;
import flash.filters.BitmapFilterQuality;
import flash.utils.setTimeout;
public class Ball extends Sprite
{
//객체
private var _ball :MovieClip;
private var _ball_sizeW :Number;
private var _ball_sizeH :Number;
private var _eclipse :Sprite;
private var _eclipse_sizeW :Number;
//움직임변수
private var _sw :int;
private var _sh :int;
private var _param_bottom :int;
private var _param_top :int;
private var _top :Number; //높이한계
private var _bottom :Number; //바닥한계
private var _wid :Number; //너비
private var _rotate :Number;
private var _vx :Number; //x축
private var _vy :Number; //y축
//중력
private var _gravity :Number = 1;
//시작좌표 범위(x1~x2, y1~y2)
private var START_X1 :int = 160;
private var START_X2 :int = 462;
private var START_Y1 :int = 175;
private var START_Y2 :int = 277;
private const SYNC_DELAY :Number = 2;
public function Ball( $stage:Stage, $mc:MovieClip, $bottom:int, $top:int)
{
_sw = $stage.stageWidth;
_sh = $stage.stageHeight;
_param_bottom = $bottom;
_param_top = $top;
_ball = $mc;
init();
}
public function init():void
{
//좌표계
_vx = _rotate = int(randomize(-10, 20));
_vy = int(randomize(5, 13)); //아래로 떨어지기 위해서는 초기값이 양수
_bottom = randomize( _param_bottom - (_ball.height/2), _param_top); //바운드되는 범위
_top = _ball.height/2;
_wid = _sw - (_ball.width/2);
//_ball 의 원근감
_ball.rotation = 0;
_ball_sizeW = proportional(_sh - (_ball.height/2), _bottom, _ball.width);
_ball_sizeH = proportional(_sh - (_ball.height/2), _bottom, _ball.height);
_ball.width = _ball_sizeW;
_ball.height = _ball_sizeH;
_ball.x = randomize(START_X1, START_X2);
_ball.y = randomize(START_Y1, START_Y2);
_ball.alpha = 0;
//_eclipse
_eclipse = new Sprite();
_eclipse.graphics.beginFill(0x000000, 0.7);
_eclipse.graphics.drawEllipse(-_ball.width/2, -19/2, _ball.width, 19);
_eclipse.graphics.endFill();
_eclipse.x = _ball.x;
_eclipse.y = _bottom + (_ball.height/2) + 15;
_eclipse.alpha = 0;
_eclipse_sizeW = percent(_ball.width, 70); //그림자는 볼의 70%사이즈
_eclipse.width = _eclipse_sizeW;
var blur:BlurFilter = new BlurFilter(10, 5, BitmapFilterQuality.HIGH);
_eclipse.filters = [blur];
//시간차
var syncDelay:Number = randomize(0, SYNC_DELAY);
setTimeout(function():void{
//
addChild(_eclipse);
addChild(_ball);
Tweener.addTween(_ball, {alpha:1, time:3});
Tweener.addTween(_eclipse, {alpha:1, time:3});
addEventLoop();
}, syncDelay * 1000);
}
private function addEventLoop():void
{
if( !_ball.hasEventListener(Event.ENTER_FRAME) ) _ball.addEventListener(Event.ENTER_FRAME,loop);
}
public function get ball_x():Number
{
return _ball.x;
}
public function get ball_y():Number
{
return _ball.y;
}
private function loop(e:Event):void
{
//_ball
if(_ball.y < Math.ceil(_top)){
_ball.y =_top;
_vy = -_vy;
}
if(_ball.y > Math.ceil(_bottom)){
_ball.y = _bottom;
_vy = -_vy;
}
if(_ball.x > Math.ceil(_wid)){
_ball.x = _wid;
_vx = -_vx;
_rotate = -_rotate;
}
if( _ball.x < Math.ceil(_ball.width/2) ){
_ball.x = _ball.width/2;
_vx = -_vx;
_rotate = -_rotate;
}
_vy += _gravity;// vy 속도에 중력가속도 값 더해서 속도 변화
_ball.y += _vy;// y축으로 운동
_ball.x += _vx;// x축으로 등속운동
_ball.rotation +=_rotate;
//_eclipse
_eclipse.x = _ball.x;
_eclipse.alpha = proportional(_bottom, _ball.y, 1);
_eclipse.width = proportional(_bottom, _ball.y, _eclipse_sizeW);
}
private function proportional($A:Number, $a:Number, $B:Number):Number
{
//proportional_1(16, 9, 1920);
var b:Number = $a * $B / $A;
return b;
}
private function randomize($min:Number, $max:Number):Number
{
return Math.random() * ($max - $min) + $min;
}
private function percent($target:Number, $per:Number):Number
{
return $target * $per / 100;
}
public function stop():void
{
_ball.removeEventListener(Event.ENTER_FRAME,loop);
}
}
}
호스트코드
import Ball; //690 ~ 300까지 / 6 = 65 var ball_1:Ball = new Ball( stage, new ball1(), 690, 625 ); var ball_2:Ball = new Ball( stage, new ball2(), 625, 560 ); var ball_3:Ball = new Ball( stage, new ball3(), 560, 495 ); var ball_4:Ball = new Ball( stage, new ball4(), 495, 430 ); var ball_5:Ball = new Ball( stage, new ball5(), 430, 365 ); var ball_6:Ball = new Ball( stage, new ball6(), 365, 300 ); addChild(ball_6); addChild(ball_5); addChild(ball_4); addChild(ball_3); addChild(ball_2); addChild(ball_1);
112 ~ 119 : 만약 Ball 객체를 생성한 호스트 코드에서 _ball의 x,y를 참조하고 싶은 경우
var ball() = new Ball(); 만든 이후
ball.x 나 ball.y 로 참조하면 안된다. 눈으로 보이는 것과 달리 ball의 x,y 는 0,0 이며
ball 클래스 안에 있는 _ball의 x,y를 가져야 하므로 접근권한을 public 으로 선언하고
get 메서드를 사용하여 현재 ball의 x,y를 가져갈수 있도록 한다.
ex) ball.ball_x, ball.ball_y
결과)
'■ 개발관련 ■ > 산수와 알고리즘' 카테고리의 다른 글
| 데이터의 단위와 속도표현 - bit, Byte, KB, MB / bps, kbps, pps (2) | 2014.11.06 |
|---|---|
| 박스모델 공식, 그리드 정렬공식 (정리와 설명은 나중에) (0) | 2014.11.02 |
| 포물선 운동과 공 step10:공의 움직임 함수로 만들기 (0) | 2014.08.31 |
| 포물선 운동과 공 step09:움직임과 위치를 랜덤하게 설정하기 (0) | 2014.08.31 |
| 포물선 운동과 공 step08:그림자의 투명도와 크기 변화 (0) | 2014.08.31 |
댓글