In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

circle_speed2

Get Adobe Flash player
by yskw717 12 Jan 2012
    Embed
/**
 * Copyright yskw717 ( http://wonderfl.net/user/yskw717 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rD7c
 */

// forked from yskw717's forked from: forked from: forked from: forked from: forked from: forked from: flash on 2011-12-18
// forked from yskw717's forked from: forked from: forked from: forked from: forked from: flash on 2011-12-18
// forked from yskw717's forked from: forked from: forked from: forked from: flash on 2011-12-18
// forked from yskw717's forked from: forked from: forked from: flash on 2011-12-18
// forked from yskw717's forked from: forked from: flash on 2011-12-18
// forked from yskw717's flash on 2011-12-18
package {
    import flash.geom.Point;
    import flash.display.AVM1Movie;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.events.MouseEvent; 
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.Timer;
    
    
    [SWF(width="465", height="415", backgroundColor="#000000", frameRate="30")]

    
    public class MovingCircle extends Sprite {
            //変数を宣言
            public var nn:Number;
            public var speed:Number;
            public var speedX:Number;//速度
            public var speedY:Number;//速度
            public var _timer:Timer = new Timer(2000);
            public var nC:Number = 1;
            public var circleArray:Array = new Array;
            public var posXArray:Array = new Array;
            public var posYArray:Array = new Array;
            public var endTime:int;
            
            // Instantiate stage variables
            public var sw:Number = stage.stageWidth;
            public var sh:Number = stage.stageHeight;

            // Draw grid lines
            public var grid:Sprite = new Sprite();
            public var horzLines:Sprite = new Sprite();
            public var vertLines:Sprite = new Sprite();
            public var gridLines:Array = new Array();
            public var spacing:Number = 24;
            public var lineColor:int = 0x333333;
            public var lineWeight:Number = 1;
            public var lineAlpha:Number = 0.5;
            public var lineHinting:Boolean = true;
            public var lineScale:String = "none";

            // Draw x-axis and y-axis
            public var xAxis:Sprite = new Sprite();
            public var yAxis:Sprite = new Sprite();
            public var xAxisColor:int = 0xeeeeee;
            public var yAxisColor:int = 0xeeeeee;

            public var hLine:Shape = new Shape();
            public var vLine:Shape = new Shape();

            public var numVertLines:int = Math.floor(sw/spacing);
            public var numHorzLines:int = Math.floor(sh/spacing);
            public var xOffset:Number = (Math.ceil(numHorzLines/2) * spacing) - (sw/2);
            public var yOffset:Number = (Math.ceil(numVertLines/2) * spacing) - (sh/2); 

            // Reusable array index variable
            private var i:int;


            
           
            public function MovingCircle() {
               speed = 2;
               endTime = 0;
               nn = 0;
               stage.addEventListener(MouseEvent.CLICK,onClick);
               //イベントリスナーを登録する
               this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
               
               // Turn scaling off and set alignment to top left
               stage.scaleMode = StageScaleMode.NO_SCALE;
               stage.align = StageAlign.TOP_LEFT;

               // Draw grid lines
               drawGridLines();
               addChild(grid);
               grid.addChild(horzLines);
               grid.addChild(vertLines);

               // Draw x- and y-axes and add to display list
               drawAxes();

               // Add event listener to redraw grid on stage resize
               stage.addEventListener(Event.RESIZE, resizeListener);

            }
            
               public function onClick(e:MouseEvent):void{
                  
                   if (nC == 2){
                       this.removeChild(circleArray[1]);
                       circleArray[1] = null;
                       posXArray[1] = null;
                       posYArray[1] = null;
                   }
                   if (nC >2 && nC<11){
                       for (var c:int=2;c<=nC;c++){
                           this.removeChild(circleArray[c]);
                           posXArray[c] = null;
                           posYArray[c] = null;
                           circleArray[c] = null;
                           //trace("aaa");
                       }
                   }
                   if (nC >= 11){
                   for (var d:int=2;d<=9;d++){
                           this.removeChild(circleArray[d]);
                           posXArray[d] = null;
                           posYArray[d] = null;
                           circleArray[d] = null;
                           //trace("bbb");
                       }
                   }
               nn = 0;
               speedX = speed;
               speedY = speed-speedX;
               nC = 1;
               posXArray[nC] = new Number;
               posXArray[nC] = 0;
               posYArray[nC] = new Number;
               posYArray[nC] = 0;
               circleArray[nC] = new Sprite();
               circleArray[nC].graphics.beginFill(0x222222);
               circleArray[nC].graphics.drawCircle(stage.stageWidth*0.5,stage.stageHeight*0.5, 45);
               this.addChild(circleArray[nC]);
               nC += 1;
              //_timer.stop();
              //_timer.removeEventListener(TimerEvent.TIMER, _again);
              //_timer.addEventListener(TimerEvent.TIMER, _again);
              //_timer.start();
               }
               
               public function enterFrameHandler(event:Event):void {
                   var my_date:Date = new Date();
                   var nSeconds:Number;
                   nSeconds = my_date.seconds;
                   //trace(nSeconds);
                   //座標を更新
                   if (nC ==2 ){ 
                       posXArray[1] += speedX;
                       posYArray[1] += speedY;
                       circleArray[1].x = posXArray[1];
                       circleArray[1].y = posYArray[1];
                       var xx:Number = circleArray[1].x;
                       var yy:Number = circleArray[1].y;
                       
                        if(xx <= -100 || xx >= 100 ){
                            //trace("yes");
                            speedX *= -1;
                            }
                        if(yy <= -100 || yy >= 100 ){   
                            //trace("yes");
                            speedY *= -1;
                            }
                            
                        if(nSeconds%7 == 0 && nSeconds%8 != 0){
                            nn = 0;
                        }

                        if(nSeconds%8 == 0){
                            if(nn == 0){
                                if(speedX == 0 && nn == 0){
                                    trace("2");
                                    speedY = 0;
                                    var q2:int = Math.round(Math.random()*5);
                                    var a2:int = Math.pow(-1,q2);
                                    speedX = speed*a2;
                                    nn = 1;
                                }
                                if(speedX == speed && nn == 0 || speedX == -1*speed && nn == 0){
                                    trace("1");
                                    speedX = 0;
                                    var q1:int = Math.round(Math.random()*5);
                                    var a1:int = Math.pow(-1,q1);
                                    speedY = speed*a1;
                                    nn = 1;
                                }
                                
                          }
                        }
                        
                        if(nSeconds == endTime || nSeconds == (endTime+30)){
                            if(nC == 2){
                                 //var npoint:Point = new Point(circleArray[1].x, circleArray[1].y);
                                 //var ltg:Point = circleArray[1].localToGlobal(npoint);
                                 var x1:Number = circleArray[1].x;
                                 var y1:Number = circleArray[1].y;
                                 this.removeChild(circleArray[1]);
                                 circleArray[1] = null;
                                 //circleArray[1].visible = !circleArray[1].visible;
                                 for(var e:int=2; e <=5;e++){
                                 posXArray[e] = new Number;
                                 posXArray[e] = 0;
                                 posYArray[e] = new Number;
                                 posYArray[e] = 0;
                                 circleArray[e] = new Sprite();
                                 circleArray[e].graphics.beginFill(0x222222);
                                 circleArray[e].graphics.drawCircle(stage.stageWidth*0.5+x1,stage.stageHeight*0.5+y1, 45);
                                 this.addChild(circleArray[e]);
                                 nC += 1;
                            }
                       }
                        
                        

                          
                      }
                   }
                    
                    if(nC == 6){
                    for (var b:int = 2; b <= 6; b++){
                        if(b == 2){
                            circleArray[b].x += speed
                        }
                        if(b == 3){
                            circleArray[b].x -= speed
                        }
                        if(b == 4){
                            circleArray[b].y += speed
                        }
                        if(b == 5){
                            circleArray[b].y -= speed
                        }
                        /*
                         if(b >4 && b<=7){
                            posXArray[b] += -5 + b;
                            posYArray[b] += 2-posXArray[b]
                            circleArray[b].x += -5+b;
                            circleArray[b].y += 7-b;
                        }
                        if(b ==8 ){
                            circleArray[b].x += 1 ;
                            circleArray[b].y -= 1;
                        }
                        if(b ==9 ){
                            circleArray[b].x -= 1 ;
                            circleArray[b].y += 1;
                        }
                        */
                    }
                    }
                   }
                   
                   
           private function resizeListener(e:Event):void {
            // Stage size at resize
            sh = stage.stageHeight;
            sw = stage.stageWidth;

            resizeAxes();
            redrawGridLines();
        }

        private function resizeAxes():void {
            // Scale x-axis and y-axis at stage resize
            yAxis.height = sh;
            yAxis.x = sw/2;
            xAxis.width = sw;
            xAxis.y = sh/2;
        }

        private function redrawGridLines():void {
            // Determine number of lines to draw
            numHorzLines = Math.floor(sh/spacing);
            numVertLines = Math.floor(sw/spacing);

            // Determine horizontal and vertical offsets
            xOffset = (sw/2) - (Math.ceil(numVertLines/2) * spacing);
            yOffset = (sh/2) - (Math.ceil(numHorzLines/2) * spacing); 

            // Resize horizontal lines
            for (i = 0; i < horzLines.numChildren; i++) {
                horzLines.getChildAt(i).width = sw;
                horzLines.getChildAt(i).y = i * spacing + yOffset;
            }

            // Resize vertical lines
            for (i = 0; i < vertLines.numChildren; i++) {
                vertLines.getChildAt(i).height = sh;
                vertLines.getChildAt(i).x = i * spacing + xOffset;
            }

            // If number of existing lines is less than lines required to fill screen
            if (numHorzLines > horzLines.numChildren) {
                // Draw additional horizontal lines
                for (i = horzLines.numChildren - 1; i <= numHorzLines; i++) {
                    gridLines[i] = new Shape();
                    gridLines[i].name = "hLine" + i;
                    drawHorzLine(gridLines[i]);
                    gridLines[i].y = (i + 1) * spacing + yOffset;
                }
            }

            // If number of existing lines is less than lines required to fill screen
            if (numVertLines > vertLines.numChildren) {
                // Draw additional vertical lines
                for (i = vertLines.numChildren - 1; i <= numVertLines; i++) {
                    gridLines[i] = new Shape();
                    gridLines[i].name = "vLine" + i;
                    drawVertLine(gridLines[i]);
                    gridLines[i].x = (i + 1) * spacing + xOffset;
                }
            }
        }

        private function drawAxes():void {
            // Draw lines at center stage
            var yg:Graphics = yAxis.graphics;
            var xg:Graphics = xAxis.graphics;

            yg.lineStyle(lineWeight, yAxisColor, lineAlpha, lineHinting, lineScale);
            yg.moveTo(0,0);
            yg.lineTo(0,sh);
            yAxis.x = sw/2;

            xg.lineStyle(lineWeight, xAxisColor, lineAlpha, lineHinting, lineScale);
            xg.moveTo(0,0);
            xg.lineTo(sw,0);
            xAxis.y = sh/2;

            addChild(yAxis);
            addChild(xAxis);
        }

        private function drawHorzLine(horzLine:Shape):void {
            var hl:Graphics = horzLine.graphics;
            hl.lineStyle(lineWeight, lineColor, lineAlpha, lineHinting, lineScale);
            hl.moveTo(0,0);
            hl.lineTo(sw,0);
            horzLines.addChild(horzLine);
        }

        private function drawVertLine(vertLine:Shape):void {
            var vl:Graphics = vertLine.graphics;
            vl.lineStyle(lineWeight, lineColor, lineAlpha, lineHinting, lineScale);
            vl.moveTo(0,0);
            vl.lineTo(0,sh);
            vertLines.addChild(vertLine);
        }

        private function drawGridLines():void {
            // Set grid spacing and line style
            numHorzLines = Math.floor(sh/spacing);
            numVertLines = Math.floor(sw/spacing);

            xOffset = (sw/2) - (Math.ceil(numVertLines/2) * spacing);
            yOffset = (sh/2) - (Math.ceil(numHorzLines/2) * spacing); 

            // Draw horizontal lines
            for (i = (-0.5*numHorzLines); i <= numHorzLines; i++) {
                gridLines[i] = new Shape();
                gridLines[i].name = "hLine" + i;
                drawHorzLine(gridLines[i]);
                gridLines[i].y = i * spacing + yOffset;
            }

            // Draw vertical lines
            for (i = (-0.5*numVertLines); i <= numVertLines; i++) {
                gridLines[i] = new Shape();
                gridLines[i].name = "vLine" + i;
                drawVertLine(gridLines[i]);
                gridLines[i].x = i * spacing + xOffset;
            }
        }
               
           
      }
}
var trace:Function = Wonderfl.log;