Help on multiple same ball colliding with land
Posted: Fri Jan 29, 2010 7:52 am
Hi
Hope someone can shed some light. Had stuck for few days. Its an iPhone apps.. Pardon me as I am a learner in Obj-C and OO as well
Objective of the app is to have multiple thrown balls collided or hit on a specific area of the land.
I created an an object class called ball
Ball.m
----------------------------------
@implementation Ball
@synthesize isHitLand;
-(id) initWithCPBody: (cpBody *) bodyIn
{
self = [super init];
if(self != nil){
ballBody = bodyIn;
isHitLand = NO;
}
return self;
}
@end
--------------------------------------------
isHitLand is a bool.
In my GameLayer.h, I defined the following
---------------------------------------
@interface GameLayer : Layer {
..
..
Ball *smallBall;
...
@end
----------------------------------------
In my GameLayer.m, I create the object with the following
----------------------------------------------------
-(void) createSmallBall
{
smallBall = [[Ball alloc] initWithCPBody:[self makeBallX:kballX y:kballY name:@"ball.png" colType:kCollisionTypeBallSmall]];
}
-(cpBody *) makeBallX:(float) x y:(float)y name:(NSString *)name colType:(int)colType
{
Sprite *emoti = [[Sprite alloc] init];
emoti = [[Sprite spriteWithFile:name] retain];
emoti.position = cpv(x,y);
[self addChild:emoti];
cpBody *emotiBody = cpBodyNew(100.0,INFINITY);
emotiBody->p = cpv(x,y);
emotiBody->v = cpvmult(cpvforangle(6.1),100);
cpSpaceAddBody(space, emotiBody);
emotiShape = cpCircleShapeNew(emotiBody, kEmotiRadius, cpvzero);
emotiShape->e = 1.1; emotiShape->u = 0.5;
emotiShape->data = emoti;
emotiShape->collision_type = colType;
emotiShape->group = 2;
cpSpaceAddShape(space, emotiShape);
return emotiBody;
}
--------------------------------------------
In my collision function. (outside of the GameLayer implementation)
static int landCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
GameLayer *game = (GameLayer *) data;
[game keephitSmallball:a];
return 0;
}
-------------------------------------
My keephitSmallball as following (within the GameLayer implementation)
-------------------------------------
-(void) keephitSmallball:(cpShape *)a
{
if(smallBall.isHitLand == NO)
{
NSLog(@"Ball hitted target on land");
smallBall.isHitLand == YES;
[cleanupShapeArray addObject:[NSValue valueWithPointer:a]];
[self delayclearShapes]
}
------------------------------------------------------------------
My problem
I created 4 balls on interval of 1 seconds and added to the space. Bacause smallBall is a global (if I don't get the term wrong), everytime when 1 ball hit the land on a specific area, the keephitSmallball will set the isHitLand to YES and the other 3 balls will have set to YES as well. How can I ensure that the keephitSmallball only set the 1st ball isHitLand to YES without affecting other balls? How can I ensure that whatever operation being done to the class/object only for that object. Will the collision function have any visibility on which ball collided with the land. I know that I should not set the smallBall global but I need some guidance and direction on how to achieve the objective without doing what I am currently doing. [ FYI, if the ball thrown 1 at a time - after the 1st ball collided and then generate new, then everything work perfectly]
Many thanks for those who able to shed some light.
Hope someone can shed some light. Had stuck for few days. Its an iPhone apps.. Pardon me as I am a learner in Obj-C and OO as well
Objective of the app is to have multiple thrown balls collided or hit on a specific area of the land.
I created an an object class called ball
Ball.m
----------------------------------
@implementation Ball
@synthesize isHitLand;
-(id) initWithCPBody: (cpBody *) bodyIn
{
self = [super init];
if(self != nil){
ballBody = bodyIn;
isHitLand = NO;
}
return self;
}
@end
--------------------------------------------
isHitLand is a bool.
In my GameLayer.h, I defined the following
---------------------------------------
@interface GameLayer : Layer {
..
..
Ball *smallBall;
...
@end
----------------------------------------
In my GameLayer.m, I create the object with the following
----------------------------------------------------
-(void) createSmallBall
{
smallBall = [[Ball alloc] initWithCPBody:[self makeBallX:kballX y:kballY name:@"ball.png" colType:kCollisionTypeBallSmall]];
}
-(cpBody *) makeBallX:(float) x y:(float)y name:(NSString *)name colType:(int)colType
{
Sprite *emoti = [[Sprite alloc] init];
emoti = [[Sprite spriteWithFile:name] retain];
emoti.position = cpv(x,y);
[self addChild:emoti];
cpBody *emotiBody = cpBodyNew(100.0,INFINITY);
emotiBody->p = cpv(x,y);
emotiBody->v = cpvmult(cpvforangle(6.1),100);
cpSpaceAddBody(space, emotiBody);
emotiShape = cpCircleShapeNew(emotiBody, kEmotiRadius, cpvzero);
emotiShape->e = 1.1; emotiShape->u = 0.5;
emotiShape->data = emoti;
emotiShape->collision_type = colType;
emotiShape->group = 2;
cpSpaceAddShape(space, emotiShape);
return emotiBody;
}
--------------------------------------------
In my collision function. (outside of the GameLayer implementation)
static int landCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
GameLayer *game = (GameLayer *) data;
[game keephitSmallball:a];
return 0;
}
-------------------------------------
My keephitSmallball as following (within the GameLayer implementation)
-------------------------------------
-(void) keephitSmallball:(cpShape *)a
{
if(smallBall.isHitLand == NO)
{
NSLog(@"Ball hitted target on land");
smallBall.isHitLand == YES;
[cleanupShapeArray addObject:[NSValue valueWithPointer:a]];
[self delayclearShapes]
}
------------------------------------------------------------------
My problem
I created 4 balls on interval of 1 seconds and added to the space. Bacause smallBall is a global (if I don't get the term wrong), everytime when 1 ball hit the land on a specific area, the keephitSmallball will set the isHitLand to YES and the other 3 balls will have set to YES as well. How can I ensure that the keephitSmallball only set the 1st ball isHitLand to YES without affecting other balls? How can I ensure that whatever operation being done to the class/object only for that object. Will the collision function have any visibility on which ball collided with the land. I know that I should not set the smallBall global but I need some guidance and direction on how to achieve the objective without doing what I am currently doing. [ FYI, if the ball thrown 1 at a time - after the 1st ball collided and then generate new, then everything work perfectly]
Many thanks for those who able to shed some light.