안녕하세요! 필자는 물리 엔진 "chipmunk"를 사용합니다. 클릭하여 동적 객체를 생성하십시오. 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...
"오류가 있습니다. 어떻게해야합니까? 감사합니다
'자녀의 정의는 어디에 있습니까? NSMutableArray 객체 여야합니다. – sunkehappy
위의 총 코드에서 자식을 NSMutableArray로 정의했습니다. 실제로 ball2Shape는 C 클래스의 객체입니다. 하지만 난 (__bridge id) 아직 도움이 안된다. – user1722231