Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- // import stuff
- import flash.display.Bitmap;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.utils.getDefinitionByName;
- import flash.display.DisplayObject;
- import flash.display.MovieClip;
- // import box2d
- import Box2D.Dynamics.Joints.*;
- import Box2D.Dynamics.*;
- import Box2D.Collision.*;
- import Box2D.Collision.Shapes.*;
- import Box2D.Common.Math.*;
- import flash.events.*;
- // import the sprite data
- import PhysicsData;
- public class Main extends Sprite {
- public var nextSprite:Number=10;
- // create an object containing the shape data
- public var physicsData:PhysicsData = new PhysicsData();
- // create the box2d world, set up gravity
- public var world:b2World=new b2World(new b2Vec2(0,10.0),true);
- public function Main():void {
- // Grab the bitmap from the compiled resources
- var bdArt = new backdrop();
- var beaker:MovieClip = new fleaker();
- var dum:MovieClip = new dummy();
- //var artClass:Class = getDefinitionByName("cupunder") as Class;
- //var artClass2:Class = getDefinitionByName("cupover") as Class;
- //var artClass3:Class = getDefinitionByName("dummy") as Class;
- // Creat a display object to add to the stage
- //var bitmap:Bitmap = new Bitmap(new artClass(0, 0), "auto", true);
- //var bitmap2:Bitmap = new Bitmap(new artClass2(0, 0), "auto", true);
- //var bitmap3:Bitmap = new Bitmap(new artClass2(0, 0), "auto", true);
- // create a physics body from the physics data structure
- var body:b2Body=physicsData.createBody("beaker",
- world,
- b2Body.b2_staticBody,
- beaker);
- var floor:b2Body=physicsData.createBody("floor", world, b2Body.b2_staticBody, dum);
- // set the position of the sprite
- body.SetPositionAndAngle(
- new b2Vec2(800/physicsData.ptm_ratio,460/physicsData.ptm_ratio), 0);
- floor.SetPositionAndAngle(
- new b2Vec2(-400/physicsData.ptm_ratio,460/physicsData.ptm_ratio), 0);
- //bitmap2.x = 600;
- //bitmap2.y = 337;
- //bitmap3.x = -5;
- //bitmap3.y = -5;
- addChildAt(bdArt, 0);
- // add the sprite to the scene
- //addChildAt(bitmap, 1);
- //addChildAt(bitmap3, 1);
- //addChildAt(bitmap2, 2);
- addChildAt(beaker, 1);
- // add an event listener to update sprite's from
- // physics data
- addEventListener(Event.ENTER_FRAME, update, false, 0, true);
- stage.addEventListener(MouseEvent.MOUSE_DOWN, addNewSprites);
- }
- // this function adds new sprites to the scene
- public function addNewSprites(e:MouseEvent):void {
- // only add sprites every 30th frame
- /*nextSprite--;
- if(nextSprite > 0)
- {
- return;
- }
- nextSprite = 30;*/
- // names of the items / sprites we use
- var items:Array=["blueberry2",
- "blueberry3"];
- var mcbb = new Blueberry();
- // choose a random object
- var name:String=items[Math.floor(Math.random()*items.length)];
- // Grab the bitmap from the compiled resources
- var artClass:Class = getDefinitionByName(name) as Class;
- // Creat a display object to add to the stage
- var bitmap:Bitmap = new Bitmap(new artClass(0, 0), "auto", true);
- // create a physics body from the physics data structure
- var body:b2Body=physicsData.createBody("bb",
- world,
- b2Body.b2_dynamicBody,
- mcbb);
- body.SetUserData(mcbb);
- // set a random start position
- body.SetPositionAndAngle(new b2Vec2(Math.random()*11 + 26,-4), 0);
- //body.SetUserData(texture);
- // add the sprite to the scene
- addChildAt(mcbb, 1);
- }
- // this function runs the world simulation
- // and adjusts the sprites according to the positions
- // of the objects in the simulation
- public function update(e : Event):void {
- // step the world
- world.Step(1 / 30, 10, 10);
- world.ClearForces();
- // add new sprites to the world
- //addNewSprites();
- // update the sprites from the physics data
- var Body:b2Body = world.GetBodyList();
- for ( ; Body; Body = Body.GetNext()) {
- if (Body.GetUserData()!=null) {
- // get the user date value from the body
- //var bb:MovieClip=Body.GetUserData() as MovieClip;
- // extract the position and set the sprite to it
- Body.GetUserData().x=Body.GetPosition().x*physicsData.ptm_ratio;
- Body.GetUserData().y=Body.GetPosition().y*physicsData.ptm_ratio;
- var dx = Body.GetPosition().x*physicsData.ptm_ratio - 960;
- var dy = Body.GetPosition().y*physicsData.ptm_ratio;
- var degrees = (Math.atan2(dy, dx) * 180 / Math.PI);
- // set the rotation of the sprite
- Body.GetUserData().rotation = Body.GetAngle() * (180/Math.PI);
- //Body.GetUserData().spec.rotation = degrees -90;
- 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))
- Body.GetUserData().gotoAndStop(66);
- else if(Body.GetUserData().currentFrame == 66)
- Body.GetUserData().play();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement