良い一日 !物理エンジン「シマリス」を使用しています。クリックして動的オブジェクトを作成してみてください。AddBalls() は、目的のボディとシェイプを作成します。クリックすると、新しいオブジェクトを生成して配列に配置する必要があります。
-(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...
" が発生しました。私は何をすべきか?ありがとう