[練習]getBoundsを使っての跳ね返り

by Tamanegi_kenshi
♥0 | Line 46 | Modified 2010-08-12 22:55:33 | MIT License
play

ActionScript3 source code

/**
 * Copyright Tamanegi_kenshi ( http://wonderfl.net/user/Tamanegi_kenshi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/il7U
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    
    public class FlashTest extends Sprite {
        
        private var ball:Ball;
        private var ball2:Ball;
        
        
        public function FlashTest() {
           init();
        }//FlashTest
        
        private function init():void{
            ball = new Ball(0x000000);
            ball2 = new Ball(0xffff00);
            
            addChild(ball);
            addChild(ball2);
            
            
            ball2.x = 400;
            ball2.y = ball.y = 400;
            addEventListener(Event.ENTER_FRAME, onEnter);
            
        }//init()
        
        private function onEnter(event:Event):void{
            ball.x += ball.vx;
            if(ball.x >= 600 || ball.x <= -100){
                ball.vx *= -1;
            }

            
           
            
            ball2.x += ball2.vx;
            if(ball2.x >= 600 || ball2.x <= -100){
                ball2.vx *= -1;
            }
        

            
            var bounds:Rectangle = ball.getBounds(this);
           // if(ball2.y - ball2.height / 2 > bounds.top && ball2.y - ball2.height / 2 < bounds.bottom){
           //     ball.vy *= -1;
           //     ball2.vy *= -1;
           // }
            if(ball2.x - ball2.width / 2 > bounds.left && ball2.x - ball2.width / 2 < bounds.right){
                ball.vx *= -1;
                ball2.vx *= -1;
            }


            
           
        }//onEnter


    }//class
}//package
import flash.display.Sprite;
class Ball extends Sprite{
    public var vx:int = 5;
    public var vy:int = 3;
    
    
    function Ball(color:uint){
        graphics.beginFill(color);
        graphics.drawCircle(0, 0, 100);
        graphics.endFill();
    }

    
}