2010-01-14 12 views
0

我正在製作一個簡單的Box2D遊戲(http://iwanttobeacircle.com),其中你以三角形開始並從較大的形狀反彈以獲得邊。Box2D牆 - 相同的代碼:左邊的工程,右邊的不是

我與我的牆壁有一個奇怪的錯誤...都是從同一個班級創建的,但左側的工程和右側沒有。如果我只是添加正確的,那麼它的工作原理,但出於某種原因,添加它們都似乎在某個地方引起問題。

的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: Error #1010: A term is undefined and has no properties. at com.carmstrong.iwanttobeacircle::GameContactListener/Add() at Box2D.Dynamics.Contacts::b2CircleContact/Evaluate() at Box2D.Dynamics.Contacts::b2Contact/Update() at Box2D.Dynamics::b2ContactManager/Collide() at Box2D.Dynamics::b2World/Step() at com.carmstrong.iwanttobeacircle::IWantToBeACircle/everyFrame()

它指的是GameContactListener類,如下圖所示(添加功能在底部):

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,因此希望瞭解如何使代碼更高效的任何提示。

在此先感謝

+0

它可能有助於縮小Add()方法中哪些數據爲null - 有很多選項:point,point.shape1,point.shape1.GetBody()等。 – 2010-01-14 22:32:48

回答

0

你問兩個問題在這裏

  1. 爲什麼左,右側壁碰撞檢測不工作?
  2. 爲什麼GameContactListener/Add()方法有錯誤?

看起來他們可能有相同的根本問題。我看到您將牆段添加爲「相機」的子項(必須跟蹤發生此添加的位置......您將「相機」的引用傳遞到對象的構造函數中。)

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

在那裏,您將leftWall添加爲該對象的子項。但我不確定什麼是「相機」......這是遊戲世界對象的這一部分嗎?

另外,你的trace.shape1和point.shape2的跟蹤語句是什麼? 2個物體碰撞什麼?