我有四节课, MAIN,世界(包含所有变量),Level(敌人和砖块的代码)和Level1
主要课程
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.*;
import Level;
import world;
public class Main extends Sprite {
public var enemy: enemyMc = new enemyMc();
public var worldVar:world = new world();
public var bric:Brick;
public function Main() {
var bg: backgroundMc = new backgroundMc();
addChild(worldVar.m_sprite);
worldVar.m_sprite.addChild(bg);
worldVar.m_sprite.addChild(enemy);
var level = new Level(this, this.stage);
}
}
}
世界级
package {
import Box2D.Dynamics.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Sprite;
public class world {
public var gravity: b2Vec2 = new b2Vec2(0, 9.81); // define gravity
public var createWorld: b2World = new b2World(gravity, true); // add gravity and put to sleep if not active to true
public var worldScale: int = 30; // convert meters into pixels
public var enemySphere: b2Body;
public var timeStep = 1 / 60;
public var enemyPlacementX = 210;
public var enemyPlacementY = 325;
public var m_sprite:Sprite = new Sprite();
public function world() {
addWall(800, 10, 0, 490); //floor
addWall(800, 10, 0, -5);// ceiling
addWall(10, 600, 0, 0); // left
addWall(10, 600, 800, 0); // right
// constructor code
}
private function addWall(w, h, px, py): void {
var floorShape: b2PolygonShape = new b2PolygonShape();
floorShape.SetAsBox(w / worldScale, h / worldScale);
var floorFixture: b2FixtureDef = new b2FixtureDef();
floorFixture.density = 0;
floorFixture.friction = 10;
floorFixture.restitution = 0.2;
floorFixture.shape = floorShape;
var floorBodyDef: b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set(px / worldScale, py / worldScale);
var floor: b2Body = createWorld.CreateBody(floorBodyDef);
floor.CreateFixture(floorFixture);
}
}
}
Level类,它从Main获取一个阶段引用 class还具有更新功能,可以检测box2D世界中的任何冲突
public function updateWorld(e: Event): void {
var velIterations: int = 10; // times velocity of objects get updated
var posIterations: int = 10; // adjusts positions to avoid overlap
worldVar.createWorld.Step(worldVar.timeStep, velIterations, posIterations); // setup the bodies in the world update speed 10, 10 is a safe number
for (var currentBody: b2Body = worldVar.createWorld.GetBodyList(); currentBody; currentBody = currentBody.GetNext()) {
if (currentBody.GetUserData()) {
currentBody.GetUserData().x = currentBody.GetPosition().x * worldVar.worldScale;
currentBody.GetUserData().y = currentBody.GetPosition().y * worldVar.worldScale;
currentBody.GetUserData().rotation = currentBody.GetAngle() * (180 / Math.PI);
}
}
worldVar.createWorld.ClearForces(); // clear forces to let the simulation start again
worldVar.createWorld.DrawDebugData();
}
它还有另一个创建砖块的功能
public function brick(pX: int, pY: int, w: Number, h: Number, stg:Stage): void {
var bric = new Brick();
stg.addChild(bric); //assign it to the stage
bric.x = pX;
bric.y = pY;
bric.width = w;
bric.height = h;
var polygonShape: b2PolygonShape = new b2PolygonShape();
var polygonFixture: b2FixtureDef = new b2FixtureDef();
polygonShape.SetAsBox(w / 2 / worldVar.worldScale, h / 2 / worldVar.worldScale);
polygonFixture.shape = polygonShape;
polygonFixture.density = 1;
polygonFixture.restitution = 0.5;
polygonFixture.friction = 0.9;
var brickbodyDef: b2BodyDef = new b2BodyDef();
brickbodyDef.type=b2Body.b2_dynamicBody;
brickbodyDef.userData = bric;
brickbodyDef.position.Set(bric.x / worldVar.worldScale, bric.y / worldVar.worldScale);
var theBrick: b2Body = worldVar.createWorld.CreateBody(brickbodyDef);
theBrick.CreateFixture(polygonFixture);
}
碰撞检测在砖块和敌人之间运作良好,直到我将砖块代码移动到另一个类别中,然后我看到的就是在悬挂或浮动状态下放置在舞台上的砖块,没有碰撞检测。
注意:敌人和墙壁之间的碰撞仍然有效 虽然墙壁也是在另一个班级创造的世界" 不知道这里有什么问题。
这是Level1类不起作用
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import world;
public class Level1 {
private var worldVar:world = new world();
public function Level1() {
}
public function brick(pX: int, pY: int, w: Number, h: Number, stg:Stage): void {
var bric = new Brick();
stg.addChild(bric);
bric.x = pX;
bric.y = pY;
bric.width = w;
bric.height = h;
var polygonShape: b2PolygonShape = new b2PolygonShape();
var polygonFixture: b2FixtureDef = new b2FixtureDef();
polygonShape.SetAsBox(w / 2 / worldVar.worldScale, h / 2 / worldVar.worldScale);
polygonFixture.shape = polygonShape;
polygonFixture.density = 1;
polygonFixture.restitution = 0.5;
polygonFixture.friction = 0.9;
var brickbodyDef: b2BodyDef = new b2BodyDef();
brickbodyDef.type=b2Body.b2_dynamicBody;
brickbodyDef.userData = bric;
brickbodyDef.position.Set(bric.x / worldVar.worldScale, bric.y / worldVar.worldScale);
var theBrick: b2Body = worldVar.createWorld.CreateBody(brickbodyDef);
theBrick.CreateFixture(polygonFixture);
}
}
}
以下是我在Level类中进行的调用
level1.brick(535, 214, 215, 120, stg);