Hi, is anybody familiar with Ray Wenderlichs simple iphone game tutorial 3? I am curious to know what line would change the number of times you have to kill a target before you move to the next level...
Thank you
A fast, easy to use, free, and community supported 2D game engine
Hi, is anybody familiar with Ray Wenderlichs simple iphone game tutorial 3? I am curious to know what line would change the number of times you have to kill a target before you move to the next level...
Thank you
On http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial
This:
_projectilesDestroyed++;
if (_projectilesDestroyed > 30) {
GameOverScene *gameOverScene = [GameOverScene node];
[gameOverScene.layer.label setString:@"You Win!"];
[[CCDirector sharedDirector] replaceScene:gameOverScene];
}
Is found inside the method to remove a target.
It increments the value of _projectilesDestroyed, then checks to see if the value of _projectilesDestroyed is above 30 to let you know you've won. It would have made more sense to me to name the int _targetsDestroyed instead of _projectilesDestroyed because in effect that is what it's counting.
Hope that helps...
Don't really know the tutorials but I took a quick look and it seems like these lines in the HelloWorldScene.m file are what you're looking for:
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
_projectilesDestroyed++;
if (_projectilesDestroyed > 2) {
Cocos2DSimpleGameAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate levelComplete];
}
}
Look at the if(_projectilesDestroyed > 2) line. If the game currently completes the level after 2 kills then this is what your after, just change that number.
A quick suggestion - if you're new to development you'll want to start getting familiar with debugging and code breaks to help you see what's going on.
Thanks to both of you I managed to find this and have played about a bit, really grateful.
In you opinions how hard would it be to change the code so you could have a different number each level, so level 1 you kill 5, level 2 you kill 10 and so on...
@phil_me_up thanks for the advice i've found some sites that i will read through tonight.
I will create a new post for this. Thanks
No you don't need to create a new post for that. If all you are doing is doubling the kill amount every level then it's a pretty easy mathematical condition.
instead of
if (_projectilesDestroyed > 2)
You can use variables so that the value changes according to your needs:
if (_projectilesDestroyed > killBase * levelNumber )
And your killBase variable could be 5 and all you do is start levelNumber from 1 and increment every new level.
I added and extra int into my level init like the following
@synthesize enemyNum = _enemyNum;
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName foxLevel:(int)foxLevel foxLevel2:(int)foxLevel2 enemyNum:(int)enemyNum {
if ((self = [super init])) {
self.levelNum = levelNum;
self.spawnRate = spawnRate;
self.bgImageName = bgImageName;
self.foxLevel = foxLevel;
self.foxLevel2 = foxLevel2;
self.enemyNum = enemyNum;
}
return self;
}
note that my level class is different from yours so don't just copy the code.
and in the game play scene call
.h
int _numEnemy
.m
in init
_numEnemy=delegate.curLevel.enemyNum;
in update
if(_projectilesDestroyed >_numEnemy)
then when you declare your level add in the numEnemy to the end
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"BGdaynew.png" foxLevel:1 foxLevel2:1 enemyNum:5] autorelease];
Thank you both for the replys, @criter88 I have followed your code but am coming up with several errors... here is my code see what you think.
first i changed level.m
#import "Level.h"
@implementation Level
@synthesize spawnRate = _spawnRate;
@synthesize levelNum = _levelNum;
@synthesize bgImageName = _bgImageName;
@synthesize enemyNum = _enemyNum;
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName Level1:(int)Level1 Level2:(int)Level2 enemyNum:(int)enemyNum {
if ((self = [super init])) {
self.levelNum = levelNum;
self.spawnRate = spawnRate;
self.bgImageName = bgImageName;
self.Level1 = Level1;
self.Level2 = Level2;
self.enemyNum = enemyNum;
self._numEnemy=delegate.curLevel.enemyNum;
}
return self;
}
@end
then i changed level.h
#import <Foundation/Foundation.h>
@interface Level : NSObject {
int _levelNum;
int _numEnemy;
float _spawnRate;
NSString *_bgImageName;
}
@property (nonatomic, assign) int levelNum;
@property (nonatomic, assign) int levelEnemy;
@property (nonatomic, assign) float spawnRate;
@property (nonatomic, copy) NSString *bgImageName;
- (id)initWithLevelNum:(int)levelNum
initWithLevelNum:(int)levelEnemy
spawnRate:(float)spawnRate
bgImageName:(NSString *)bgImageName;
@end
I then changed the delegate.m
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" Level1:1 enemyNum:5] autorelease];
Level *level2 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" Level2:1 enemyNum:10] autorelease];
[_levels addObject:level1];
[_levels addObject:level2];
then finally helloworldscene.m
if(_projectilesDestroyed >_numEnemy) {
Cocos2DSimpleGameAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate levelComplete];
Any ideas?
Thank you
I have tried to follow your code and at first i had 7 errors, then 90, and now I have it down to 1 error! I just cant figure out what I am doing wrong, I guess I'm not putting the code in the right place... sorry for sounding so stupid but I am very new to coding and cocos2d.
here is my level.m
#import "Level.h"
@implementation Level
@synthesize spawnRate = _spawnRate;
@synthesize levelNum = _levelNum;
@synthesize bgImageName = _bgImageName;
@synthesize enemyNum = _enemyNum;
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName Level1:(int)Level1 Level2:(int)Level2 enemyNum:(int)enemyNum {
if ((self = [super init])) {
self.levelNum = levelNum;
self.spawnRate = spawnRate;
self.bgImageName = bgImageName;
self.enemyNum = enemyNum;
}
return self;
}
@end
here is my level.h
#import <Foundation/Foundation.h>
@interface Level : NSObject {
int _levelNum;
int _enemyNum;
float _spawnRate;
NSString *_bgImageName;
}
@property (nonatomic, assign) int levelNum;
@property (nonatomic, assign) int enemyNum;
@property (nonatomic, assign) float spawnRate;
@property (nonatomic, copy) NSString *bgImageName;
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName;
@end
If I'm right I then added int _numEnemy into my helloworldscene.h
I have then added _numEnemy=delegate.curLevel.enemyNum; at the bottom of my helloworldscene init as below
-(id) init
{
if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
self.isTouchEnabled = YES;
_targets = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
CGSize winSize = [[CCDirector sharedDirector] winSize];
_player = [[CCSprite spriteWithFile:@"Player2.png"] retain];
_player.position = ccp(_player.contentSize.width/2 + 100, winSize.height/2);
[self addChild:_player z:1];
// Set up score and score label
_score = 0;
_oldScore = -1;
self.scoreLabel = [CCLabelTTF labelWithString:@"" dimensions:CGSizeMake(100, 50) alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:32];
_scoreLabel.position = ccp(winSize.width - _scoreLabel.contentSize.width/2, _scoreLabel.contentSize.height/2);
_scoreLabel.color = ccc3(0,0,0);
[self addChild:_scoreLabel z:1];
//[[CCScheduler sharedScheduler] setTimeScale:0.1];
[self schedule:@selector(update:)];
[self schedule:@selector(gameLogic:) interval:1.0];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background-music-aac.caf"];
_numEnemy=delegate.curLevel.enemyNum;
}
return self;
}
and finally i added if(_projectilesDestroyed >_numEnemy) in helloworldscene.m to the below
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
_projectilesDestroyed++;
//controls how many targets you have to kill on each level
if(_projectilesDestroyed >_numEnemy)
{
Cocos2DSimpleGameAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate levelComplete];
}
}
please can somebody help with this I know its going to be something stupid but I just cant work it out.
Thank you.
i should add that the error is against this line
_numEnemy=delegate.curLevel.enemyNum;
the error - delegate undeclared (first use in this function)
The error is as it sounds... the variable 'delegate' is undeclared and it's the first time it's been seen in this function.
Look in your init method and you'll see that you've not told it what 'delegate' is, so it has no idea what to do with it. You're either missing a line that defines 'delegate' (something like the following maybe)
Cocos2DSimpleGameAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
Or perhaps you need to make sure you've got all the right #import statements in your helloworld file etc....
Remember, unless you give your code all the information it needs, it's not going to work.
Thank you for that, it makes much more sense now you mention it.
i have added the above but i am receiving 2 warnings in the delegate.m for these lines -
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" enemyNum:10] autorelease];
Level *level2 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" enemyNum:10] autorelease];
the warnings for both -
No -initWithLevelNum:spawnRate:bgimageName:enemyNum: method found
my project builds but nothing happens it gets to the cocos2d screen them back to the iphone app screen.. any ideas?
Thank you
Again, the warning message does spell it out for you. There is no method that matches your call.
Look in your level.m file and you'll see this
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName Level1:(int)Level1 Level2:(int)Level2 enemyNum:(int)enemyNum {
That's not the same as the method you're trying to call because it's expecting more variables.
Look in your level.h file and you'll see this
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName;
That doesn't match it either and more importantly, this MUST match the m file (the h file is basically declaring that such a method exists in the m file). At the moment, your m file is saying that you have Level1:(int)Level1 Level2(int)Level2 etc and you're h file doesn't.
When you've got them sorted, look at the code you provided
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" enemyNum:10] autorelease];
You're trying to call a function that doesn't exist. Whilst it's the right name, it's expecting you to pass more variables (as defined in your h and m file).
I'm guessing this is the first programming language you've used. Don't worry, we've all been there but as a bit of friendly advice, you might wanna get a 'Learn Objective C' book and work through it or follow some really basic tutorials / examples (starting with hello world). Game programming get's pretty complex and you NEED to know the basics or you'll always be running into problems.
Thank you for all you help @phil_me_up I hear everything you saying I have 2 books on there way already. I am just really interested in learning how to code iphone games, i have definitely jumped in at the deep end but I have learned a lot from it. really appreciate you time though.
I am finally getting somewhere with this now... i think...
my level.h and level.m
- (id)initWithLevelNum:(int)levelNum spawnRate:(float)spawnRate bgImageName:(NSString *)bgImageName enemyNum:(int)enemyNum;
my delegate
Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" enemyNum:1] autorelease];
Level *level2 = [[[Level alloc] initWithLevelNum:1 spawnRate:3 bgImageName:@"bg.png" enemyNum:5] autorelease];
however the game now runs but both levels require you to kill 2 not what I have said on the above two lines...
I am just really interested in learning how to code iphone games, i have definitely jumped in at the deep end but I have learned a lot from it.
That's why most of us are here (using cocos2d), but you'll tear your hair out if you continue down this road (instead of understanding the basics). At the moment, you don't seem to understand what the code is actually doing, which is fine if you make connections while you change it, but fundamentals are required to do so.
If you already know C, or a derivative, this may help you get up to speed on the important ObjC paradigms: http://cocoadevcentral.com/d/learn_objectivec/
Otherwise, you'll need to learn about some of the C concepts as well (or hopefully have bought an ObjC book that does not assume you already know it).
As for your problem, when do you set this?:
_numEnemy
Thank you for the advice, I definitely agree with you on that. I don't suppose you could recommend any books that don't assume i already know coding?
as for _numEnemey I have int _numEnemy; in my helloworldscene.h...
int _numEnemy;
This declares the variable, but does not set it. You need to set it equal to the current level's enemy number (or just compare projectiles destroyed to the current level enemy number).
I don't have specific books to recommend, but they usually tell you what is expected of the reader, in the introduction. I personally dislike learning from books, since you cannot search, they are not update frequently, and there is no comments section with other reader's experiences. I'm fairly certain someone else can give you some recommendations though.
I am really sorry about this but I am totally lost now. What exactly am I missing and where? The sooner I learn the basics the better!
You must log in to post.