ActionScript3 在函数内创建对象

发布于 2024-11-29 05:31:43 字数 1112 浏览 1 评论 0原文

我一直在尝试编写一个函数来在 Box2DFlash 中创建一个简单的圆圈。但它一直告诉我该对象为 null 并且我无法访问它的属性,这是代码:

    public var f1:b2Body;
    public var f2:b2Body;
    public function addACrate(fallingCrate:b2Body, positionX:Number,positionY:Number):void
    {
                var fallingBodyDef:b2BodyDef = new b2BodyDef();
                fallingBodyDef.type = b2Body.b2_dynamicBody;
                fallingBodyDef.position.Set(positionX/ratio,positionY/ratio);

                fallingCrate =_world.CreateBody(fallingBodyDef);

                var fallingCrateShape:b2CircleShape = new b2CircleShape();
                fallingCrateShape.SetRadius(10/ratio);

                var fixtureDef:b2FixtureDef = new b2FixtureDef();
                fixtureDef.shape = fallingCrateShape;
                fixtureDef.density = 0.7;
                fixtureDef.friction = 0.5;
                fixtureDef.restitution = 0.3;//bouncyness

                fallingCrate.CreateFixture(fixtureDef);

    }

    addACrate(f1,270,0);
    trace(f1.GetPosition().y);

当我尝试访问“f1”对象的“y”属性时,它告诉我它是 null 。如果有人能告诉我出了什么问题,我将不胜感激
谢谢

I've been trying to write a function to create a simple circle in Box2DFlash . but it keeps telling me that the object is null and I can't access it's properties here's the code:

    public var f1:b2Body;
    public var f2:b2Body;
    public function addACrate(fallingCrate:b2Body, positionX:Number,positionY:Number):void
    {
                var fallingBodyDef:b2BodyDef = new b2BodyDef();
                fallingBodyDef.type = b2Body.b2_dynamicBody;
                fallingBodyDef.position.Set(positionX/ratio,positionY/ratio);

                fallingCrate =_world.CreateBody(fallingBodyDef);

                var fallingCrateShape:b2CircleShape = new b2CircleShape();
                fallingCrateShape.SetRadius(10/ratio);

                var fixtureDef:b2FixtureDef = new b2FixtureDef();
                fixtureDef.shape = fallingCrateShape;
                fixtureDef.density = 0.7;
                fixtureDef.friction = 0.5;
                fixtureDef.restitution = 0.3;//bouncyness

                fallingCrate.CreateFixture(fixtureDef);

    }

    addACrate(f1,270,0);
    trace(f1.GetPosition().y);

and when I try to access the "y" property of my "f1" object it tells me that it's null . I'll be appreciated if someone can tell me what's wrong
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑寡妇 2024-12-06 05:31:43

要么更改 addACrate() 函数以返回 b2Body 而不是设置它,所以您的调用是

f1 = this.addACrate( 270.0, 0.0 );

,或者直接在函数中设置 f1 (而不是使用 fallingCrate 参数)。

那么它应该可以工作。它与 Flash 传递引用的方式有关。当您将对象传递给函数时,您可以更改该对象内部的属性,但不能使其指向新对象。在您的示例中,fallingCrate 本质上是一个局部变量,它设置为您在调用 addACrate 时传入的任何对象。在您的行中,

fallingCrate = _world.CreateBody(fallingBodyDef);

fallingCrate 现在是指向新对象的局部变量。由于 fallingCrate 是本地的,因此当您退出该函数时它会超出范围。

Either change the addACrate() function to return the b2Body instead of setting it, so your call is

f1 = this.addACrate( 270.0, 0.0 );

or, set f1 directly in the function (instead of using the fallingCrate parameter).

Then it should work. It has to do with the way Flash passes references. When you pass an object to a function, you can change properties inside that object, but you can't make it point to a new object. In your example fallingCrate is essentially a local variable that is set to whatever object you pass in when you make the call to addACrate. In your line

fallingCrate = _world.CreateBody(fallingBodyDef);

fallingCrate is now a local variable pointing to a new object. As fallingCrate is local, it goes out of scope when you exit the function.

遮了一弯 2024-12-06 05:31:43

好吧,您会得到一个空引用异常,因为您可能没有在任何地方设置 f1 的值。解决方案是:

在函数中,在末尾使用 f1 =fallingCrate;

将函数定义为

public function addACrate(fallingCrate:b2Body, positionX:Number,positionY:Number):b2Body

,然后将其调用为 f1 = addACrate(arguments);

-- -编辑---

您正在发送对该函数的引用,因此如果不是这一行,它应该按预期工作:

fallingCrate =_world.CreateBody(fallingBodyDef);

这会重新分配fallingCrate的值,因此fallingCrate不再引用与f1相同的对象。

上面提到的解决方案仍然适用:)

well, you get a Null reference exception because you presumably haven't set the value of f1 anywhere. Solutions to this are:

In the function, use f1 = fallingCrate; at the end

or

Define the function as

public function addACrate(fallingCrate:b2Body, positionX:Number,positionY:Number):b2Body

and then call it as f1 = addACrate(arguments);

---EDIT---

You are sending a reference to the function, so it should work as intended if it weren't for this line:

fallingCrate =_world.CreateBody(fallingBodyDef);

This reassigns the value of fallingCrate, so fallingCrate no longer refers to the same object as f1.

The solutions mentioned above still apply :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文