■ 개발관련 ■/산수와 알고리즘
포물선 운동과 공 step10:공의 움직임 함수로 만들기
serpiko
2014. 8. 31. 20:36
포물선 운동과 공
step10 : 공의 움직임 함수로 만들기
버튼을 넣고 클릭 할 때마다 우리가 만들었던 내용을 계속해서 반영하려면
반영 될 때마다 ball의 오리지널 width, height, rotation을 지정해주고 거기에서 부터 다시 시작해야 한다.
즉 전역변수에
var ball_original_w:Number = 79;
var ball_original_h:Number = 77.25; 으로 지정해 주고
function 공함수():void
{
ball.rotation = 0;
ball.width = ball_original_w;
ball.height = ball_original_h;
}
이렇게 기존의 width, height, rotation값을 모두 초기화해 준 뒤시작해야 한다.
소스)
import flash.events.Event;
import flash.display.Sprite;
import flash.filters.BlurFilter;
import flash.filters.BitmapFilterQuality;
import flash.events.MouseEvent;
var g:Number = 1;
var vx:Number;
var vy:Number;
var rot:Number;
var bottom:Number;
var ball_original_w:Number = 79;
var ball_original_h:Number = 77.25;
var _eclipse:Sprite = new Sprite();
var _eclipse_sizeW:Number;
function init():void
{
vx = rot = int(randomize(-10,20));
vy = int(randomize(5,13));
bottom = randomize( stage.stageHeight - (ball.height/2) - 20, stage.stageHeight/4 );//바운드되는 범위
ball.rotation = 0;
ball.width = ball_original_w;
ball.height = ball_original_h;
ball.width = proportional( stage.stageHeight - (ball.height/2), bottom, ball.width);
ball.height = proportional( stage.stageHeight - (ball.height/2), bottom, ball.height);
_eclipse.graphics.beginFill(0x000000, 0.5);
_eclipse.graphics.drawEllipse(-ball.width/2, -20/2, ball.width, 20);
_eclipse.graphics.endFill();
_eclipse.x = ball.x;
_eclipse.y = bottom + (ball.height/2) + 10;
_eclipse_sizeW = ball.width * 70 / 100;//그림자는 볼의 70%사이즈
_eclipse.width = _eclipse_sizeW;
_eclipse_sizeW = percent(ball.width,70);
_eclipse.width = _eclipse_sizeW;
var blur:BlurFilter = new BlurFilter(10,5,BitmapFilterQuality.HIGH);
_eclipse.filters = [blur];
addChild(_eclipse);
var num:int = this.numChildren;
this.setChildIndex(ball, num-1);
//addChild(ball);
if( !this.hasEventListener(Event.ENTER_FRAME) ) this.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event):void
{
vy += g;
ball.y += vy;
ball.x += vx;
ball.rotation += rot;
_eclipse.x = ball.x;
_eclipse.alpha = proportional(bottom,ball.y,1);
_eclipse.width = proportional(bottom,ball.y,_eclipse_sizeW);
if ( ball.x > stage.stageWidth - (ball.width/2) )
{
ball.x = stage.stageWidth - (ball.width/2);
vx = - vx;
rot = - rot;
}
if ( ball.x < (ball.width/2) )
{
ball.x = ball.width / 2;
vx = - vx;
rot = - rot;
}
if (ball.y > bottom)
{
ball.y = bottom;
vy = - vy;
}
if ( ball.y < (ball.height/2) )
{
ball.y = ball.height / 2;
vy = - vy;
}
}
function percent($target:Number, $per:Number):Number
{
return $target * $per / 100;
}
function proportional($A:Number, $a:Number, $B:Number):Number
{
//proportional_1(16, 9, 1920);
var b:Number = $a * $B / $A;
return b;
}
function randomize($min:Number, $max:Number):Number
{
return Math.random() * ($max - $min) + $min;
}
button_mc.buttonMode = true;
button_mc.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{
init();
});
25~27 : 공의 사이즈와 각도를 초기화 해준 뒤 시작하면 된다.
54 : 리스너가 계속 추가되지 않도록 hasEventListener메서드로 검사한 뒤,
"만약 이벤트가 등록되어있지 않다면 등록하시오" 라는 구문이다.
결과)
step10.fla