Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2009, 07:03 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 7
markvw is on a distinguished road
Default Gaming Tutorial

You can find a great game programming tutorial on iCodeBlog:
iPhone Game Programming Tutorial - Part 1 | iCodeBlog

Also I recently started to create a tutorial on how to create a game from beginning to end on my blog:
CrystalMinds

It's far from finished but I am planning on posting frequently and of course open for any kind of suggestions and/or improvements.
markvw is offline   Reply With Quote
Old 03-14-2009, 08:03 AM   #2 (permalink)
CTO Malus Pumila, LLC
 
Join Date: Feb 2009
Location: Michigan
Posts: 52
myemailisjustin is on a distinguished road
Default Trouble with NSTimer

I've followed the DuckHunt tutorial up to the point you've created. So far it's really cool and has taught me a lot. I wanted to continue on where you left off and attempt to get the game rolling. In the GameViewController - I think you called your VCGame, I've created a new method: playCurrentLevel - which takes in the cGL (currentGameLevel). From within there, I call another method called "countDownLevel", which I want to execute based on the NSTimer. So this is what I have in the playCurrentLevel method:
Code:
-(void)playCurrentLevel:(NSInteger *)cGL {
	currentGameLevel = cGL;
	countDownValue = 3;
	[self countDownLevel];
	tmrCountDownStartLevel = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(countDownLevel) userInfo:nil repeats:YES];
	// Actually create the duck and send it across the screen.....
	if (currentGameLevel == 1) {
		// Create and initialize a timer object to loop through the timing and creating of a duck to fly across the screen.
		tmrSendADuck = [NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(sendADuck) userInfo:nil repeats:YES];
	} else {
		
		
	}
	
}
And here is the shell for the countDownLevel method:
Code:
-(id) countDownLevel {
	//Show the countdown label for the current count that you are on:  3...2....1..
	switch (countDownValue) {
		case 3:
			NSLog(@"3........");
			break;
		case 2:
			NSLog(@"2........");
			break;
		case 1:
			NSLog(@"1........");
			break;
		case 0:
			NSLog(@"0........");
			NSLog(@"Start Game!");
			[tmrCountDownStartLevel invalidate];
			tmrCountDownStartLevel = nil;
		default:
			break;
	}
}
the idea is that every second - the tmrCountDownStartLevel timer will fire the "countDownLevel" method. The issue is that for some reason, which I can't figure out, the program steps right over the tmrCountDownStartLevel line in the playCurrentLevel method without going into the countDownLevel method.

Obviously this is a learning process for me, but can someone shed some light on why I might not be entering into the countDownLevel method. I just want it to change a labels text value to the current countDownValue, and then decrement that value each time the time fires. then upon reaching 0 - invalidate the timer, and continue on with the rest of the game.

If you need more of my code, let me know. I think I included everything in here. - except the actual declarations. *tmrCountDownStartLevel is NSTimer in the GameViewController.h and compiles fine.
myemailisjustin is offline   Reply With Quote
Old 03-14-2009, 08:24 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 7
markvw is on a distinguished road
Default

Nice to know that you like my tutorial.

I am not sure what exactly you've done wrong, but if you open the VCGame.h file and add the following:
Code:
NSTimer *tmrCountDownStartLevel;
- (void)countDownLevel;
Open VCGame.m and add the following in the proper positions:
Code:
@synthesize tmrCountDownStartLevel;
[tmrCountDownStartLevel dealloc];

- (void)countDownLevel {
    NSLog(@"Counting");
}
Then add the following code at the end, within the initWithGameSetup method:
Code:
	[self countDownLevel];
	tmrCountDownStartLevel = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(countDownLevel) userInfo:nil repeats:YES];
	
	return self;
That should work. From there on you could create the method playCurrentLevel, remove the code within initWithGameSetup and add it in there. Call the playCurrentLevel method and see if it still works and move from there. Hope this helps.
markvw is offline   Reply With Quote
Old 03-14-2009, 09:20 AM   #4 (permalink)
CTO Malus Pumila, LLC
 
Join Date: Feb 2009
Location: Michigan
Posts: 52
myemailisjustin is on a distinguished road
Default

Quote:
Originally Posted by markvw View Post
Nice to know that you like my tutorial.

I am not sure what exactly you've done wrong, but if you open the VCGame.h file and add the following:
Code:
NSTimer *tmrCountDownStartLevel;
- (void)countDownLevel;
Open VCGame.m and add the following in the proper positions:
Code:
@synthesize tmrCountDownStartLevel;
[tmrCountDownStartLevel dealloc];

- (void)countDownLevel {
    NSLog(@"Counting");
}
Then add the following code at the end, within the initWithGameSetup method:
Code:
	[self countDownLevel];
	tmrCountDownStartLevel = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(countDownLevel) userInfo:nil repeats:YES];
	
	return self;
That should work. From there on you could create the method playCurrentLevel, remove the code within initWithGameSetup and add it in there. Call the playCurrentLevel method and see if it still works and move from there. Hope this helps.
Thanks for the quick reply. I'll give that all a shot later today, and will let you know how I fare.
myemailisjustin is offline   Reply With Quote
Old 03-14-2009, 10:05 AM   #5 (permalink)
CTO Malus Pumila, LLC
 
Join Date: Feb 2009
Location: Michigan
Posts: 52
myemailisjustin is on a distinguished road
Default

Quote:
That should work. From there on you could create the method playCurrentLevel, remove the code within initWithGameSetup and add it in there. Call the playCurrentLevel method and see if it still works and move from there. Hope this helps.
You know - something strange is/was definitely going on. I've done the code you suggested, and have the same results I had before. So I moved my instantiation of the tmrCountDownStartLevel to the bottom of the initWithGameSetup. When I'm running through that, the program gets to the point in code within the initWithGameSetup:

Code:
	//initialize variables
	isAllowedToMove  = NO;
	isAllowedToShoot = NO;
	
	//Setup countDownStartLevel timer         
	[self countDownLevel];   <-- Never gets called !!??!!
	tmrCountDownStartLevel = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(countDownLevel) userInfo:nil repeats:YES];
and within self - the GameViewController - I have the countDownLevel implemented as:

Code:
-(void) countDownLevel {
	//Show the countdown label for the current count that you are on:  3...2....1..
	switch (countDownValue) {
		case 3:
			NSLog(@"3........");
			break;
		case 2:
			NSLog(@"2........");
			break;
		case 1:
			NSLog(@"1........");
			break;
		case 0:
			NSLog(@"0........");
			NSLog(@"Start Game!");
			[tmrCountDownStartLevel invalidate];
			tmrCountDownStartLevel = nil;
		default:
			break;
	}
}
In the GameViewController.h I've already got the following:
Code:
        //This is inside the @interface GameViewController : UIViewController {
	NSTimer *tmrCountDownStartLevel;

        // and then the property
        @property(nonatomic, retain) NSTimer *tmrCountDownStartLevel;
   
       // and the method declaration
-(void)countDownLevel;
So I compiled ok after that - and I had the exact same problem as when I tried to instantiate the timer from within the playCurrentLevel method I defined. After a little frustration, I realized that the "code-sense" colors that XCode provides for keywords, variables, etc. on the property declaration didn't match the color of the NSTimer variable declaration within the @interface. So I double checked the values and they appeared to be the exact same. So I copied/pasted the variable declaration for tmrCountDownStartLevel to the clipboard and used that to paste over the typing I did for tmrCountDownStartLevel on the @property portion. Then magically the color of the variables matched. And all of sudden I was falling into the countDownLevel method finally.

So obviously there must've just been a typo - but if you look at my code I originally posted, I don't see a typo - but by copy/paste the variable name instead of re-typing it, it worked! - I don't know if htat made sense or not, but Thank you for your help!

now it's onto creating a duckTimer to fire and make the duck start along a path - I would assume you need to get random values that are at least within the bounds of the game screen - and set the vDucks.center to that CGRectPoint(randomX,randomY) & all within an animation block so they appear to be flying, then if the player has not shot the duck when the vDucks.center reaches its destination, it will get another set of random X&Y to fly to -- provided the flightDestination count is less than some value I specify, and if it is the same - make the duck fly away off the screen - because the player can't shoot worth a darn.

Of course there is more to it, but it seems to make sense.

Thanks again for your work and assistance on this project.
myemailisjustin is offline   Reply With Quote
Old 03-14-2009, 10:25 AM   #6 (permalink)
CTO Malus Pumila, LLC
 
Join Date: Feb 2009
Location: Michigan
Posts: 52
myemailisjustin is on a distinguished road
Default Looking Good So Far!

The Debugger has exited with status 0.
[Session started at 2009-03-14 11:20:32 -0400.]
2009-03-14 11:20:39.238 Duck Hunt[13714:20b] 3........
2009-03-14 11:20:40.240 Duck Hunt[13714:20b] 2........
2009-03-14 11:20:41.240 Duck Hunt[13714:20b] 1........
2009-03-14 11:20:42.240 Duck Hunt[13714:20b] 0........
2009-03-14 11:20:42.241 Duck Hunt[13714:20b] Start Game!

I guess the first second will have to be 1.002 seconds....
myemailisjustin is offline   Reply With Quote
Old 04-06-2009, 07:12 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: LONDON
Posts: 22
josh is on a distinguished road
Default a helping hand

I started writing a game tutorial for a air-hockey clone - helpful (hopefully) for beginners wanting a hand is get started. Tutorial can be read at Building Airhockey for the iPhone « playingwithcode.com
josh is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0