【问题标题】:Adding sprite to box2d object in flash AS3在 Flash AS3 中将精灵添加到 box2d 对象
【发布时间】:2012-12-16 17:03:04
【问题描述】:

我对 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,但我总是收到相同的错误。

【问题讨论】:

    标签: actionscript-3 flash box2d flashdevelop


    【解决方案1】:

    在这里,这是一个很长的 box2d 教程列表。至于将精灵添加到 Box2d,您的想法是错误的。 Box2d 是一个模拟实时物理的物理引擎。使用 DebugDraw 将为您的形状提供基本图形。

    渲染和物理模拟是两个相互独立的“线程”。

    您需要做的是让您的图形模拟形状所在的位置。因此,在您的 OnEnterFrame 事件中,您应该让您的精灵从您的 box2d 形状中获取位置、旋转和缩放。也不要忘记 box2d 是公制,而 flash 是像素,使用转换常量。一切都在下面的链接中进行了解释,请花点时间阅读。

    http://www.kerp.net/box2d/

    希望这会有所帮助:)

    【讨论】:

    • 我已经看过那些教程,它们实际上是在 Flash 中而不是在 C++ 中,但问题是它们非常过时,他使用的许多类和函数不再起作用对于新版本的box2d:/我认为我的问题更多在于理解事物的flash方面,我一直在查看一些有关如何导入图像的flash示例,我已经尝试过这里看到的加载器类@987654322 @ 和他们有的其他一些例子,我还是想不通!
    • 您当前的开发设置是什么?你在 Flash 的 IDE 中编码吗?
    • 我设法按照本教程streamhead.com/embedding-images 导入它,然后我继续查看您建议让图像跟随box2d 对象的教程,感谢您的帮助! :)
    • 您也可以嵌入SWC格式的图形库。您进入 Flash 的 IDE,然后制作一个影片剪辑,将其导出为 actionscript(在属性中),在您的发布设置中检查导出为 SWC 并编译它。您应该有一个 .SWC 文件,然后您可以将其导入 FDT / FlashBuilder。因此,如果您为动作脚本导出了一个影片剪辑,您可以创建一个新图形,如果这种性质,var mc:MovieClip = new MYExportedMovieClip();
    【解决方案2】:

    这一行非常错误:

    asprite.graphics.beginBitmapFill(crate.jpg, null, false, false);
    

    板条箱.jpg?这应该是一个位图数据实例,但我认为 crate.jpg 不是有效的实例名称。

    除了您已经接受了另一个答案作为最好的答案之外,它甚至无法回答您的问题:访问未定义的属性箱。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      相关资源
      最近更新 更多