Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3
50 posts
• Page 1 of 5 • 1, 2, 3, 4, 5
Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3
This is the official thread to discuss the following blog post: Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

rwenderlich - Site Admin
- Posts: 1812
- Joined: Thu Dec 23, 2010 4:14 pm
- Has thanked: 26 times
- Been thanked: 191 times
Re: Harder Monsters and More Levels: How To Make A Simple iP
Hello, Ray!!! Are you never remove _mainScene from memory? If I add command NSLog(@"MAIN SCENE IS REMOVED!!!") to dealloc method for main scene it is never called. Please, help me to understand.
- dyulyur
- Baby Hacker
- Posts: 5
- Joined: Fri Dec 24, 2010 11:05 am
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
In this example, the game creates a node for each scene (main scene, new level, game over) and re-uses the scenes over and over as the game goes on. So you really won't see the memory get dealloced (since dealloc in the App Delegate doesn't get called in practice due to the way multi-threading is implemented on the iPhone, but it doesn't really matter since all of the memory for the app is released anyway on app termination).
If you want different behavior, you can create new instances of each scene as you transition to it, so that the old one releases from memory each time.
If you want different behavior, you can create new instances of each scene as you transition to it, so that the old one releases from memory each time.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

rwenderlich - Site Admin
- Posts: 1812
- Joined: Thu Dec 23, 2010 4:14 pm
- Has thanked: 26 times
- Been thanked: 191 times
Re: Harder Monsters and More Levels: How To Make A Simple iP
Thanks so much Ray for all your hard work. You have managed to synthesize and incredible amount of your personal learnings into a very easy to understand tutorial format. I encourage everyone to recognize your efforts with a donation, wherever possible.
In the spirit of giving, I would like to share the logic I added to give our hero the machine gun he's been desperately wanting. To accomplish this we will need to make our hero to start shooting on ccTouchesBegan, turn the turret on ccTouchesMoved, and stop firing on ccTouchesEnded.
First, we will need to add the following member to our HelloWorld.h in order to track the current direction of fire:
We then need to repurpose our ccTouchesEnded method as a fire method, by changing the signature:
becomes
While we're at it, let's go ahead and remove or comment out the following three lines of code as the fire method will no longer be responsible for responding to touches:
/* UITouch *touch = [myTouches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
*/
To cap off our fire method implementation, we just need to substitute shootingPoint for location, as follows:
becomes
We now need to implement ccTouchesBegan update set our shooting point and schedule an event to repeat fire (booya!):
Next, we need to ensure that our gun will turn as we swipe towards our targets. To do this, we simply need to implement ccTouchesMoved to delegate to our ccTouchesBegan method:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self ccTouchesBegan:touches withEvent:event];
}
Finally, we should probably stop shooting when the player lifts their finger, so let's go ahead and unschedule the fire method:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self unschedule:@selector(fire)];
}
And that's that. Enjoy and be sure to thank Ray with a donation if you are able.
In the spirit of giving, I would like to share the logic I added to give our hero the machine gun he's been desperately wanting. To accomplish this we will need to make our hero to start shooting on ccTouchesBegan, turn the turret on ccTouchesMoved, and stop firing on ccTouchesEnded.
First, we will need to add the following member to our HelloWorld.h in order to track the current direction of fire:
- Code: Select all
CGPoint shootingPoint;
We then need to repurpose our ccTouchesEnded method as a fire method, by changing the signature:
- Code: Select all
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
becomes
- Code: Select all
- (void)fire {
While we're at it, let's go ahead and remove or comment out the following three lines of code as the fire method will no longer be responsible for responding to touches:
/* UITouch *touch = [myTouches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
*/
To cap off our fire method implementation, we just need to substitute shootingPoint for location, as follows:
- Code: Select all
CGPoint shootVector = ccpSub(location, _nextProjectile.position);
becomes
- Code: Select all
CGPoint shootVector = ccpSub(shootingPoint, _nextProjectile.position);
We now need to implement ccTouchesBegan update set our shooting point and schedule an event to repeat fire (booya!):
- Code: Select all
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
shootingPoint = [touch locationInView:[touch view]];
shootingPoint = [[CCDirector sharedDirector] convertToGL:shootingPoint];
/* the shorter the interval the faster the shots */
[self schedule:@selector(fire) interval:0.05];
}
Next, we need to ensure that our gun will turn as we swipe towards our targets. To do this, we simply need to implement ccTouchesMoved to delegate to our ccTouchesBegan method:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self ccTouchesBegan:touches withEvent:event];
}
Finally, we should probably stop shooting when the player lifts their finger, so let's go ahead and unschedule the fire method:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self unschedule:@selector(fire)];
}
And that's that. Enjoy and be sure to thank Ray with a donation if you are able.
- sroxxx
- n00b
- Posts: 1
- Joined: Sat Jan 22, 2011 5:54 am
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
Hey Ray! you're awesome, i love your tutorials, they're the best.
I'm a n00b at iphone programming/cocos2d so excuse my question if it's something most ppl know, but -
how would i go about swapping in a new img for the StrongAndSlowMonster after each hit, so the little bugger looks more worn down as it's hp is going down?
i just bought Math Ninja HD and i'll be hitting that donation button first chance possible!
I'm a n00b at iphone programming/cocos2d so excuse my question if it's something most ppl know, but -
how would i go about swapping in a new img for the StrongAndSlowMonster after each hit, so the little bugger looks more worn down as it's hp is going down?
i just bought Math Ninja HD and i'll be hitting that donation button first chance possible!
- zoran.em
- n00b
- Posts: 3
- Joined: Fri Feb 04, 2011 2:01 am
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
big ups sroxxx, just what i was looking for! thanks man much appreciated
- zoran.em
- n00b
- Posts: 3
- Joined: Fri Feb 04, 2011 2:01 am
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
hey.. im pretty much a newbie at this.. how would i go about adding a 3rd target that when hit you lose the game?
- zcrugby
- n00b
- Posts: 1
- Joined: Wed Feb 16, 2011 2:36 am
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
Hi Ray. I got lost a the end of the Tougher Monsters section. I made the changes to the code as you say in your tutorial, I compiled it and run with no error, but in the game, it crashes as soon as a StrongAndSlow monster appears on the screen. I downloaded the sample code of this part of the tutorial but this one includes the changes made to add multiple levels in the game, so its hard to tell what or where is my code error. Can you help me out? I really will appreciate it. Thank you.
- Sotres
- Baby Hacker
- Posts: 6
- Joined: Thu Feb 10, 2011 6:23 pm
- Location: México
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
Sotres wrote:Hi Ray. I got lost a the end of the Tougher Monsters section. I made the changes to the code as you say in your tutorial, I compiled it and run with no error, but in the game, it crashes as soon as a StrongAndSlow monster appears on the screen. I downloaded the sample code of this part of the tutorial but this one includes the changes made to add multiple levels in the game, so its hard to tell what or where is my code error. Can you help me out? I really will appreciate it. Thank you.
Hi Sotres,
What error message are you getting on the crash?
-

shawngrimes - Uber Haxx0r
- Posts: 69
- Joined: Thu Feb 17, 2011 6:14 pm
- Location: Aberdeen, MD
- Has thanked: 0 time
- Been thanked: 0 time
Re: Harder Monsters and More Levels: How To Make A Simple iP
Hi Sotres,
What error message are you getting on the crash?[/quote]
Hi Shawn, thank you for the reply. I dont get any error message, as soon as I touch the screen to shot, it closes itself. I include this video so you can see what I'm talking about:
http://www.youtube.com/watch?v=910ZMaItpI4
Thank you very much for the help
What error message are you getting on the crash?[/quote]
Hi Shawn, thank you for the reply. I dont get any error message, as soon as I touch the screen to shot, it closes itself. I include this video so you can see what I'm talking about:
http://www.youtube.com/watch?v=910ZMaItpI4
Thank you very much for the help
- Sotres
- Baby Hacker
- Posts: 6
- Joined: Thu Feb 10, 2011 6:23 pm
- Location: México
- Has thanked: 0 time
- Been thanked: 0 time
50 posts
• Page 1 of 5 • 1, 2, 3, 4, 5
Who is online
Users browsing this forum: No registered users and 0 guests