周期弾

by medadotter
周期的にしたかったら円関数に放り込むのが早い。オーバーフローくらいには注意するべきかもしれんが。
♥0 | Line 117 | Modified 2012-03-13 06:17:41 | MIT License
play

ActionScript3 source code

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

package  {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    
    /**
     * ...
     * @author DT
     */
    [SWF(backgroundColor="0xffffff",width="465",height="465")]
    public class STGMain extends Sprite {
        private var bullets:Vector.<Bullet> = new Vector.<Bullet>();
        private var numBullets:uint = 50;
        private var beginX:Number = stage.stageWidth / 2;
        private var beginY:Number = stage.stageHeight / 2;
        private var frames:int = 0;
        static private const speed:Number = 3;
        
        public function STGMain() {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(event:Event):void {
            if (frames++ % 2 == 0) {
                var bullet:Bullet = makeBullet();
                addChild(bullet);
                bullets.push(bullet);                
            }
            
            for (var i:int = bullets.length - 1; i >= 0 ; i--) {
                bullets[i].update();
                if (!bullets[i].onArea()) removeBullet(i);
            }
        }
        
        private function removeBullet(i:int):void {
            removeChild(bullets[i]);
            bullets.splice(i, 1);
        }
        
        private function refreshBullet(bullet:Bullet):Bullet {
            var color:uint = Math.random() * 0xffffff;
            var radius:Number = Math.random() * 15 + 5;
            bullet.color = color;
            bullet.radius = radius;
            bullet.x = beginX;
            bullet.y = beginY;
            bullet.velocity.x = speed * Math.cos(angle());
            bullet.velocity.y = speed * Math.sin(angle());
            return bullet;
        }
        
        private function angle():Number {
            return 2 * Math.PI / 3 * Math.sin(frames / 10.0) + Math.PI / 2;
        }
        
        private function makeBullet():Bullet {
            return refreshBullet(new Bullet());
        }
        
    }

}

import flash.display.Sprite;
import flash.geom.Point;
class Bullet extends Sprite {
        private var _position:Point = new Point();
        private var _velocity:Point = new Point();
        private var _radius:Number;
        private var _color:uint;
        
        public function Bullet(radius:Number = 40, color:uint = 0xff0000,x:Number = 0,y:Number=0) {
            this.radius = radius;
            this.color = color;
            this.x = x;
            this.y = y;
            draw();
        }
        
        public function update():void {
            x += velocity.x;
            y += velocity.y;
        }
        
        public function onArea():Boolean {
            if (x + radius >= 0 && x - radius <= stage.stageWidth && 
                y + radius >= 0 && y - radius <= stage.stageHeight) return true;
            return false;
        }
        
        private function draw():void {
            graphics.clear();
            graphics.beginFill(color);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
        
        override public function set x(value:Number):void {
            position.x = value;
            super.x = position.x;
        }
        
        override public function set y(value:Number):void {
            position.y = value;
            super.y = position.y;
        }
        
        public function get position():Point {
            return _position;
        }
        
        public function set position(value:Point):void {
            _position = value;
        }
        
        public function get velocity():Point {
            return _velocity;
        }
        
        public function set velocity(value:Point):void {
            _velocity = value;
        }
        
        public function get radius():Number {
            return _radius;
        }
        
        public function set radius(value:Number):void {
            _radius = value;
            draw();
        }
        
        public function get color():uint {
            return _color;
        }
        
        public function set color(value:uint):void {
            _color = value;
            draw();
        }
        
    }