2012-11-19 97 views
0

美好的一天! 我使用物理引擎「花栗鼠」。嘗試通過單擊來創建動態對象。 AddBalls()創建所需的身體和形狀。通過單擊它必須生成一個新的對象,並將其放入數組中。不能添加對象到NSMutableArray

-(void)AddBalls: (UIImageView *)image; 
{ 
    cpBody *ball2Body = cpBodyNew(100.0, INFINITY); 
    ball2Body->p = cpv(60, 250); 
    cpSpaceAddBody(space, ball2Body); 

    cpShape *ball2Shape = cpCircleShapeNew(ball2Body, 20.0, cpvzero); 
    ball2Shape->e = 0.5; 
    ball2Shape->u=0.2; 
    ball2Shape->data = (__bridge void*)image; 
    ball2Shape->collision_type = 1; 
    cpSpaceAddShape(space, ball2Shape); 

    [children addObject: (__bridge id)ball2Shape];//EXC_BAD_ACCESS code=1 
} 

-(void)setupChipmunk 

{ 
    cpInitChipmunk(); 
    space = cpSpaceNew(); 
    space->gravity = cpv(0, -100); 
    space->elasticIterations = 10; 

    [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES]; 

    cpBody *ballBody = cpBodyNew(100.0, INFINITY); 
    ballBody->p = cpv(60,250); 
    cpSpaceAddBody(space, ballBody); 
    cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero); 
    ballShape->e = 0.5; 
    ballShape->u = 0.8; 
    (ballShape->data) =(__bridge void*) ball; 
    ballShape->collision_type = 1; 
    cpSpaceAddShape(space, ballShape); 
} 

-(void)tick:(NSTimer *)timer 
{ 
    cpSpaceStep(space, 1.0f/60.0f); 
    cpSpaceHashEach(space->activeShapes, &updateShape, nil); 
} 

-(void) updateShape (void *ptr, void *unused) 
{ 
    cpShape *shape = (cpShape*)ptr; 

    if(shape == nil || shape->body == nil || shape->data == nil) { 
     NSLog(@"Unexpected shape please debug here..."); 
     return; 
    } 

    if([(__bridge UIImageView*)shape->data isKindOfClass:[UIView class]]) { 
     [(UIView *)((__bridge UIImageView*)shape->data) setCenter:CGPointMake(shape->body->p.x, 480 - shape->body->p.y)]; 
    } 
} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { 
     return YES; 
    } 
} 

- (void)handleTap:(UITapGestureRecognizer *)sender {  
    if (sender.state == UIGestureRecognizerStateEnded) { 
     [self AddBalls]; 
    } 
} 

方法AddBalls應該將新形狀放入數組中。但我得到錯誤「EXC_BAD_ACCESS...」。我該怎麼辦?謝謝

+1

你孩子的定義在哪裏?它應該是一個'NSMutableArray'對象。 – sunkehappy

+0

在我已經將孩子定義爲NSMutableArray的總代碼之上。實際上,ball2Shape是C類的一個對象。但我使用(__bridge id),但它並沒有幫助 – user1722231

回答

2

你不能只將任何類型投到id,它需要實際上是一個Objective-C類型。您需要將ball2Shape換成NSValue

[children addObject:[NSValue value:&ball2Shape withObjCType:@encode(cpBody*)]]; 

... 
//When you need to use/free the values 
for (NSValue *value in children) 
{ 
    cpBody *body = (cpBody*)[value pointerValue]; 

    //Use body like you did above. 

    //Even though it is ARC you will need to free cpBody since 
    // it is not an Objective-C object 
    cpBodyFree(body); 
} 
+0

我嘗試它!謝謝! – user1722231

+0

什麼是'cpBodyFree'? – sunkehappy

+0

https://github.com/slembcke/Chipmunk-Physics/blob/master/include/chipmunk/cpBody.h – Joe