저는 플래시와 box2d에 새로워졌습니다. 나는 box2d의 기초를 돌아 다닐 수 있었지만 지금까지는 b2debugdraw 함수를 사용하여 내가 만든 모든 객체를 표시했습니다. 그래서 실제로 객체에 스프라이트 나 이미지를 추가하는 방법을 알아보기 시작했습니다.플래시 AS3에서 box2d 객체에 스프라이트 추가하기
저는 몇 시간 동안 Google에서 절망적으로 검색해 왔으며, 지금이 시점에서 완전히 좌절감을 느낍니다. 그렇다면 여러분 중 한 분이이 문제를 해결할 수있는 손을 제공 할 수 있다면 정말 고맙겠습니다. 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를 비롯한 다양한 형식의 이미지가 있지만 항상 동일한 오류가 발생합니다.
이미 자습서를 살펴 봤는데 실제로 C++이 아닌 플래시에 있지만 문제는 구식이므로 많이 사용하는 클래스와 기능이 더 이상 새 버전에서 작동하지 않는다는 것입니다. box2d :/내 문제는 물건을 이해하는 플래시 측면에서 더 많은 것, 나는 이미지를 가져 오는 방법에 대한 몇 가지 플래시 샘플을보고 있었고, 여기에 표시된 것처럼 로더 클래스를 시도했다. http : //www.java2s .com/Code/Flash-Flex-ActionScript/Graphics/AddingaBitmapFill.htm과 그 밖의 몇 가지 예가 있지만 아직 이해할 수 없습니다. – patrickdamery
현재 사용중인 dev 설정은 무엇입니까? Flash IDE에서 코딩하고 있습니까? –
나는이 자습서 http://www.streamhead.com/embedding-images/에 따라 가져 오기를 할 수 있었고, 그 다음에는 도움을 주신 덕분에 이미지가 box2d 객체를 따르도록 제안한 자습서를 살펴 보았습니다. ! :) – patrickdamery