ActionScriptアニメーションより練習2(衝突判定) ... ActionScript3.0アニメーションのP264の練習 sakusan393 forked:2favorite:4lines:106license : All rights reserved modified : 2009-04-20 17:28:58 Embed Tweet package { import flash.display.Sprite; import flash.events.Event; import flash.geom.Rectangle; /** * ... * ActionScript3.0アニメーションのP264の練習 */ [SWF(width="400", height="400", backgroundColor="0xFFFFFF", frameRate="30")] public class MultiAngleBounce extends Sprite { private var ball:Ball; private var lines:Array; private var numLines:uint = 5; private var gravity:Number = 0.3; private var bounce:Number = -0.6; public function MultiAngleBounce() { init(); } private function init():void { //ボールを作成 ball = new Ball(20); addChild(ball); ball.x = 100; ball.y = 50; //5本のラインを作成(中心基準で、太さ1px、長さ100px) lines = new Array(); for ( var i:uint = 0; i < numLines; i++) { var line:Sprite = new Sprite(); line.graphics.lineStyle(1); line.graphics.moveTo( -50, 0); line.graphics.lineTo(50, 0); addChild(line); lines.push(line); } //線の配置と回転 lines[0].x = 100; lines[0].y = 100; lines[0].rotation = 30; lines[1].x = 100; lines[1].y = 230; lines[1].rotation = 45; lines[2].x = 250; lines[2].y = 180; lines[2].rotation = -30; lines[3].x = 150; lines[3].y = 330; lines[3].rotation = 10; lines[4].x = 230; lines[4].y = 250; lines[4].rotation = -30; addEventListener(Event.ENTER_FRAME , xEnteFrame); } private function xEnteFrame(e:Event):void { //通常のモーションコード ball.vy += gravity; ball.x += ball.vx; ball.y += ball.vy; //ステージ枠での跳ね返り if (ball.x + ball.radius > stage.stageWidth) { ball.x = stage.stageWidth - ball.radius; ball.vx *= bounce } else if(ball.x - ball.radius < 0){ ball.x = 0 + ball.radius; ball.vx *= bounce; } if (ball.y + ball.radius > stage.stageHeight) { ball.y = stage.stageHeight - ball.radius; ball.vy *= bounce } else if(ball.y - ball.radius < 0){ ball.y = 0 + ball.radius; ball.vy *= bounce; } //各線のチェック for (var i:uint = 0; i < numLines; i++) { checkLine(lines[i]); } } private function checkLine(line:Sprite):void{ //線の境界ボックスの取得 var bounds:Rectangle = line.getBounds(this); //ボールのx座標が線の内側に入っていれば if (ball.x > bounds.left && ball.x < bounds.right) { //角度、サイン、コサインの取得 var angle:Number = line.rotation * Math.PI / 180; var cos:Number = Math.cos(angle); var sin:Number = Math.sin(angle); //線を基準にしたボールの位置を取得 var x1:Number = ball.x - line.x; var y1:Number = ball.y - line.y; //座標系(y)の回転 var y2:Number = cos * y1 - sin * x1; //速度(y方向)の回転 var vy1:Number = cos * ball.vy - sin * ball.vx; //もし、回転後のボールの最下端のy座標が線の座標よりも下、かつ、最初からボールが線よりも下になかったなら if (y2 > -ball.radius && y2 < vy1) { //座標系(x)の回転 var x2:Number = cos * x1 + sin * y1; //速度(x方向)の回転 var vx1:Number = cos * ball.vx + sin * ball.vy; //線のちょうど上に来るように補正 y2 = -ball.radius; //速度ベクトルを逆転 vy1 *= bounce; //もとの座標系に戻す x1 = cos * x2 - sin * y2; y1 = cos * y2 + sin * x2; ball.vx = cos * vx1 - sin * vy1; ball.vy = cos * vy1 + sin * vx1; ball.x = line.x + x1; ball.y = line.y + y1; } } } } } import flash.display.Shape; class Ball extends Shape { public var vx:Number = 0; public var vy:Number = 0; public var radius:Number; function Ball(radius:Number = 20) { this.radius = radius; var color:Number = Math.random() * 0xFFFFFF; graphics.beginFill(color); graphics.drawCircle(0, 0, radius); graphics.endFill(); } } Code Fullscreen Preview Fullscreen Phahede otias : 自由落下 lasa : 自由落下 no48 : 100lines 100lines 自由落下 Math.cos getBounds addEventListener rotation right left Rectangle Shape push Math.PI Array Math.sin Event.ENTER_FRAME uint Event Math.random Number sort new page view favorite forked pv746 forked from: ActionScriptアニメーシ.. realleo forked:0 favorite:0lines:106 (diff:1) pv839 forked from: ActionScriptアニメーシ.. ezshine forked:1 favorite:0lines:106 (diff:1)