我正在用 cocos2D 创建我的第一个游戏。我在寻求帮助时遇到了问题。
我想创建一些选定的数字并随机下降。
所以这就是我为这个程序所做的。
在我的 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;
}
我有一种方法可以为数字生成颜色
-(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);
}
}
这是更新方法
-(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];
}
另外,我在其他文件中创建了 FallingNumber CCSprite
它包含有关方向,速度...等的属性
好吧,问题是我无法将下降数字对象添加到数组中。我发现其中一个原因是我使用 CCLabelBMFont 来创建下降数字。
因为如果我使用 png 文件就可以工作,所以它可以在 Operator 中工作。
寻求帮助。