Cocos2D Tutorial for iOS: How To Make A Simple iPhone Game

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: How To Make A Simple iPhone Game

Postby rwenderlich » Fri Feb 12, 2010 3:00 am

This is the official thread to discuss the following blog post: Cocos2D Tutorial for iOS: How To Make A Simple iPhone Game
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2058
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 293 times

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby dyulyur » Fri Dec 24, 2010 11:09 am

Hi, Ray and all developers! Please, help me to understand why [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"music_bg.mp3"] and [[SimpleAudioEngine sharedEngine] playEffect:@"effect1.mp3"] give memory leaks? What is the best for usage mp3 or caf format?
dyulyur
Baby Hacker
 
Posts: 5
Joined: Fri Dec 24, 2010 11:05 am
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby rwenderlich » Fri Dec 24, 2010 10:09 pm

Not sure about the memory leaks. But CAF format is the preferred data format for sounds on the iPhone, see this guide for some more info:

http://www.raywenderlich.com/204/audio-101-for-iphone-developers-file-and-data-formats
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2058
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 293 times

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby dyulyur » Mon Dec 27, 2010 9:12 am

Thank you very much for your great help, Ray!
dyulyur
Baby Hacker
 
Posts: 5
Joined: Fri Dec 24, 2010 11:05 am
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby dyulyur » Wed Dec 29, 2010 8:50 am

Hello, Ray! I really need your help because nobody has answer. My problem is the initial Hello World cocos2d project. It gives memory leaks as you can see with XCode -> Run with performance tool -> Leaks
http://efreedom.com/Question/1-3668558/Cocos2d-Hello-World-Application-Leaking-Bytes How to eliminate this problem? Please, if you know something answer me.
dyulyur
Baby Hacker
 
Posts: 5
Joined: Fri Dec 24, 2010 11:05 am
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby dany88 » Wed Dec 29, 2010 12:53 pm

why changing the class to CCColorLayer and change in init if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
the background remain black? (i added a breakpoint su super initWithColo and it is executed) i have just tried on the last build (0.95) something changed? i created a layer CCColorLayer and added as subview and it work, but why not work as you said?
dany88
n00b
 
Posts: 4
Joined: Wed Dec 29, 2010 12:50 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby simonkinghk999 » Mon Jan 03, 2011 9:39 am

i love you :idea:
simonkinghk999
n00b
 
Posts: 2
Joined: Mon Jan 03, 2011 9:37 am
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby rwenderlich » Mon Jan 03, 2011 7:24 pm

@dany88: Hm, I just made a test project with the latest version of Cocos2D - changed HelloWorld to be CCColorLayer and replaced the first line in init with the following:

Code: Select all
if( (self=[super initWithColor:ccc4(255, 0, 0, 255)] )) {


And the background is now red. That doesn't work for you?
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2058
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 293 times

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby montymccune » Mon Jan 03, 2011 9:39 pm

Hey great tutorial!

I know I have a very simple/NOOB problem on my hands - but I'm unclear as to how resolve it - if anyone could reply, I'd be very appreciative.

My problem deals with the dreaded Memory Management monster.
I have a scenario much like the above tutorial where I am capturing objects to destroy (once collision occurs) in an array and then trying to release them in the Update method.

Here is my Update Method:
Code: Select all
-(void) update:(ccTime)delta
{
   for(GameObject *obj in toBeDestroyed)
   {
      [obj removeSprite];
   }
   [toBeDestroyed removeAllObjects];
}


...where GameObject is a subclass of CCSprite and toBeDestroyed is a NSMutableArray. GameObject's removeSprite method looks like this...
Code: Select all
-(void)removeSprite;
{
   GameScene *game = [GameScene sharedGameScene];
   [game removeChild:self cleanup:YES];
}


..and GameObject also has another method like this...
Code: Select all
-(void)removeBody
{
   if(self.spriteBody != nil)
   {
      b2World *world = spriteBody->GetWorld();
      world->DestroyBody(spriteBody);
      spriteBody = NULL;
   }
}


..that is called in dealloc....
Code: Select all
-(void) dealloc
{
   [self removeBody];
   // don't forget to call "super dealloc"
   [super dealloc];
}


Why do I get EXC_BAD_ACCESS when I try to call "removeAllObjects" on my array?

thanks,
Monty
montymccune
n00b
 
Posts: 1
Joined: Mon Jan 03, 2011 9:26 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How To Make A Simple iPhone Game with Cocos2D Tutorial

Postby rwenderlich » Mon Jan 03, 2011 10:05 pm

@Monty: Hm, not sure offhand... usually that would indicate that something is being double deallocced somewhere... one good way to test for that is to set the NSZombieEnabled flag and checking for warnings, as indicated here: http://www.cocoadev.com/index.pl?NSZombieEnabled

Another good thing to try is Build\Build and Analyze, which often helps you identify problem spots.

If you try those and still can't get anywhere, post or email me your project and I'll take a quick look.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2058
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 293 times

Next

Return to Official Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests