开发学院图形图像Flash Full Totem Destroyer prototype 阅读

Full Totem Destroyer prototype

 2009-11-12 00:00:00 来源:WEB开发网   
核心提示:Some time ago I posted Create a Flash game like Totem Destroyer, and now I am showing you a complete prototype made by Collin Douch.The prototype features slimy

Some time ago I posted Create a Flash game like Totem Destroyer, and now I am showing you a complete prototype made by Collin Douch.

The prototype features slimy and non-removable blocks and collision detection between the totem and the ground.

Such detection was made customizing Box2D’s b2ContactListener class. For more information about this class refer to Understanding how Box2D manages collisions.

This is the main file:

package{
 import flash.events.*
 import flash.events.MouseEvent
 import flash.display.Sprite
 import flash.text.*
 import Box2D.Dynamics.*
 import Box2D.Collision.*
 import Box2D.Collision.Shapes.*
 import Box2D.Common.Math.*
 
 public class totem extends Sprite{
 
  var boxDef:b2PolygonDef
  var bodyDef:b2BodyDef
  var circleDef:b2CircleDef
  var body:b2Body
  var nx:Number
  var ny:Number
  var nn:String
  var mousePVec:b2Vec2 = new b2Vec2();
  var boxCount:Number = 0
  var bombCount = 3
  var foundTotem:Boolean = false
  var totemFound:b2Body = null
  var n_width:Number
  var n_height:Number
  var n_static:Boolean
  var n_name:String
  var n_destroyable:Boolean
  var n_restitution:Number
  var n_friction:Number
  public function totem(){
   addEventListener(Event.ENTER_FRAME,Update)
   stage.addEventListener(MouseEvent.MOUSE_DOWN,destroyBody)
 
   var worldAABB:b2AABB = new b2AABB();
   worldAABB.lowerBound.Set(-100.0,-100.0)
   worldAABB.upperBound.Set(100.0,100.0)
 
   var gravity:b2Vec2 = new b2Vec2(0.0,10.0)
   var doSleep:Boolean = true
   m_world = new b2World(worldAABB,gravity,doSleep)
 
   var totemContact:b2ContactListener = new b2ContactListener();
   m_world.SetContactListener(totemContact)
 
   findTotem();
   CreateLevel(new Array([275,400,550,100,true,"floor",false,0.3,0.3,"Floor"],[200,340,20,20,false,GetName("Box"),true,1,0.001,"Slimy"],[200,320,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[300,340,20,20,false,GetName("Box"),true,1,0.001,"Slimy"],[300,320,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[250,300,120,20,false,"BG",false,0.3,0.3,"unBreakable"],[200,280,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[200,260,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[200,240,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[300,280,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[300,260,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[300,240,20,20,false,GetName("Box"),true,0.3,0.3,"Block"],[250,220,120,20,false,"BB",false,0.3,0.3,"unBreakable"],[250,205,21.4,33.6,false,"Totem",false,0.3,0.3,"Totem"]))
  }
  public function GetName(possName:String){
   var nameing = "Box"+boxCount
   return "Box"+boxCount;
   boxCount++
  }
  public function CreateLevel(LevelArray:Array){
   for(var a:int=0;a<LevelArray.length;a++){
    for(var b:int = 0;b<10;b++){
     switch(b){
      case 0:
      nx = LevelArray[a][b]
      break;
 
      case 1:
      ny = LevelArray[a][b]
      break;
 
      case 2:
      n_width = LevelArray[a][b]
      break;
 
      case 3:
      n_height = LevelArray[a][b]
      break;
 
      case 4:
      n_static = LevelArray[a][b]
      break;
 
      case 5:
      if(LevelArray[a][b]=="Box0"){
       n_name = "Box"+boxCount
      }
      else{
      n_name = LevelArray[a][b]
      }
      boxCount++
      break;
 
      case 6:
      n_destroyable = LevelArray[a][b]
      break;
 
      case 7:
      n_restitution = LevelArray[a][b]
      break;
 
      case 8:
      n_friction = LevelArray[a][b]
      break;
 
      case 9:
      CreateBox(nx,ny,n_width,n_height,n_static,n_name,n_destroyable,n_restitution,n_friction,Decode(LevelArray[a][b],"iden"))
      break;
     }
 
    }
   }
  }
 
  public function CreateBox(x:Number,y:Number,width:Number,height:Number,static:Boolean,name:String,destroyable:Boolean,restitution:Number,friction:Number,Cover){
   boxDef = new b2PolygonDef();
   boxDef.SetAsBox(GetB2Nums(width,true),GetB2Nums(height,true))
   if(!static){
   boxDef.density = 1.0
   }
   else{
    boxDef.density = 0.0
   }
   boxDef.friction = friction
   boxDef.restitution = restitution
   bodyDef = new b2BodyDef();
   bodyDef.position.x = GetB2Nums(x,false)
   bodyDef.position.y = GetB2Nums(y,false)
   bodyDef.userData = Cover
   bodyDef.userData.name = name
   bodyDef.userData.destroyable = destroyable
   bodyDef.userData.width = 30 * 2 * GetB2Nums(width,true)
   bodyDef.userData.height = 30 * 2* GetB2Nums(height,true)
   body = m_world.CreateBody(bodyDef)
   body.CreateShape(boxDef)
   body.SetMassFromShapes();
   addChild(bodyDef.userData)
  }
 
  public function Decode(WTD:String,Encryption:String){
   switch(Encryption){
    case "iden":
    if(WTD =="Block"){
     return new Block();
    }
    if(WTD == "Floor"){
     return new Floor();
    }
    if(WTD == "Totem"){
     return new Totem();
    }
    if(WTD == "unBreakable"){
     return new unBreakable();
    }
    if(WTD == "Slimy"){
     return new Slimy();
    }
    break;
   }
  }
 
  public function CreateCircle(x:Number,y:Number,radius:Number,static:Boolean){
   circleDef = new b2CircleDef();
   circleDef.radius = GetB2Nums(radius,true)
   if(static){
   circleDef.density = 0.0
   }
   else{
    circleDef.density = 1.0
   }
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(GetB2Nums(x,false),GetB2Nums(y,false))
   body = m_world.CreateBody(bodyDef)
   body.CreateShape(circleDef)
   body.SetMassFromShapes();
  }
 
  public function destroyBody(evt:MouseEvent):void{
   var selectedShape = GetBodyAtMouse();
   var user = selectedShape.GetUserData().name
   var susceptable = selectedShape.GetUserData().destroyable
   var Child = getChildByName(user)
    if(selectedShape){
     if(susceptable){
   m_world.DestroyBody(selectedShape)
   removeChild(Child)
   Child = null
   user = null
   selectedShape = null
     }
   }
  }
 
  public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {
   var mouseXWorldPhys = (mouseX)/30;
   var mouseYWorldPhys = (mouseY)/30;
   mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
   var aabb:b2AABB = new b2AABB();
   aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
   aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
   var k_maxCount:int=10;
   var shapes:Array = new Array();
   var count:int=m_world.Query(aabb,shapes,k_maxCount);
   var body:b2Body=null;
   for (var i:int = 0; i < count; ++i) {
    if (shapes[i].GetBody().IsStatic()==false||includeStatic) {
     var tShape:b2Shape=shapes[i] as b2Shape;
     var inside:Boolean=tShape.TestPoint(tShape.GetBody().GetXForm(),mousePVec);
     if (inside) {
      body=tShape.GetBody()
      break;
     }
    }
   }
   return body;
  }
   public function findTotem(includeStaticBodies:Boolean = false){
    for (var tm:b2Body = m_world.m_bodyList; tm; tm = tm.m_next){
    if (tm.m_userData is Sprite){
     if(tm.m_userData.name=="Totem" && foundTotem==false){
      totemFound==tm
      foundTotem=true
      break;
     }
    }
    }
   }
 
  public function GetB2Nums(Num:Number,WH:Boolean){
   switch(WH){
    case true:
    return Num/30/2
    break;
 
    case false:
    return Num/30
    break;
   }
  }
 
  public function Update(e:Event):void{
   m_world.Step(m_timeStep,m_iterations)
   for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
    if (bb.m_userData is Sprite){
     bb.m_userData.x = bb.GetPosition().x * 30;
     bb.m_userData.y = bb.GetPosition().y * 30;
     bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
    }
    }
 
  }
 
  public var m_world:b2World
  public var m_timeStep:Number = GetB2Nums(1.0,false)
  public var m_iterations:int = 10
  public var lose:Boolean = false
 }
}

1 2  下一页

Tags:Full Totem Destroyer

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接