Reputation: 109
Below is a method for handling creation of balls depending on colour and some other properties. The createBall method returns a CCPhysicsSprite. numberOfBallsToCreate returns one of each colour but five for black.
-(void)ballCreation:(b2World*)world inLayer:(CCLayer*)layer {
CGSize s = [[CCDirector sharedDirector] winSize];
ballType = blackBall;
for (int i = 0; i < [self numberOfBallsToCreate]; i++) {
switch (ballType) {
case greenBall:
[self createBall:world inLayer:layer atPosition:[self ballPosition] ballImage:@"greenBall.png"];
break;
case redBall:
[self createBall:world inLayer:layer atPosition:[self ballPosition] ballImage:@"redBall.png"];
break;
case blueBall:
[self createBall:world inLayer:layer atPosition:[self ballPosition] ballImage:@"blueBall.png"];
break;
case blackBall:
[blackBallArray addObject:[self createBall:world inLayer:layer atPosition:[self ballBirdPositions] ballImage:@"blackBall.png"] ];
break;
default:
break;
}
}
}
Also below is a method that I update every 1/60 of a second, applying force on each ball. All the ball updates are working well except the blackBall.
-(void)updateBalls {
switch ([self getBallType]) {
case greenBall:
[self applyGreenForce];
break;
case redBall:
[self applyRedForce];
break;
case blueBall:
[self applyBlueForce];
break;
case blackBall: {
//[self applyBlackForce];
createdBlackBallArray = [self getBlackBallArray];
CCLOG(@"the createdBlackBallArray count is %d", createdBlackBallArray.count); // always prints zero
if (createdBlackBallArray != NULL) {
//CCLOG(@"code reached here");
for (int i = 0; i < createdBlackBallArray.count; i++) {
CCPhysicsSprite* blackBallPhysicsSprite = [createdBlackBallArray objectAtIndex:i];
//if (blackBallPhysicsSprite.b2Body != NULL) {
b2Vec2 ballForce = b2Vec2(1.0, 0.5);
blackBallPhysicsSprite.b2Body->ApplyForce(ballForce, blackBallPhysicsSprite.b2Body->GetWorldCenter() );
}
}
}
break;
default:
break;
}
The CCLOG above always gives zero for the array count so the force will not be applied. I am not sure what I am doing wrong. The getBlackBallArray details are below:
-(NSMutableArray*)getblackBallArray {
return blackBallArray;
}
How can I solve this?
UPDATED
Here is the initWithBall method where the arrays are initialized.
-(id)initWithBall:(b2World*)world inLayer:(CCLayer*)layer {
[self ballCreation:world inLayer:layer];
blackBallArray = [[NSMutableArray alloc] init];
createdBlackBallArray = [[NSMutableArray alloc] init];
return self;
}
Part of the Ball.h class is below:
#import "GameObjects.h"
typedef NS_ENUM(NSUInteger, ballTypes) {
blueBall = 0,
blackBall = 1,
redBall = 2,
greenBall = 3,
};
@interface Ball : CCNode {
b2Body* ballBody;
b2World* ballWorld;
CCLayer* ballLayer;
ballTypes ballType;
NSString* ballImageName;
NSMutableArray* blackBallArray;
NSMutableArray* createdBlackBallArray;
}
-(void)updateBalls;
@end
Upvotes: 0
Views: 143
Reputation: 1072
You have made a small mistake in your code. You have initialized blackBallArray after calling ballCreation method.
It should be like this:
-(id)initWithBall:(b2World*)world inLayer:(CCLayer*)layer {
if ((self = [super init]) != nil)
{
//Initialize array first
blackBallArray = [[NSMutableArray alloc] init];
createdBlackBallArray = [[NSMutableArray alloc] init];
[self ballCreation:world inLayer:layer];
}
return self;
}
A word of caution. When putting up a post always make sure only most important code is added. You really put up a lot of code which made it a bit confusing to go through. Happy coding.
Upvotes: 4