Advertisement
Guest User

Untitled

a guest
May 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     // import stuff
  3.     import flash.display.Bitmap;
  4.     import flash.display.Sprite;
  5.     import flash.events.Event;
  6.     import flash.utils.getDefinitionByName;
  7.     import flash.display.DisplayObject;
  8.     import flash.display.MovieClip;
  9.    
  10.     // import box2d
  11.     import Box2D.Dynamics.Joints.*;
  12.     import Box2D.Dynamics.*;
  13.     import Box2D.Collision.*;
  14.     import Box2D.Collision.Shapes.*;
  15.     import Box2D.Common.Math.*;
  16.     import flash.events.*;
  17.  
  18.     // import the sprite data
  19.     import PhysicsData;
  20.    
  21.     public class Main extends Sprite {
  22.  
  23.         public var nextSprite:Number=10;
  24.  
  25.         // create an object containing the shape data
  26.         public var physicsData:PhysicsData = new PhysicsData();
  27.  
  28.         // create the box2d world, set up gravity
  29.         public var world:b2World=new b2World(new b2Vec2(0,10.0),true);
  30.                
  31.        
  32.         public function Main():void {
  33.             // Grab the bitmap from the compiled resources
  34.             var bdArt = new backdrop();
  35.             var beaker:MovieClip = new fleaker();
  36.             var dum:MovieClip = new dummy();
  37.  
  38.             //var artClass:Class = getDefinitionByName("cupunder") as Class;
  39.             //var artClass2:Class = getDefinitionByName("cupover") as Class;
  40.             //var artClass3:Class = getDefinitionByName("dummy") as Class;
  41.  
  42.             // Creat a display object to add to the stage
  43.             //var bitmap:Bitmap = new Bitmap(new artClass(0, 0), "auto", true);
  44.             //var bitmap2:Bitmap = new Bitmap(new artClass2(0, 0), "auto", true);
  45.             //var bitmap3:Bitmap = new Bitmap(new artClass2(0, 0), "auto", true);
  46.  
  47.             // create a physics body from the physics data structure
  48.             var body:b2Body=physicsData.createBody("beaker",
  49.                                                    world,
  50.                                                    b2Body.b2_staticBody,
  51.                                                    beaker);
  52.            
  53.             var floor:b2Body=physicsData.createBody("floor", world, b2Body.b2_staticBody, dum);
  54.                
  55.             // set the position of the sprite
  56.             body.SetPositionAndAngle(
  57.                         new b2Vec2(800/physicsData.ptm_ratio,460/physicsData.ptm_ratio), 0);
  58.                        
  59.             floor.SetPositionAndAngle(
  60.                         new b2Vec2(-400/physicsData.ptm_ratio,460/physicsData.ptm_ratio), 0);
  61.                        
  62.            
  63.                        
  64.             //bitmap2.x = 600;
  65.             //bitmap2.y = 337;
  66.             //bitmap3.x = -5;
  67.             //bitmap3.y = -5;
  68.                        
  69.             addChildAt(bdArt, 0);
  70.             // add the sprite to the scene
  71.             //addChildAt(bitmap, 1);
  72.             //addChildAt(bitmap3, 1);
  73.             //addChildAt(bitmap2, 2);
  74.             addChildAt(beaker, 1);
  75.  
  76.             // add an event listener to update sprite's from
  77.             // physics data
  78.             addEventListener(Event.ENTER_FRAME, update, false, 0, true);
  79.             stage.addEventListener(MouseEvent.MOUSE_DOWN, addNewSprites);
  80.         }
  81.  
  82.         // this function adds new sprites to the scene
  83.         public function addNewSprites(e:MouseEvent):void {
  84.  
  85.             // only add sprites every 30th frame
  86.             /*nextSprite--;
  87.             if(nextSprite > 0)
  88.             {
  89.                 return;
  90.             }
  91.             nextSprite = 30;*/
  92.  
  93.             // names of the items / sprites we use
  94.             var items:Array=["blueberry2",
  95.                             "blueberry3"];
  96.            
  97.             var mcbb = new Blueberry();
  98.                
  99.             // choose a random object
  100.             var name:String=items[Math.floor(Math.random()*items.length)];
  101.                
  102.             // Grab the bitmap from the compiled resources
  103.             var artClass:Class = getDefinitionByName(name) as Class;
  104.  
  105.             // Creat a display object to add to the stage
  106.             var bitmap:Bitmap = new Bitmap(new artClass(0, 0), "auto", true);
  107.            
  108.             // create a physics body from the physics data structure
  109.             var body:b2Body=physicsData.createBody("bb",
  110.                                                    world,
  111.                                                    b2Body.b2_dynamicBody,
  112.                                                    mcbb);
  113.                                                    
  114.             body.SetUserData(mcbb);
  115.                
  116.             // set a random start position
  117.             body.SetPositionAndAngle(new b2Vec2(Math.random()*11 + 26,-4), 0);
  118.    
  119.             //body.SetUserData(texture);
  120.                
  121.             // add the sprite to the scene
  122.             addChildAt(mcbb, 1);
  123.         }
  124.  
  125.         // this function runs the world simulation
  126.         // and adjusts the sprites according to the positions
  127.         // of the objects in the simulation
  128.         public function update(e : Event):void {
  129.             // step the world
  130.             world.Step(1 / 30, 10, 10);
  131.            
  132.             world.ClearForces();
  133.            
  134.             // add new sprites to the world
  135.             //addNewSprites();
  136.  
  137.             // update the sprites from the physics data
  138.             var Body:b2Body = world.GetBodyList();
  139.             for ( ; Body; Body = Body.GetNext()) {
  140.                 if (Body.GetUserData()!=null) {
  141.                    
  142.                     // get the user date value from the body
  143.                     //var bb:MovieClip=Body.GetUserData() as MovieClip;
  144.                    
  145.                     // extract the position and set the sprite to it
  146.                     Body.GetUserData().x=Body.GetPosition().x*physicsData.ptm_ratio;
  147.                     Body.GetUserData().y=Body.GetPosition().y*physicsData.ptm_ratio;
  148.                    
  149.                     var dx = Body.GetPosition().x*physicsData.ptm_ratio - 960;
  150.                     var dy = Body.GetPosition().y*physicsData.ptm_ratio;
  151.                     var degrees = (Math.atan2(dy, dx) * 180 / Math.PI);
  152.                    
  153.                     // set the rotation of the sprite
  154.                     Body.GetUserData().rotation = Body.GetAngle() * (180/Math.PI);
  155.                     //Body.GetUserData().spec.rotation = degrees -90;
  156.                    
  157.                     if((Body.GetPosition().x < 810/physicsData.ptm_ratio && Body.GetPosition().y > 500/physicsData.ptm_ratio) || (Body.GetPosition().x > 1090/physicsData.ptm_ratio && Body.GetPosition().y > 500/physicsData.ptm_ratio))
  158.                         Body.GetUserData().gotoAndStop(66);
  159.                     else if(Body.GetUserData().currentFrame == 66)
  160.                         Body.GetUserData().play();
  161.                 }
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement