WEB开发网      婵犻潧鍊婚弲顐︽偟椤栨稓闄勯柦妯侯槸閻庤霉濠婂骸浜剧紒杈ㄥ笚閹峰懘鎮╅崹顐ゆ殸婵炴垶鎸撮崑鎾趁归悩鐑橆棄闁搞劌瀛╃粋宥夘敃閿濆柊锕傛煙鐎涙ê鐏f繝濠冨灴閹啴宕熼鍡╀紘婵炲濮惧Λ鍕叏閳哄懎绀夋繛鎴濈-楠炪垽鎮归崶褍妲婚柛銊ュ缁傚秹鏁撻敓锟� ---闂佹寧娲╅幏锟�
开发学院图形图像Flash Platform engine using Box2D 阅读

Platform engine using Box2D

 2009-10-18 00:00:00 来源:WEB开发网 闂侀潧妫撮幏锟�闂佸憡鍨电换鎰版儍椤掑倵鍋撳☉娆嶄沪缂傚稄鎷�婵犫拃鍛粶闁靛洤娲ㄩ埀顒佺⊕閵囩偟绱為敓锟�闂侀潧妫撮幏锟�  闂佺ǹ绻楀▍鏇㈠极閻愬搫绾ч柕濠忕細閼割亜顪冪€n剙浠ф繛鍫熷灥椤曘儵顢欓悡搴ば�
核心提示:I am also using the input class included in Box2D, why writing my own one if that one works?At the moment it’s only a bunch of lines starting from the Hel

I am also using the input class included in Box2D, why writing my own one if that one works?

At the moment it’s only a bunch of lines starting from the Hello World example, but starting from tomorrow I’ll code the main character shooting as seen in Rick Triqui.

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import Box2D.Dynamics.*;
 import Box2D.Collision.*;
 import Box2D.Collision.Shapes.*;
 import Box2D.Common.Math.*;
 import General.Input;
 import flash.events.MouseEvent;
 public class HelloWorld extends Sprite {
  public var m_world:b2World;
  public var pixels_in_a_meter:int=30;
  public var bodyDef:b2BodyDef;
  public var boxDef:b2PolygonDef;
  public var m_input:Input;
  public var xspeed:int=0;
  var body:b2Body;
  public function HelloWorld() {
   addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
   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 m_sprite:Sprite;
   m_sprite = new Sprite();
   addChild(m_sprite);
   var dbgDraw:b2DebugDraw = new b2DebugDraw();
   var dbgSprite:Sprite = new Sprite();
   m_sprite.addChild(dbgSprite);
   dbgDraw.m_sprite=m_sprite;
   dbgDraw.m_drawScale=30;
   dbgDraw.m_alpha=1;
   dbgDraw.m_fillAlpha=0.5;
   dbgDraw.m_lineThickness=1;
   dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
   m_world.SetDebugDraw(dbgDraw);
   var bodyDef:b2BodyDef;
   var boxDef:b2PolygonDef;
   var circleDef:b2CircleDef;
   square_tile(250,395,500,10);
   hero(100,370,10,40);
   // Box2D input class - why should I bother making my one?
   m_sprite = new Sprite();
   addChild(m_sprite);
   // input
   m_input=new Input(m_sprite);
  }
  public function square_tile(px:int,py:int,w:int,h:int) {
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(px/pixels_in_a_meter, py/pixels_in_a_meter);
   boxDef = new b2PolygonDef();
   boxDef.SetAsBox(real_pixels(w), real_pixels(h));
   boxDef.friction=0.3;
   boxDef.density=0;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
  }
  public function hero(px:int, py:int, w:int, h:int) {
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(px/pixels_in_a_meter, py/pixels_in_a_meter);
   boxDef = new b2PolygonDef();
   boxDef.SetAsBox(real_pixels(w), real_pixels(h));
   boxDef.density=1.0;
   boxDef.friction=0.3;
   boxDef.restitution=0.2;
   bodyDef.userData = new Sprite();
   bodyDef.userData.name="Player";
   body=m_world.CreateBody(bodyDef);
   body.SetBullet(true);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
  }
  public function real_pixels(n:int) {
   return (n/pixels_in_a_meter/2);
  }
  public function Update(e:Event):void {
   m_world.Step(1/30, 10);
   for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
    if (bb.GetUserData()!=null) {
     if (Input.isKeyPressed(39)) {
      xspeed=3;
     } else if (Input.isKeyPressed(37)) {
      xspeed=-3;
     }
     if (xspeed) {
      bb.WakeUp();
      bb.m_linearVelocity.x=xspeed;
     }
     bb.m_sweep.a = 0;
    }
   }
   Input.update();
  }
 }
}

1 2  下一页

Tags:Platform engine using

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