我是flash和box2d的新手。我已經設法解決了box2d的基礎知識沒有麻煩,但直到現在,我一直在使用b2debugdraw函數來顯示我創建的所有對象。所以我決定我應該開始研究如何實際添加精靈或圖像到我的對象。在flash中添加精靈到box2d對象AS3
我已經絕望地在谷歌上搜索了幾個小時,現在我完全沮喪,所以我非常感謝你們中的一位能否幫我弄清楚這一點,我正在嘗試做的將我製作的箱子的圖像/精靈添加到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,但我始終得到相同的錯誤。
我已經看過那些教程,它們實際上是在Flash中不是在C++中,但問題是它們非常過時,他使用的很多類和函數不再適用於新版本的box2d:/我認爲我的問題更多的是在理解事物的閃存方面,我一直在看一些如何導入圖像的Flash樣本,我已經嘗試了在這裏看到的加載器類http://www.java2s .com/Code/Flash-Flex-ActionScript/Graphics/AddingaBitmapFill.htm和其他一些例子,我仍然無法弄清楚! – patrickdamery
你目前的開發設置是什麼?你在Flash的IDE中編碼嗎? –
我設法按照這個教程http://www.streamhead.com/embedding-images/導入它,然後我繼續看看你建議讓圖像跟隨box2d對象的教程,謝謝你的幫助! :) – patrickdamery