WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院图形图像Flash A smart way to manage sleeping objects with Bo... 阅读

A smart way to manage sleeping objects with Box2D

 2009-10-31 00:00:00 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劕鎼痪褔鏌曟繝蹇曠窗闁煎壊浜滈—鍐偓锝庡墮娴犙勭箾閸喎鐏ユい鏇樺劦椤㈡瑩鎮℃惔銇帮拷
核心提示:This is CPU saving but can be used for some game concepts too.Just think about Super Stacker, where you have happy and afraid bricks.Super StackerIf you spend s

This is CPU saving but can be used for some game concepts too.

Just think about Super Stacker, where you have happy and afraid bricks.

Super Stacker

If you spend some time on this game, you will find happy bricks are the inactive ones while the afraid ones are still falling/moving.

This can be achieved by playing on the IsSleeping status.

Let’s take the same script introduced in Box2D: tutorial for the absolute beginners and make some changes:

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.utils.Timer;
 import flash.events.TimerEvent;
 import Box2D.Dynamics.*;
 import Box2D.Collision.*;
 import Box2D.Collision.Shapes.*;
 import Box2D.Common.Math.*;
 public class demo extends Sprite {
  var the_world:b2World;
  var time_count:Timer=new Timer(3000);
  public function demo() {
   var environment:b2AABB=new b2AABB  ;
   environment.lowerBound.Set(-100.0,-100.0);
   environment.upperBound.Set(100.0,100.0);
   var gravity:b2Vec2=new b2Vec2(0.0,10.0);
   the_world=new b2World(environment,gravity,true);
   var final_body:b2Body;
   var the_body:b2BodyDef;
   var the_box:b2PolygonDef;
   the_body=new b2BodyDef  ;
   the_body.position.Set(8.5,14);
   the_box=new b2PolygonDef  ;
   the_box.SetAsBox(8.5,0.5);
   the_box.friction=0.3;
   the_box.density=0;
   final_body=the_world.CreateBody(the_body);
   final_body.CreateShape(the_box);
   final_body.SetMassFromShapes();
   addEventListener(Event.ENTER_FRAME,on_enter_frame);
   time_count.addEventListener(TimerEvent.TIMER,on_time);
   time_count.start();
  }
  public function on_time(e:Event) {
   var final_body:b2Body;
   var the_body:b2BodyDef;
   var the_box:b2PolygonDef;
   var box_width=Math.random() + 0.1;
   var box_height=Math.random() + 0.1;
   the_body=new b2BodyDef;
   the_body.position.Set(Math.random() * 10 + 2,0);
   the_body.userData=new cubeface();
   the_body.userData.width=box_width * 60;
   the_body.userData.height=box_height * 60;
   the_box=new b2PolygonDef;
   the_box.SetAsBox(box_width,box_height);
   the_box.friction=0.3;
   the_box.density=1;
   final_body=the_world.CreateBody(the_body);
   final_body.CreateShape(the_box);
   final_body.SetMassFromShapes();
   addChild(the_body.userData);
  }
  public function on_enter_frame(e:Event) {
   the_world.Step(1 / 30,10);
   for (var bb:b2Body=the_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;
     if (bb.IsSleeping()) {
      bb.m_userData.gotoAndStop(2);
     }
     else {
      bb.m_userData.gotoAndStop(1);
     }
    }
   }
  }
 }
}

1 2  下一页

Tags:smart way to

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接