Cocos2D tutorial for iOS: Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Cocos2D tutorial for iOS: Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3

Postby rwenderlich » Tue Mar 23, 2010 8:00 am

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2218
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 359 times

Re: Harder Monsters and More Levels: How To Make A Simple iP

Postby dyulyur » Mon Jan 10, 2011 12:29 pm

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

Postby rwenderlich » Mon Jan 10, 2011 9:10 pm

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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2218
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 359 times

Re: Harder Monsters and More Levels: How To Make A Simple iP

Postby sroxxx » Sat Jan 22, 2011 6:24 am

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:

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

Postby zoran.em » Thu Feb 10, 2011 11:16 pm

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!
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

Postby zoran.em » Sat Feb 12, 2011 8:40 am

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

Postby zcrugby » Wed Feb 16, 2011 2:38 am

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

Postby Sotres » Thu Feb 17, 2011 3:51 am

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

Postby shawngrimes » Thu Feb 17, 2011 6:43 pm

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?
-----------
Shawn Grimes
Blog: http://www.shawnsbits.com
Twitter: @shawng
User avatar
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

Postby Sotres » Fri Feb 18, 2011 2:40 am

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 :)
Sotres
Baby Hacker
 
Posts: 6
Joined: Thu Feb 10, 2011 6:23 pm
Location: México
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: Bing [Bot] and 10 guests