Box2D墙 - 相同的代码:左边的一个工作,右边的工作没有

时间:2010-01-14 01:38:19

标签: actionscript-3 actionscript box2d

我正在制作一个简单的Box2D游戏(http://iwanttobeacircle.com),你可以从三角形开始,然后从更大的形状中跳出来以获得优势。

我的墙壁上有一个奇怪的错误......两个都是从同一个班级创建的,但是左侧是有效的,右侧没有。如果我只添加正确的,那么它可以工作,但由于某种原因添加它们似乎都在某处造成问题。

WallSegment类如下:

    package com.carmstrong.iwanttobeacircle {

 import flash.display.DisplayObjectContainer;
 import flash.display.Sprite;
 import flash.display.Shape;
 import Box2D.Collision.Shapes.*;
 import Box2D.Dynamics.*;
 import Box2D.Common.Math.b2Vec2;

 import com.carmstrong.iwanttobeacircle.Config;

 public class WallSegment extends Actor {

  private var _shape:Sprite;
  private var _shapeBody:b2Body;
  private var _colour:uint = 0x666666;

  private var prevEdge:int;
  private var thisEdge:int;
  private var side:Number;

  private var name:String;

  private var _pathWidth:int;
  private var _parent:DisplayObjectContainer;

  public function WallSegment(Width:int, Position:String, Parent:DisplayObjectContainer) {
   name = "Wall";
   _pathWidth = Width;
   _parent = Parent;

   if(Position == "left") {
    side = 1;
    prevEdge = (Config.WIDTH - Config.PREV_WIDTH)/2;
    thisEdge = (Config.WIDTH-_pathWidth)/2;
   } else {
    side = -1;
    prevEdge = Config.WIDTH-(Config.WIDTH - Config.PREV_WIDTH)/2;
    thisEdge = Config.WIDTH-(Config.WIDTH-_pathWidth)/2;
   }// check if its left or right wall


   //Create the costume
   drawShape();
   drawBody();

   super(_shapeBody, _shape);
  }//DynamicWall

  private function drawShape():void {
   //Draw visual
   _shape = new Sprite();

   var left:Sprite = new Sprite();

   left.graphics.beginFill(_colour, 0.5);
   left.graphics.moveTo(prevEdge, Config.HEIGHT/2);
   left.graphics.lineTo(prevEdge-Config.WIDTH*side, Config.HEIGHT/2);
   left.graphics.lineTo(thisEdge-Config.WIDTH*side, -Config.HEIGHT/2);
   left.graphics.lineTo(thisEdge, -Config.HEIGHT/2);
   left.graphics.endFill();
   _shape.addChild(left);

   _parent.addChild(_shape);
  }//drawShape

  private function drawBody():void {
   //Create the shape definition
   var shapeDef:b2PolygonDef = new b2PolygonDef();
   shapeDef.vertexCount = 4;
   b2Vec2(shapeDef.vertices[0]).Set(prevEdge/Config.RATIO, Config.HEIGHT/2/Config.RATIO);
   b2Vec2(shapeDef.vertices[1]).Set((prevEdge-Config.WIDTH*side)/Config.RATIO, Config.HEIGHT/2/Config.RATIO);
   b2Vec2(shapeDef.vertices[2]).Set((thisEdge-Config.WIDTH*side)/Config.RATIO, -Config.HEIGHT/2/Config.RATIO);
   b2Vec2(shapeDef.vertices[3]).Set(thisEdge/Config.RATIO, -Config.HEIGHT/2/Config.RATIO);
   shapeDef.density = 0;
   shapeDef.friction = 10;
   shapeDef.restitution = 0.45;

   //Create the body definition (specify location here)
   var shapeBodyDef:b2BodyDef = new b2BodyDef();
   shapeBodyDef.position.Set(0, -Config.HEIGHT*(Config.CURRENT_SEGMENT+1)/Config.RATIO);

   //Create the body
   _shapeBody = Config.world.CreateBody(shapeBodyDef);

   //Create the shape
   _shapeBody.CreateShape(shapeDef);

  }//drawBody

 }//class

    }//package

为了保持关卡动态,每次在主类中使用以下代码将墙绘制在玩家对象的前面:

private function addWall(Width:int) {      
   Config.CURRENT_SEGMENT++;

   //addWalls
   var leftWall:WallSegment = new WallSegment(Width, "left",camera);
   var rightWall:WallSegment = new WallSegment(Width, "right",camera);

   Config.PREV_WIDTH = Width;
  }//addWall

我收到了这个错误:

  

TypeError:错误#1010:术语是   未定义且没有属性。在   com.carmstrong.iwanttobeacircle :: GameContactListener /添加()    在   Box2D.Dynamics.Contacts :: b2CircleContact /评估()    在   Box2D.Dynamics.Contacts :: b2Contact /更新()    在   Box2D.Dynamics :: b2ContactManager /碰撞()    在Box2D.Dynamics :: b2World / Step()at   com.carmstrong.iwanttobeacircle :: IWantToBeACircle / everyFrame()

其中引用了GameContactListener类,如下所示(add函数位于底部):

    package com.carmstrong.iwanttobeacircle {

 import Box2D.Collision.b2ContactPoint;
 import Box2D.Dynamics.b2ContactListener;

 public class GameContactListener extends b2ContactListener {

  public function GameContactListener() {

  }//GameContactListener  

  override public function Add(point:b2ContactPoint):void {

   if (point.shape1.GetBody().GetUserData() is ShapeActor && point.shape2.GetBody().GetUserData() is ShapeActor) {
    //trace("Two shapes collided: Shape 1 has "+ point.shape1.GetBody().GetUserData().sides + " sides and Shape 2 has " + point.shape2.GetBody().GetUserData().sides + " sides");

    if (point.shape1.GetBody().GetUserData().sides > point.shape2.GetBody().GetUserData().sides) {
     //remove side from shape 1 and add side to shape 2
     point.shape1.GetBody().GetUserData().sides--;
     point.shape2.GetBody().GetUserData().sides++;
     //point.shape2.GetBody().GetUserData().updateColour;
    } else if (point.shape1.GetBody().GetUserData().sides < point.shape2.GetBody().GetUserData().sides) {
     //remove side from shape 2 and add side to shape 1
     point.shape1.GetBody().GetUserData().sides++;
     point.shape2.GetBody().GetUserData().sides--;
     //point.shape2.GetBody().GetUserData().updateColour;
    }// add side to smaller shape and take away from larger shape

    if(point.shape1.GetBody().GetUserData().name == "player" || point.shape2.GetBody().GetUserData().name == "player") {


     if(point.shape1.GetBody().GetUserData().name == "player" && point.shape2.GetBody().GetUserData().sides <= point.shape1.GetBody().GetUserData().sides) {
      Config.FULFILLMENT++;
      Config.SOUNDS[3+Math.ceil(Math.random()*5)][1].play();
      trace(Config.FULFILLMENT);
     } else if (point.shape2.GetBody().GetUserData().name == "player" && point.shape1.GetBody().GetUserData().sides <= point.shape2.GetBody().GetUserData().sides) {
      Config.FULFILLMENT++;
      Config.SOUNDS[Math.ceil(Math.random()*5)][1].play();
      trace(Config.FULFILLMENT);
     } else {
      Config.SOUNDS[Math.ceil(Math.random()*3)][1].play();
      Config.FULFILLMENT = int(Config.FULFILLMENT - 5);
      trace(Config.FULFILLMENT);
     }//if other shape is less than or equal to player
    }//if one of the shapes is player
   }// if both collider objects are shapes

   super.Add(point);

  }// override Add

 }//class 

    }//package

我很感激任何想法或想法。此外,这是我第一次参加Box2D,所以我将非常感谢如何提高我的代码效率。

提前致谢

1 个答案:

答案 0 :(得分:0)

你在这里问两个问题

  1. 为什么左右墙碰撞检测不起作用?
  2. 为什么GameContactListener / Add()方法出错?
  3. 看起来他们可能有相同的根问题。我看到你正在添加墙段作为“相机”的孩子(必须追踪这个添加发生的地方......你将“相机”的引用传递给对象的构造函数。)

    i.e.    var leftWall:WallSegment = new WallSegment(Width, "left",camera);
    

    在那里,您将leftWall添加为该对象的子级。但是我不确定“相机”到底是什么......这部分游戏世界是对象吗?

    另外,你的point.shape1和point.shape2的跟踪语句是什么? 2个物体碰撞了什么?