ActionScript3 在函数内创建对象
我一直在尝试编写一个函数来在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么更改
addACrate()
函数以返回b2Body
而不是设置它,所以您的调用是,或者直接在函数中设置
f1
(而不是使用fallingCrate
参数)。那么它应该可以工作。它与 Flash 传递引用的方式有关。当您将对象传递给函数时,您可以更改该对象内部的属性,但不能使其指向新对象。在您的示例中,
fallingCrate
本质上是一个局部变量,它设置为您在调用addACrate
时传入的任何对象。在您的行中,fallingCrate
现在是指向新对象的局部变量。由于fallingCrate
是本地的,因此当您退出该函数时它会超出范围。Either change the
addACrate()
function to return theb2Body
instead of setting it, so your call isor, set
f1
directly in the function (instead of using thefallingCrate
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 toaddACrate
. In your linefallingCrate
is now a local variable pointing to a new object. AsfallingCrate
is local, it goes out of scope when you exit the function.好吧,您会得到一个空引用异常,因为您可能没有在任何地方设置
f1
的值。解决方案是:在函数中,在末尾使用
f1 =fallingCrate;
或
将函数定义为
,然后将其调用为
f1 = addACrate(arguments);
-- -编辑---
您正在发送对该函数的引用,因此如果不是这一行,它应该按预期工作:
这会重新分配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 endor
Define the function as
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:
This reassigns the value of fallingCrate, so fallingCrate no longer refers to the same object as f1.
The solutions mentioned above still apply :)