<small id='Jl9B8'></small><noframes id='Jl9B8'>

    1. <legend id='Jl9B8'><style id='Jl9B8'><dir id='Jl9B8'><q id='Jl9B8'></q></dir></style></legend>
      <i id='Jl9B8'><tr id='Jl9B8'><dt id='Jl9B8'><q id='Jl9B8'><span id='Jl9B8'><b id='Jl9B8'><form id='Jl9B8'><ins id='Jl9B8'></ins><ul id='Jl9B8'></ul><sub id='Jl9B8'></sub></form><legend id='Jl9B8'></legend><bdo id='Jl9B8'><pre id='Jl9B8'><center id='Jl9B8'></center></pre></bdo></b><th id='Jl9B8'></th></span></q></dt></tr></i><div id='Jl9B8'><tfoot id='Jl9B8'></tfoot><dl id='Jl9B8'><fieldset id='Jl9B8'></fieldset></dl></div>
        • <bdo id='Jl9B8'></bdo><ul id='Jl9B8'></ul>

      1. <tfoot id='Jl9B8'></tfoot>

        Cocos2d-iPhone 与 Box2D:CCPhysicsSprite EXC_BAD_ACCESS

        时间:2024-08-11
      2. <i id='muF8W'><tr id='muF8W'><dt id='muF8W'><q id='muF8W'><span id='muF8W'><b id='muF8W'><form id='muF8W'><ins id='muF8W'></ins><ul id='muF8W'></ul><sub id='muF8W'></sub></form><legend id='muF8W'></legend><bdo id='muF8W'><pre id='muF8W'><center id='muF8W'></center></pre></bdo></b><th id='muF8W'></th></span></q></dt></tr></i><div id='muF8W'><tfoot id='muF8W'></tfoot><dl id='muF8W'><fieldset id='muF8W'></fieldset></dl></div>
          <tfoot id='muF8W'></tfoot>

                  <tbody id='muF8W'></tbody>

              1. <small id='muF8W'></small><noframes id='muF8W'>

                <legend id='muF8W'><style id='muF8W'><dir id='muF8W'><q id='muF8W'></q></dir></style></legend>

                  <bdo id='muF8W'></bdo><ul id='muF8W'></ul>
                • 本文介绍了Cocos2d-iPhone 与 Box2D:CCPhysicsSprite EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近才开始搞乱 cocos2d 的 Box2D 集成,虽然大部分过程都很简单直接,但在使用 CCPhysicsSprite(将 b2body 与 sprite 集成的 CCSprite 子类)时,我一直遇到 EXC_BAD_ACCESS 错误.我使用的代码是:

                  I just recently started messing with cocos2d's Box2D integration, while most of the process has been simple and straight forward, I keep running into a EXC_BAD_ACCESS error when using a CCPhysicsSprite (CCSprite subclass that integrates a b2body with the sprite). The code I'm using is:

                  - (void)spawnBallAtPoint:(CGPoint)point
                  {
                      count++;
                  
                      CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
                      sprite.position = point;
                      [self addChild:sprite];
                  
                      b2BodyDef bodyDef;
                      bodyDef.type = b2_dynamicBody;
                      bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
                      bodyDef.userData = sprite;
                      b2Body *body = world->CreateBody(&bodyDef);
                  
                      b2CircleShape circleShape;
                      circleShape.m_radius = (26.0 / PTM_RATIO);
                  
                      b2FixtureDef fixtureDef;
                      fixtureDef.shape = &circleShape;
                      fixtureDef.density = 1.0f;
                      fixtureDef.friction = 0.2f;
                      fixtureDef.restitution = 0.8f;
                      body->CreateFixture(&fixtureDef);
                  
                      sprite.b2Body = body;
                  }
                  

                  此代码触发 EXC_BAD_ACCESS,我知道它是 CCPhysicsSprite,因为将 CCPhysicsSprite 更改为 CCSprite 会引发零错误:

                  This code triggers an EXC_BAD_ACCESS, I know it's the CCPhysicsSprite because changing CCPhysicsSprite to CCSprite throws zero errors:

                  - (void)spawnBallAtPoint:(CGPoint)point
                  {
                      count++;
                  
                      CCSprite *sprite = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
                      sprite.position = point;
                      [self addChild:sprite];
                  
                      b2BodyDef bodyDef;
                      bodyDef.type = b2_dynamicBody;
                      bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
                      bodyDef.userData = sprite;
                      b2Body *body = world->CreateBody(&bodyDef);
                  
                      b2CircleShape circleShape;
                      circleShape.m_radius = (26.0 / PTM_RATIO);
                  
                      b2FixtureDef fixtureDef;
                      fixtureDef.shape = &circleShape;
                      fixtureDef.density = 1.0f;
                      fixtureDef.friction = 0.2f;
                      fixtureDef.restitution = 0.8f;
                      body->CreateFixture(&fixtureDef);
                  }
                  

                  我环顾四周,找不到真正的答案(因为所有示例代码都以这种方式使用 CCPhysicsSprite 没有错误).我确定我犯了一个愚蠢的错误,但我想在学习新东西时这是可以预料的:P

                  I've been looking around and can't find a real answer (as all the sample code uses CCPhysicsSprite in this way without error). I'm sure I'm making a stupid mistake but I guess that's to be expected when learning something new :P

                  提前致谢!

                  推荐答案

                  使用 CCPhysicsSprite 必须在设置精灵位置之前设置主体.我假设这条线是发生崩溃的地方:

                  With CCPhysicsSprite you must set the body BEFORE you set the position of the sprite. I assume that this line is where the crash occurs:

                  sprite.position = point;
                  

                  如果您查看 CCPhysicsSprite,它会覆盖 position/setPosition 以始终使用包含的 b2body 对象中的值.所以它真的访问了它的 b2body 属性,在那个时候是 nil → Crash.通常,在创建 CCPhysicsSprite 后,您希望尽快设置其 .b2body - 可能会出现更多类似问题,因为您需要先设置 .b2body 属性.

                  If you look at CCPhysicsSprite it overrides the position/setPosition to always use the values from the contained b2body object. So it really accesses its b2body property, which is nil at that point → Crash. Generally after creating a CCPhysicsSprite you want to set its .b2body as soon as possible - there might be more occasions where similar problems might occur just because you need to set the .b2body property first.

                  所以,将 sprite.position = point; 移到 sprite.b2Body = body; 下方.

                  So, move sprite.position = point; below sprite.b2Body = body;.

                  这篇关于Cocos2d-iPhone 与 Box2D:CCPhysicsSprite EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 cocos2d 中以编程方式创建新的 UIView? 下一篇:为什么我的分布式应用程序看起来与在 xcode 中调试时不同?

                  相关文章

                  <i id='3JzkH'><tr id='3JzkH'><dt id='3JzkH'><q id='3JzkH'><span id='3JzkH'><b id='3JzkH'><form id='3JzkH'><ins id='3JzkH'></ins><ul id='3JzkH'></ul><sub id='3JzkH'></sub></form><legend id='3JzkH'></legend><bdo id='3JzkH'><pre id='3JzkH'><center id='3JzkH'></center></pre></bdo></b><th id='3JzkH'></th></span></q></dt></tr></i><div id='3JzkH'><tfoot id='3JzkH'></tfoot><dl id='3JzkH'><fieldset id='3JzkH'></fieldset></dl></div>

                    <tfoot id='3JzkH'></tfoot>
                    • <bdo id='3JzkH'></bdo><ul id='3JzkH'></ul>
                    <legend id='3JzkH'><style id='3JzkH'><dir id='3JzkH'><q id='3JzkH'></q></dir></style></legend>
                  1. <small id='3JzkH'></small><noframes id='3JzkH'>