In my GameManager singleton object I create:
// Load levels
// Create array property to store level objects
self.levels = [[[NSMutableArray alloc] init] autorelease];
//Create 2 level objects with spawnRates
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:10000 bgImageName:#"Icon.png"] autorelease];
Level *level2 = [[[Level alloc] initWithLevelNum:2 spawnRate:10000 bgImageName:#"Icon.png"] autorelease];
Level *level3 = [[[Level alloc] initWithLevelNum:3 spawnRate:80 bgImageName:#"Icon.png"] autorelease];
Level *level4 = [[[Level alloc] initWithLevelNum:4 spawnRate:80 bgImageName:#"Icon.png"] autorelease];
Level *level5 = [[[Level alloc] initWithLevelNum:5 spawnRate:2 bgImageName:#"Icon.png"] autorelease];
Level *level6 = [[[Level alloc] initWithLevelNum:6 spawnRate:1 bgImageName:#"Icon.png"] autorelease];
//Put level objects into array property
[_levels addObject:level1];
[_levels addObject:level2];
[_levels addObject:level3];
[_levels addObject:level4];
[_levels addObject:level5];
[_levels addObject:level6];
//Set current level index to 0
self.curLevelIndex = 0;
And further down the line, I implement methods to be called by layers like so:
- (Level *)curLevel {
NSLog(#"curLevelIndex is %d", _curLevelIndex);
//returns object at current level index where at start, first slot [0] = spawnrate = 1
return [_levels objectAtIndex:_curLevelIndex];
}
- (void)restartGame {
_curLevelIndex = 0;
[self nextLevel];
}
- (void)levelComplete {
NSLog(#"curLevel spawnRate is %d", [self curLevel].spawnRate);
//Increase currentlevelindex
_curLevelIndex++;
if (_curLevelIndex >= [_levels count]) {
_curLevelIndex = 0;
NSLog(#"resetting levels");
} else {
//make it faster
//NSLog(#"current level %d", [[_levels objectAtIndex:_curLevelIndex].spawnRate ];
NSLog(#"new level %d", _curLevelIndex);
}
}
Please excuse all the NSLogs but im going crazy trying to find out why my spawnRate is not being changed. In my game's update method I do this:
if (timeSinceLastRateBump > 3) {
NSLog(#"DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
[[GameManager sharedGameManager] levelComplete];
timeSinceLastRateBump = 0;
}
And this is called every second more or less. Now Im getting this in my NSLog:
2013-04-13 11:08:22:489 MZ[1408:3079] target added to _targets
2013-04-13 11:08:23:489 MZ[1408:3079] target added to _targets
2013-04-13 11:08:24:472 MZ[1408:3079] DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2013-04-13 11:08:24:473 MZ[1408:3079] curLevelIndex is 1
2013-04-13 11:08:24:474 MZ[1408:3079] curLevel spawnRate is 0
2013-04-13 11:08:24:475 MZ[1408:3079] new level 2
2013-04-13 11:08:24:509 MZ[1408:3079] target added to _targets
2013-04-13 11:08:25:506 MZ[1408:3079] target added to _targets
2013-04-13 11:08:26:522 MZ[1408:3079] target added to _targets
2013-04-13 11:08:27:490 MZ[1408:3079] DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2013-04-13 11:08:27:491 MZ[1408:3079] curLevelIndex is 2
2013-04-13 11:08:27:493 MZ[1408:3079] curLevel spawnRate is 0
2013-04-13 11:08:27:494 MZ[1408:3079] new level 3
2013-04-13 11:08:27:523 MZ[1408:3079] target added to _targets
2013-04-13 11:08:27:856 MZ[1408:3079] Globals called. Points: 1
2013-04-13 11:08:27:856 MZ[1408:3079] Globals leaving. Points: 2
2013-04-13 11:08:28:539 MZ[1408:3079] target added to _targets
Which proves curLevelIndex is increasing from 0 to 1 to 2. But the spawnRate remains at 0. Why?
in your levelComplete routine, instead of :
NSLog(#"new level %d", _curLevelIndex);
do :
Level* current = [self curLevel];
NSLog(#"current level no : %d , spw rate : %f",current.levelNumber,current.spawnRate];
// *** WAG for the getters.
and breakpoint on log. If current is nil, figure that out. If not, inspect the var and look at the spawn rate in that object.
in the code:
NSLog(#"curLevel spawnRate is %d", [self curLevel].spawnRate);
%d is used for a double value, not a float. change it to %f.
Related
Ok so I've been trying to get a level select scene working for Custom levels (levels that the user created), so i need to have a button for each created level, and i have been trying to do it with a for loop, but only one button shows up. here is my code:
-(void)AddLevelButtons{
NSArray *levels = [[NSArray alloc]init];
levels = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:CustomPath error:nil];
LevelCount = (int)[levels count];
for (int counter= 0;counter<LevelCount;counter++) {
CCButton *tempButton;
tempButton=[CCButton buttonWithTitle:#"" spriteFrame:[CCSpriteFrame frameWithImageNamed:#"othercategory.png"]];
tempButton.block= ^(id sender) {
GameScene *GameNode = [[GameScene alloc]initWithPlistName:[NSString stringWithFormat:#"CustomLevel_%d",counter]withObjIndex:counter];
[[CCDirector sharedDirector] replaceScene:LevelEditorNode.scene withTransition:[CCTransition transitionFadeWithDuration:.5f]];
};
tempButton.positionType=CCPositionTypeNormalized;
tempButton.position = ccp(counter+2/5,.5f);
[ScrollingNode addChild:tempButton];
}
}
I thought that maybe both buttons were created, and they just had the same position, but i set a breakpoint, and only one button is created.
I'm having a strange problem... I have a custom class with sprite and booleans properties
In my main GameLayer, I have CCLayerColor *my1layer and in my .m section I have this code:
my1layer = [CCLayerColor layerWithColor:ccc4(144,238,144, 255)];
[self addChild: my1layer z:5 tag:100];
[my1layer setContentSize:CGSizeMake(200, 1280)];`
//default start position
my1layer =ccp(s.width/2-420, s.height-s.height+180.);
MylmageItem *postImage1 = [[MylmageItem alloc] initWithLayer:self];
[postImage1 release];
postImage1.postImageNum=CCRANDOM_0_1()*12 +1;
postImage1.postImage = [CCSprite spriteWithSpriteFrameName:(NSString*)[self assignPostImage:postImage1.postImageNum ]];
postImage1.postImage.position =ccp(my1layer.contentSize.width/2, my1layer.contentSize.height-my1layer.contentSize.height+80);
[my1layer addChild:myImage1.slotImage z:6 tag:101];’
code above does work and shows the images on layer, however when I try to access the property postImage1.postImageNum, it shows 0 always...
I do this: CCLayer *myLayer1 = (CCLayer*)[self getChildByTag:100]; later to retrieve my object...
MyImageItem *tmpImage1 = (MymageItem*)[myLayer1 getChildByTag:101]; from here I always get 0 when I do CCLOG(#"postImage1.postImageNum %d",tmpImage1.postImageNum);
How do I get data from my1layer?
I've created a quiz game, that implements a time bar. In the first play it's all right, but if, after gameover, the player tap "restart", the game goes on properly, but the time bar disappears!
Here my code from GameOverLayer to Game:
-(void) restart {
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}
Here the function to create a new question
-(void)creaDomanda{
//bar
CCProgressFromTo *to1 = [CCProgressFromTo actionWithDuration:MaxTime from:100 to:0];
bar = [CCProgressTimer progressWithFile:#"barra.png"];
bar.type = kCCProgressTimerTypeHorizontalBarLR;
[bar setPosition:ccp(size.width - 250 , size.height - 18)];
int randomValue = (arc4random() % 4) + 1;
NSString *stringa = [NSString stringWithFormat:#"Domanda%i", randomValue];
dictionary = [plistData objectForKey:stringa];
domanda = [dictionary valueForKey:#"Titolo"];
labelDomanda = [CCLabelTTF labelWithString:domanda fontName:#"Marker Felt" fontSize:24];
labelDomanda.position = ccp( size.width /2 , 400 );
[self addChild: labelDomanda];
int rispostaEsatta = [[dictionary valueForKey:#"Soluzione"] intValue];
menu = [CCMenu menuWithItems:nil];
for (int i = 1; i<5;i++)
{
if(rispostaEsatta == i){
item = [CCMenuItemFont itemFromString:[dictionary valueForKey:
[NSString stringWithFormat:#"Risposta%i",i] ]
target:self selector:#selector(corretto)];
}else{
item = [CCMenuItemFont itemFromString:[dictionary valueForKey:
[NSString stringWithFormat:#"Risposta%i",i] ]
target:self selector:#selector(sbagliato)];
}
[menu addChild:item];
}
//[..]
[self addChild:menu];
[self addChild:bar];
[bar runAction:to1];
}
And here one of the correct/wrong method (similar) that after all, create a new question:
-(void)sbagliato{
CCLOG(#"Sbagliato");
if (menu) [self removeChild:menu cleanup:YES];
if (labelDomanda) [self removeChild:labelDomanda cleanup:YES];
if (bar) [self removeChild:bar cleanup:YES];
labelRisultato = [CCLabelTTF labelWithString:#"Hai sbagliato!" fontName:#"Marker Felt" fontSize:24];
[labelRisultato setColor:ccc3(255, 1, 1)];
labelRisultato.position = ccp(size.width / 2, 280);
[self addChild:labelRisultato];
[self gameOver:2 punteggio:0];
// Richiamiamo il metodo per eliminare la label dopo 0,3 secondi
[self performSelector:#selector(eliminaLabel) withObject:nil afterDelay:0.5];
increment = increment - 20;
[pointLabel setString: [NSString stringWithFormat: #"Punti: %i", increment]];
// new question
[self performSelector:#selector(creaDomanda) withObject:nil afterDelay:0.5];
}
Can anyone explain to me please why when I restart the time bar desappers?
Thank You
My best guess:
The CCProgressFromTo action is still running. Since it progresses down to 0, the CCProgressTimer eventually doesn't display any part of it anymore. This may continue even if you run another CCProgressFromTo action on the progress timer.
Solution: be sure to stop any running CCProgressFromTo actions before running another.
If that doesn't fix it, then I imagine the CCProgressTimer needs to be reset by setting percentage back to 100.
I decided to have some practice with BatchNodes, but I'm stuck
here is some code:
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"scene1atlas.plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"CloudsAtlas.plist"];
sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:#"scene1atlas.png"];
cloudSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:#"CloudsAtlas.png"];
[self addChild:sceneSpriteBatchNode z:20];
[self addChild:cloudSpriteBatchNode z:21];
SomeGameObject *gameObject = [[SomeGameObject alloc]
initWithSpriteFrameName:#"sv_anim_1.png"];
gameObject.texture=[sceneSpriteBatchNode texture];
[sceneSpriteBatchNode addChild:gameObject
z:1000 tag:kGameObjectTagValue];
*** Assertion failure in -[GameObject setTexture:]
How to set texture properly?
I created a CCAnimation as seen below. I tried lowering the delay to something below .05f and the animation now fails to complete! Its only 8 frames. I don't know if I am doing something wrong. At first I thought it was a memory leak and I was losing the action, so I assigned it to a strong property to test that, and still did it. I'm not sure how the delay could cause my animation to fail to finish. I am running in the simulator at 60 frames per sec.
Using Kobold 2.0.4
Can anyone help?
else if([model currentState] == PROCESSING_FIRST_ACTION)
{
CCDirector* director = [CCDirector sharedDirector]; // process model
//Get the top left corner of the screen
int screen_height = director.screenSizeAsPoint.y;
int screen_width = director.screenSizeAsPoint.x;
id attacker = [model attacker];
id attackChoice = [attacker getRegisteredAttack];
CCAction* attack = [model.resourceManager animation: #"simple-attack-frame"];
CCSprite * attackSprite = [model.resourceManager sprite: #"simple-attack-frame-01"];
attackSprite.position = ccp(screen_width - rxa(80), screen_height - rya(80));
attackSprite.tag = 5;
self.action = attack;
CCNode * check = [self getChildByTag:5];
if(check == nil)
{
[self addChild: attackSprite];
[attackSprite runAction:attack];
}
}
resource manager:
-(id)animation: (NSString*)_resource
{
NSMutableArray *frames = [NSMutableArray array];
for(int i = 1; i <= 8; ++i)
{
[frames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"%#-0%d", _resource, i]]];
}
CCAnimation *animation = [CCAnimation animationWithSpriteFrames: frames delay:1/60];
CCAction * action = [CCAnimate actionWithAnimation: animation];
return action;
}
In your line
CCAnimation *animation = [CCAnimation animationWithSpriteFrames: frames delay:1/60];
you're setting the delay to 1/60, but as 1 and 60 are both integers, 1/60 is 0. Try using 1.0f/60.0f and you'll get a floating point divide.
After digging around on the web I found a solution. I'm not the one who submitted this solution but I can attest that it fixed my problems:
https://github.com/cocos2d/cocos2d-iphone/commit/60f9cc98783b9a6a5635db4f468f83e0511c74c8