ball in maze

by aduonety forked from forked from: [QuickBox2D] Doll in space (diff: 134)
[SWF(width = 465, height = 465, backgroundColor = 0x333333, frameRate = 60)]
♥0 | Line 117 | Modified 2012-05-23 17:20:40 | MIT License
play

ActionScript3 source code

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

// forked from a24's forked from: [QuickBox2D] Doll in space
// forked from paq's [QuickBox2D] Doll in space

package 
{
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import com.actionsnippet.qbox.objects.CircleObject;
    import com.actionsnippet.qbox.QuickBox2D;
    import com.actionsnippet.qbox.QuickObject;
    import flash.display.MovieClip;
    import flash.display.StageScaleMode;
    import flash.events.Event;
      import frocessing.math.*;
    import flash.geom.Rectangle;
    import flash.events.MouseEvent;
    import jp.progression.commands.lists.*;
    import flash.display.Sprite;
    
    
   [SWF(width = 465, height = 465, backgroundColor = 0x333333, frameRate = 60)]
    
    public class Sample extends MovieClip
    {
        private var sim:QuickBox2D;
        private var fixedPoint:b2Vec2 = new b2Vec2(7.25, 7.25);
        private var scale:Number = 10;
        private var balls:Array = new Array();
        private var list:SerialList = new SerialList();
        public var data:Array = new Array(16);
        
///*       
        //フラグ用の配列の生成
       public function Makearray():void 
       {
           {
            for (var i_m:int = 0; i_m < 16; i_m++)
                {
                data[i_m] = new Array(16);
                }
                
                for(var i:int = 0; i<16; i++){
                for(var j:int = 0; j<16; j++)
                {
                    data[i][j]= 0;    //初期値0
          
                }
                }
           }
       }
//*/           
           
           
           
         // 配列の値を返す  
           public function CheckExistBox(xe:int, ye:int):int
         {     
         var value:int = data[xe][ye];
  
            return value;
           }
           
        
      
 
        public function Sample():void 
        {
                
            if (stage) init();
            
            else addEventListener(Event.ADDED_TO_STAGE, init);
            
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            sim = new QuickBox2D(this);
            sim.gravity = new b2Vec2(0, 30);
            sim.iterations = 10;
        // sim.createStageWalls();
           
            sim.setDefault( { fillColor:0xFFFFFF, lineColor:0x333333 } );
            
            Makearray();
            //玉の生成
            for (var i:int = 0; i < 350; i++)  
            {
                balls.push(sim.addCircle( { x:Math.random() * 14, y:Math.random() * 2, radius:0.15 } ));
            }


                  
             //枠生成
              for(var ix:int = 0; ix < 15; ix++){
                    for(var iy:int = 0; iy < 15; iy++){
                           if(ix == 0 || ix == 14 || iy == 0 || iy == 14){
                               sim.addBox( { x:ix, y:iy, width:1, height:1,density:0 } );
                           }
                    }
              }
             
      
        
              //枠の中身
              {
                  
             for(var ix_d:int = 1; ix_d < 14; ix_d++){
                 for(var iy_d:int = 1; iy_d < 14; iy_d++){
                           if(ix_d % 2 == 0 && iy_d % 2 == 0){
                               sim.addBox( { x:ix_d, y:iy_d, width:1, height:1,density:0 } );
                               
                             
                             
                             
                                 
                         switch (int(Math.random() * 4)) // 0~3のランダム値を生成
                                {
                                    // 右に倒す
                                    case 0: ix_d++;
                                        sim.addBox( { x:ix_d, y:iy_d, width:1, height:1,density:0 } );
                                        data[ix_d][iy_d] = 1;
                                        ix_d--;
                                        break;
                     
                                    // 左                
                                    case 1: ix_d--; 
                                    if(CheckExistBox(ix_d,iy_d) == 1){
                                        ix_d++;
                                        iy_d--;
                                        break;
                                    }
                                       else {
                                        sim.addBox( { x:ix_d, y:iy_d, width:1, height:1,density:0 } );
                                        data[ix_d][iy_d] = 1;
                                        ix_d++;                                       
                                         break;
                                    }
                                    

                                    
                                    // 下
                                    case 2: iy_d++; 
                                        sim.addBox( { x:ix_d, y:iy_d, width:1, height:1,density:0 } );
                                        data[ix_d][iy_d] = 1;
                                        iy_d--;                                         
                                         break;
                                    
                                     
                                     
                                     
                                   // 上
                                    case 3: iy_d--; 
                                    if(CheckExistBox(ix_d,iy_d) == 1){
                                        break;
                                    }
                                    else{
                                    sim.addBox( { x:ix_d, y:iy_d, width:1, height:1,density:0 } );
                                    data[ix_d][iy_d] = 1;
                                        iy_d++;                                       
                                         break;
                                     }
                                }   
                           
                           } 
                           
                    }
              
              }
              
              
                  }
            sim.start();
            sim.mouseDrag();
       
        }



        
        private function loop(e:Event):void 
        {
            for ( var i:int = 0; i < balls.length; i ++ )
            {
               var ball:QuickObject = balls[i];
                var fp:b2Vec2 = fixedPoint.Copy();
               fp.Subtract(ball.body.GetPosition());
                fp.Normalize();
               fp.Multiply(50);
                balls.push(sim.addCircle( { x:Math.random() * 15.5, y:Math.random() * 7, radius:0.6 } ));
               ball.body.ApplyForce(fp, ball.body.GetWorldCenter());
                ball.body.ApplyForce(new b2Vec2(0, -30), ball.body.GetWorldCenter());
            }
       }
    }
}

Forked