我对 flash 和 box2d 很陌生。我已经设法绕过 box2d 的基础知识,但到目前为止,我一直在使用 b2debugdraw 函数来显示我创建的所有对象。所以我决定我应该开始研究如何将精灵或图像实际添加到我的对象中。
我已经绝望地在谷歌上搜索了几个小时,在这一点上我完全感到沮丧,所以如果你们中的一个人能帮我解决这个问题,我真的很感激,我要做的就是添加图像/我用 box2d 制作的正方形的板条箱的精灵。
这是我最近的尝试:
package
{
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2Fixture;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author Robert Damery
*/
public class Main extends Sprite
{
//World object
public var world:b2World;
//Scale number
public const scale:int = 30;
//Time Counter
public var Counter:int = 60;
public var boxbody:Sprite;
public function Main():void
{
var asprite:Sprite;
asprite.graphics.beginBitmapFill(crate.jpg, null, false, false);
asprite.graphics.drawRect(0, 0, 25, 25);
asprite.graphics.endFill();
asprite.x = 100;
asprite.y = 100;
stage.addChild(asprite);
// create world
CreateWorld();
//Create a box function
CreateBox(300, 600, 600, 25, false, .8);
CreateBox(0, 600, 25, 600, false, .8);
CreateBox(800, 0, 25, 600, false, .8);
CreateBox(400, 100, 25, 25, true, .8);
//Make frames pass in flash
addEventListener(Event.ENTER_FRAME, newframeevent);
//Draw our debug data
debug_draw();
}
//Event handler function, makes time go by
private function newframeevent(e:Event):void
{
world.Step(1 / 30, 10, 10);
world.ClearForces();
world.DrawDebugData();
}
private function CreateWorld():void
{
//Size of World
var worldsize:b2AABB = new b2AABB();
worldsize.lowerBound.Set(-500, -500);
worldsize.upperBound.Set(500 , 500);
//Define Gravity
var gravity:b2Vec2 = new b2Vec2(0 , 9.8);
// Ignore sleeping objects
var doSleep:Boolean = true;
world = new b2World(gravity, doSleep);
}
private function CreateBox(x:Number, y:Number, width:Number, height:Number, is_Dynamic:Boolean, density:Number):b2Body
{
x = con2D(x);
y = con2D(y);
width = con2D(width);
height = con2D(height);
//Create the body definition
var floorshapedef:b2BodyDef = new b2BodyDef();
floorshapedef.position.Set(x, y);
//Determine whether object is dynamic or not
if (is_Dynamic == true)
{
floorshapedef.type = b2Body.b2_dynamicBody;
}
else
{
floorshapedef.type = b2Body.b2_staticBody;
}
//Create the shape
var floorshape:b2PolygonShape = new b2PolygonShape();
floorshape.SetAsBox(width, height);
//Create the fixture
var floorfixture = new b2FixtureDef();
floorfixture.shape = floorshape;
floorfixture.density = density;
floorfixture.restitution = .5;
floorfixture.friction = .25;
//Create body
var floorbody:b2Body = world.CreateBody(floorshapedef);
floorbody.CreateFixture(floorfixture);
return floorbody;
}
//Debug Draw function
public function debug_draw():void
{
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
}
public function con2D(num:Number):Number
{
return num / scale;
}
}
}
我已经知道我什至没有尝试将精灵附加到盒子上,但那是因为我什至无法让盒子出现。当我运行此特定代码时,我收到一条错误消息:访问未定义的属性箱。
我有不同格式的相同图像,包括 .fla,但我总是收到相同的错误。