0

我正在做一个游戏。我在 box2d 中遇到问题。我创建了一个 World 和一个 Web(sprite,body),然后我在 touchend 上投掷 web,以及一个获取 sprite 位置及其给出错误的刻度选择器。

这是刻度选择器 在此处输入图像描述

onTouchEnd

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    if (freeBody) {
        [self schedule: @selector(tick:)];
        [self schedule:@selector(WebHitsFood:) interval:0.01f];

        freeBody->SetType(b2_dynamicBody);

        //this is the maximum force that can be applied
        const CGFloat maxForce = 600;


        //get the location of the end point of the swipe
        UITouch *myTouch = [touches anyObject];
        CGPoint location = [myTouch locationInView:[myTouch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];


        //get the rotation b/w the start point and the end point
        CGFloat rotAngle = atan2f(location.y - startPoint.y,location.x - startPoint.x);

        //the distance of the swipe if the force
        CGFloat distance = ccpDistance(startPoint, location) * 0.5;

        //put a cap on the force, too much of it will break the rope
        if (distance>maxForce) distance = maxForce;

        //apply force
        freeBody->ApplyForce(b2Vec2(cosf(rotAngle) * distance, sinf(rotAngle) * distance), freeBody->GetPosition());

        //lose the weak reference to the body for next time usage.
        freeBody = nil;


    }
    self.isTouchEnabled = NO;

}

并创建 Web 方法

-(void) createWeb
{
    freeBodySprite = [CCSprite spriteWithFile:@"web1.png"];//web_ani_6_1
    //freeBodySprite.position = ccp(100, 300);
    [self addChild:freeBodySprite z:2 tag:6];

    CGPoint startPos = CGPointMake(100, 320/1.25);

    bodyDef.type = b2_staticBody;
    bodyDef.position = [self toMeters:startPos];
    bodyDef.userData = freeBodySprite;


    float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 0.5f);
    shape.m_radius = radiusInMeters;


    fixtureDef.shape = &shape;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 1.0f;
    fixtureDef.restitution = 0.0f;

    circularObstacleBody = world->CreateBody(&bodyDef);
    stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
    freeBody = circularObstacleBody;

}
4

2 回答 2

0

您确定 GetPosition() 是问题所在吗?尝试添加行

浮动测试 = b->GetPosition().x;

就在给您问题以确定问题实际上是与 GetPosition() 还是 myActor.position 是 exc_bad_access 的来源的问题之前。

于 2013-06-25T13:51:01.443 回答
0

解决崩溃的简单方法:

for(b2Body* b= world->GetBodyList(); b; B->GetNext())
{
    id sprite = b->GetUserData();

   if( sprite && [sprite isKindOfClass:[CCSprite class])
   {
         CCSprite *myActor = (CCSprite*) sprite;

         myActor.position = CGPointMake(b->getPosition().x*PTM_RATIO, b->getPosition().y*PTM_RATIO);

       myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
   }
}
于 2013-06-25T16:33:52.457 回答