Reputation: 3
I am creating my first game with cocos2D. And I have a problem looking for help.
I want to create some selected numbers and falling down randomly.
so this is what I did for the programe.
In my HelloWorldLayer.m
-(id) init
{
if( (self=[super init]) ) {
screenSize = [CCDirector sharedDirector].winSize;
CCSprite* background = [CCSprite spriteWithFile:@"game.png"];
background.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:background z:0];
operatorArray = [[NSMutableArray alloc] init];
fallingNumberArray = [[NSMutableArray alloc] init];
[[[CCDirector sharedDirector] scheduler] scheduleSelector:@selector(update:) forTarget:self interval:0.0 paused:NO];
// Falling Number Font Type & Position
fallingNumber = [CCLabelBMFont labelWithString:@"99" fntFile:@"MyHelveticaCondensed.fnt"];
fallingNumber.anchorPoint=ccp(0.5f,0.5f);
fallingNumber.position = ccp(160,180);
[self addChild:fallingNumber z:3];
}
return self;
}
I have a method to generate the color for the numbers
-(void) colorFallingNumber{
// FallingNumber random and color ------------- START CODE -------------
do {
fallNum = (arc4random() % 60) + 1;
}
while ( fallNum == 23 || fallNum == 29 || fallNum == 31 || fallNum == 35 || fallNum == 37 || fallNum == 41 || fallNum == 43|| fallNum == 44|| fallNum == 46|| fallNum == 47|| fallNum == 49 || fallNum == 52|| fallNum == 53 || fallNum == 55 || fallNum == 56 || fallNum == 58|| fallNum == 59);
if (fallNum == 1 ||fallNum == 7 ||fallNum == 13 ||fallNum == 19 ||fallNum == 26 ||fallNum == 34 ||fallNum == 45 ||fallNum == 60) {
fallingNumber.color = ccc3(217,101,75);
}
if (fallNum == 2 ||fallNum == 8 ||fallNum == 14 ||fallNum == 20 ||fallNum == 27 ||fallNum == 36 ||fallNum == 48) {
fallingNumber.color = ccc3(79,166,134);
}
if (fallNum == 3 ||fallNum == 9 ||fallNum == 14 ||fallNum == 21 ||fallNum == 28 ||fallNum == 38 ||fallNum == 50) {
fallingNumber.color = ccc3(217,148,15);
}
if (fallNum == 4 ||fallNum == 10 ||fallNum == 16 ||fallNum == 22 ||fallNum == 30 ||fallNum == 39 ||fallNum == 51) {
fallingNumber.color = ccc3(209,75,217);
}
if (fallNum == 5 ||fallNum == 11 ||fallNum == 17 ||fallNum == 24 ||fallNum == 32 ||fallNum == 40 ||fallNum == 54) {
fallingNumber.color = ccc3(75,149,217);
}
if (fallNum == 6 ||fallNum == 12 ||fallNum == 18 ||fallNum == 25 ||fallNum == 33 ||fallNum == 42 ||fallNum == 57) {
fallingNumber.color = ccc3(217,75,32);
}
}
And this is update method
-(void) update:(ccTime)dt{
NSMutableArray* removeArray = [NSMutableArray array];
[self colorFallingNumber];
fallingNumber.string = [NSString stringWithFormat:@"%d",fallNum];
NSString *fallingNumberName = [NSString stringWithFormat:@"%d",fallNum]; ////// I think here is the problem
FallingNumber *newFallingNumber = [[NSClassFromString(fallingNumberName) alloc] init]; ////// I think here is the problem
[newFallingNumber animateFallingNumber];
newFallingNumber.scale = 1;
//newFallingNumber.rotation = atan2(newFallingNumber.direction.x,newFallingNumber.direction.y) * 180.0 / M_PI;
[fallingNumberArray addObject:newFallingNumber];
[self addChild:newFallingNumber z:4.0];
for(FallingNumber* fnumber in fallingNumberArray){
fnumber.position = ccp(fnumber.position.x + fnumber.speed *
fnumber.direction.x ,fnumber.position.y + fnumber.speed * fnumber.direction.y);
if(!CGRectContainsPoint(CGRectMake(-80,-80,320+160,480+160), fnumber.position)){
[self removeChild:fnumber cleanup:YES];
[removeArray addObject:fnumber];
}
}
// Operator animation ------------- START CODE -------------
if(RANDOM_FLOAT() < 0.1 && [operatorArray count] < 15){
NSString *operatorName = [NSString stringWithFormat:@"operator0%d.png", ((arc4random() % 4)+1)];
Operator* newOperator = [Operator spriteWithFile:operatorName];
[newOperator animateOperator];
newOperator.scale = 1;
newOperator.rotation = atan2(newOperator.direction.x,newOperator.direction.y) * 180.0 / M_PI;
[operatorArray addObject:newOperator];
[self addChild:newOperator z:2.0];
}
for(Operator* operator in operatorArray){
operator.position = ccp(operator.position.x + operator.speed *
operator.direction.x ,operator.position.y + operator.speed * operator.direction.y);
if(!CGRectContainsPoint(CGRectMake(-80,-80,320+160,480+160), operator.position)){
[self removeChild:operator cleanup:YES];
[removeArray addObject:operator];
}
} // Operator animation ------------- END CODE -------------
[operatorArray removeObjectsInArray:removeArray];
[removeArray removeAllObjects];
}
Also, I have create FallingNumber CCSprite in other file
It contains about the property of direction, speed...etc
Well, the problem is I cannot add the falling number object to the array. And I found that one of the reason is I use CCLabelBMFont to create the falling number.
Because it is work if i use png file, it works in Operator.
Looking for help.
Upvotes: 0
Views: 145
Reputation: 536
This line,
FallingNumber *newFallingNumber = [[NSClassFromString(fallingNumberName) alloc] init];
Is confusing to me. It looks like you are trying to create an instance of the class FallingNumber, but by calling NSClassFromString(fallingNumberName)
it looks like you are trying to instantiate a class that is literally called "16" or whatever the falling number happens to be.
From what I gather, it looks like you are just trying to pass the value of (fallingNumberName) to a new instance of the class FallingNumber. In that case, I would make a custom initalizer in FallingNumber that takes the parameter, such as this:
in FallingNumber.h:
-(id) initWithString:(NSString*)name;
Then you instantiate the class by making the following call:
FallingNumber *newFallingNumber = [[FallingNumber alloc] initWithString:fallingNumberName];
Then you have a new instance of the FallingNumber class with the value of the string that you want. Was this what you were looking for?
Upvotes: 1