■ 개발관련 ■/산수와 알고리즘
포물선 운동과 공 step04:공의 회전
serpiko
2014. 8. 31. 18:59
포물선 운동과 공
step04 : 공의 회전
공의 회전은 오른쪽으로 진행할 때는 시계방향이므로 + , 왼쪽으로 진행할 때는 반시계방향이므로 - 이다.
다른 처리를 해줄 필요없이 ball의 x방향에 따라서 rotation을 적용해 주면 된다.
rot 라는 변수를 하나 만들어서 x축의 변화가 있을때마다 부호를 바꿔준다.
소스)
import flash.events.Event; var g:Number = 1; var vy:Number = 8; var vx:Number = 10; var rot:Number = vx; this.addEventListener(Event.ENTER_FRAME, function(e:Event):void{ vy += g; ball.y += vy; ball.x += vx; ball.rotation += rot; 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 > stage.stageHeight - (ball.height/2) ) { ball.y = stage.stageHeight - (ball.height/2); vy = -vy; } if( ball.y < (ball.height/2) ) { ball.y = ball.height/2; vy = -vy; } });
결과)