<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Things you wish you knew when you started with cocos2d?</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/737</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 01:23:08 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/737" rel="self" type="application/rss+xml" />

		<item>
			<title>Panajev on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-100763</link>
			<pubDate>Fri, 24 Jun 2011 07:52:11 +0000</pubDate>
			<dc:creator>Panajev</dc:creator>
			<guid isPermaLink="false">100763@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><a href="https://github.com/cjhanson/Objective-C-Optimized-Singleton" rel="nofollow">https://github.com/cjhanson/Objective-C-Optimized-Singleton</a></p>
<p>That singleton macro can be easily used in any exiting Objective-C class and to do the serialization and de-serialization process you can still use the +alloc and  -initWithCoder: methods if you so wish.
</p></description>
		</item>
		<item>
			<title>Clain on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-97821</link>
			<pubDate>Tue, 07 Jun 2011 22:15:17 +0000</pubDate>
			<dc:creator>Clain</dc:creator>
			<guid isPermaLink="false">97821@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/49014'>Mustafa</a>: you're welcome. I'm glad (and amused) to see this is still helpful to some people.
</p></description>
		</item>
		<item>
			<title>Mustafa on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-97756</link>
			<pubDate>Tue, 07 Jun 2011 15:15:10 +0000</pubDate>
			<dc:creator>Mustafa</dc:creator>
			<guid isPermaLink="false">97756@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Just wanted to send my thanks to @<a href='http://www.cocos2d-iphone.org/forum/profile/208'>clain</a> and to @NatWeiss for their code samples - they were exactly what I needed to get going with my Game State code.</p>
<p>-m
</p></description>
		</item>
		<item>
			<title>salmansqadeer on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-84779</link>
			<pubDate>Sat, 26 Mar 2011 21:34:03 +0000</pubDate>
			<dc:creator>salmansqadeer</dc:creator>
			<guid isPermaLink="false">84779@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Oh and LEVEL CREATION!!!  Thats got to be the one thing that im having the most difficulty with - dealing with SVG files (cause i dont want tilemap styled levels) to easily create multiple levels effectively and efficiently without having to roll my own solution.</p>
<p>(IT WOULD BE AWESOME if this was part of the cocos2d framework in fact i think ill post it)
</p></description>
		</item>
		<item>
			<title>salmansqadeer on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-84776</link>
			<pubDate>Sat, 26 Mar 2011 21:17:44 +0000</pubDate>
			<dc:creator>salmansqadeer</dc:creator>
			<guid isPermaLink="false">84776@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Any chance the presentation that was the original starting point of this thread can be found online anywhere?</p>
<p>Personally, the issues that I have faced are the following:</p>
<p>- Cocos2d Game Architecture best practices<br />
- How to best logically divide layers (HUD, Touch, Player, Level, etc), and how to get layers to communicate with one another (im using boolean switches which I KNOW is not the best way to do it but is simply the easiest lol<br />
- Moving from proof of concept (that you can build from a simple tutorial) to a full game -<br />
- The steps you should take in architecting and designing your game for future extensibility and so you dont have to refactor your code if you want to add new features.</p>
<p>Would LOVE to begin a wiki holding all of the insights that I have gotten just by reading this thread
</p></description>
		</item>
		<item>
			<title>Benihana on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-57394</link>
			<pubDate>Wed, 29 Sep 2010 21:50:49 +0000</pubDate>
			<dc:creator>Benihana</dc:creator>
			<guid isPermaLink="false">57394@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>This isn't cocos specific, but I do wish I had thought about it prior to the start of my project. I use one coordinate system to represent a linear non-manipulated grid (such as a tiled map, or non-isometric representation), and another for the screen displayed coordinates. Thus any transformations need only go from one coordinate system to the other, rather than copying complicated code or having inconsistencies.</p>
<p>I use UV for the pre-transformed, and XY for the screen display, so it becomes easy to know which I am dealing with by naming my properties: UVPosition and XYPosition, or similar.
</p></description>
		</item>
		<item>
			<title>Nat Weiss on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-27334</link>
			<pubDate>Fri, 19 Feb 2010 19:45:00 +0000</pubDate>
			<dc:creator>Nat Weiss</dc:creator>
			<guid isPermaLink="false">27334@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/25'>PhilM</a> thank you! That was what we needed.</p>
<p>Here's the implementation of @<a href='http://www.cocos2d-iphone.org/forum/profile/208'>Clain</a>'s suggestions above. How does it look? Any "best practices" suggestions?</p>
<p>In particular, do I need to have <code>@synchronized()</code> around my <code>saveState</code> method?</p>
<p>Is there a difference between <code>@synchronized([GameState class])</code> and <code>@synchronized(self)</code>?</p>
<p>Am I retaining and releasing the <code>savePath</code> and <code>currentLevel</code> correctly?</p>
<p>GameState.h:<br />
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;Import.h&#34;

@interface GameState : NSObject &#60;NSCoding&#62;
	{
		NSString* currentLevel;
		CGFloat health,maxHealth,strength,dexterity;
		int swordLevel;
		BOOL terminating;
	}

	@property (copy) NSString* currentLevel;
	@property CGFloat health,maxHealth,strength,dexterity;
	@property int swordLevel;
	@property BOOL terminating;

	+(GameState*) get;
	+(void) purge;
	+(void) loadState;
	+(void) saveState;
	+(NSString*) makeSavePath;

	-(void) clear;

@end</code></pre>
<p>GameState.m:<br />
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;Import.h&#34;

static GameState* singleton = nil;

@implementation GameState

	@synthesize currentLevel,health,maxHealth,strength,dexterity,swordLevel;

	+(GameState*) get
	{
		@synchronized(self)
		{
			// create our single instance
			if(singleton == nil)
				singleton = [[self alloc] init];
		}
		return singleton;
	}

	+(id) alloc
	{
		@synchronized(self)
		{
			// assert that we are the only instance
			NSAssert(singleton == nil, @&#34;There can only be one GameState&#34;);
			return [super alloc];
		}
		return nil;
	}

	+(void) purge
	{
		@synchronized(self)
		{
			[singleton release];
		}
	}

	+(void) loadState
	{
		@synchronized(self)
		{
			// release any existing instance
			[singleton release];

			// load our data
			NSString* savePath = [self makeSavePath];
			singleton = [[NSKeyedUnarchiver unarchiveObjectWithFile:savePath] retain];
			[savePath release];

			// couldn&#39;t load?
			if(singleton == nil)
			{
				NSLog(@&#34;Couldn&#39;t load game state, so initialized with defaults&#34;);
				[self get];
			}

			NSLog(@&#34;Loaded game state %@&#34;, singleton);
		}
	}

	+(void) saveState
	{
		// save our data
		NSString* savePath = [self makeSavePath];
		[NSKeyedArchiver archiveRootObject:[GameState get] toFile:savePath];
		[savePath release];

		NSLog(@&#34;Saved game state %@ to file %@&#34;, singleton, savePath);
	}

	+(NSString*) makeSavePath
	{
		// make save path from our document&#39;s directory
		NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString* documentsDirectory = [paths objectAtIndex:0];
		NSString* savePath = [documentsDirectory stringByAppendingPathComponent:@&#34;savefile.sav&#34;];
		[savePath retain];
		return savePath;
	}

	-(id) init
	{
		self = [super init];
		if( self != nil )
		{
			[self clear];
			NSLog(@&#34;gameState init %@&#34;, self);
		}
		return self;
	}

	-(void) dealloc
	{
		[currentLevel release];
		[super dealloc];
	}

	-(NSString*) description
	{
		return [NSString stringWithFormat:@&#34;level=%@, health=%.1f, maxHealth=%.1f, strength=%.1f, dexterity=%.1f, swordLevel=%d&#34;,
			currentLevel, health, maxHealth, strength, dexterity, swordLevel];
	}

	-(void) clear
	{
		// erase all data
		[currentLevel release];
		currentLevel = nil;

		health = maxHealth = 100.0f;
		strength = 6.0f;
		dexterity = 5.0f;
		swordLevel = 0;

		terminating = NO;
	}

	-(id) initWithCoder:(NSCoder*)coder
	{
		self = [super init];
		if( self != nil )
		{
			// decode data
			currentLevel = [[coder decodeObjectForKey:@&#34;currentLevel&#34;] retain];
			health = [coder decodeFloatForKey:@&#34;health&#34;];
			maxHealth = [coder decodeFloatForKey:@&#34;maxHealth&#34;];
			strength = [coder decodeFloatForKey:@&#34;strength&#34;];
			dexterity = [coder decodeFloatForKey:@&#34;dexterity&#34;];
			swordLevel = [coder decodeIntForKey:@&#34;swordLevel&#34;];
			terminating = NO;
		}
		return self;
	}

	-(void) encodeWithCoder:(NSCoder*)coder
	{
		// encode data
		[coder encodeObject:currentLevel forKey:@&#34;currentLevel&#34;];
		[coder encodeFloat:health forKey:@&#34;health&#34;];
		[coder encodeFloat:maxHealth forKey:@&#34;maxHealth&#34;];
		[coder encodeFloat:strength forKey:@&#34;strength&#34;];
		[coder encodeFloat:dexterity forKey:@&#34;dexterity&#34;];
		[coder encodeInt:swordLevel forKey:@&#34;swordLevel&#34;];
	}

@end</code></pre></description>
		</item>
		<item>
			<title>PhilM on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-27286</link>
			<pubDate>Fri, 19 Feb 2010 04:57:37 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">27286@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I think you need to provide the full path.</p>
<p>This is what I pass to archiveRootObject to save to the apps document directory (the only one thats writable from your app I believe):</p>
<pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@&#34;savefile.sav&#34;];</code></pre></description>
		</item>
		<item>
			<title>Nat Weiss on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-27259</link>
			<pubDate>Thu, 18 Feb 2010 23:13:28 +0000</pubDate>
			<dc:creator>Nat Weiss</dc:creator>
			<guid isPermaLink="false">27259@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi. I've implemented a singleton class similar to @<a href='http://www.cocos2d-iphone.org/forum/profile/208'>Clain</a>'s. Though, I'm having trouble using the NSKeyedUnarchiver's unarchiveObjectWithFile method. I don't know what file name and/or path to use. I've tried:<br />
<code>[NSKeyedUnarchiver unarchiveObjectWithFile:@&#34;savefile.sav&#34;]</code><br />
and<br />
<code>[NSKeyedUnarchiver unarchiveObjectWithFile:[CCFileUtils fullPathFromRelativePath:@&#34;savefile.sav&#34;]]</code><br />
and<br />
<code>[NSKeyedUnarchiver unarchiveObjectWithFile:[CCFileUtils fullPathFromRelativePath:@&#34;./savefile.sav&#34;]]</code><br />
and I've even tried creating a "savefile.plist" in my bundle's resources and using that.</p>
<p>All of these attempts return nil except in the case of loading my bundle's "savefile.plist" which always returns the original plist file, even after I have encoded and saved the data back to it.</p>
<p>What am I doing wrong?
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15692</link>
			<pubDate>Fri, 16 Oct 2009 19:52:41 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15692@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Will do, thanks Codemattic.  I'd love to get this figured out as well  :)
</p></description>
		</item>
		<item>
			<title>Codemattic on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15691</link>
			<pubDate>Fri, 16 Oct 2009 19:48:58 +0000</pubDate>
			<dc:creator>Codemattic</dc:creator>
			<guid isPermaLink="false">15691@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>"I figured that since NSValue conforms to NSCoder" yes - it really seems like it should! But Im looking at the CAAdditions specifically sayd it conforms to NSCoding:</p>
<p><a href="http://developer.apple.com/IPhone/library/documentation/GraphicsImaging/Reference/NSValue_CA_additions/Introduction/Introduction.html" rel="nofollow">http://developer.apple.com/IPhone/library/documentation/GraphicsImaging/Reference/NSValue_CA_additions/Introduction/Introduction.html</a></p>
<p>"I don't know how it would fit into this scenario, but there is a<br />
-(CGPoint) encodeWithCGPoint:forKey"</p>
<p>I dont see how you can use that since your CGPoint is inside an NSArray.</p>
<p>@<a href='http://www.cocos2d-iphone.org/forum/profile/1743'>eshirt</a>, if you want to continue this discussion, could you please start a new thread in "everything else"? This has become off-topic for this thread and for this group. Id fork it, but I dont know how without deleting the previous messages, so feel free to start a new thread in "everything else" regarding serializing CGPoints. It would be a good thing to figure out actually since Riq plans for adding serializing to cocos and CGPoints will have to be a part of that. Thanks!
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15657</link>
			<pubDate>Fri, 16 Oct 2009 15:37:09 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15657@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I don't know how it would fit into this scenario, but there is a</p>
<p><code>-(CGPoint) encodeWithCGPoint:forKey</code></p>
<p>method declared in UIGeometry that should be useful, but how would I use that in my encoder method?  </p>
<p>Could I iterate through the array in that method and encode each point individually then save those?  Obviously, I'd then have to create a new array in my decode to hold all the points.  That doesn't seem like the most efficient way to do this, but would that work?
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15654</link>
			<pubDate>Fri, 16 Oct 2009 15:12:12 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15654@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hmm, I guess I'm just confused then.  I figured that since NSValue conforms to NSCoder, it's additions would.</p>
<p>I've searched everywhere for solutions and this was actually the recommended solution on another forum.  The people there didn't seem to have any trouble getting it to work, but maybe their circumstances were different.</p>
<p>Unless anyone else has a better way, then I guess I'll have to start working on subclassing CGPoint to make it conform.  Any ideas how this would affect (if at all) any usage of ccp() in my code?
</p></description>
		</item>
		<item>
			<title>Codemattic on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15653</link>
			<pubDate>Fri, 16 Oct 2009 15:03:05 +0000</pubDate>
			<dc:creator>Codemattic</dc:creator>
			<guid isPermaLink="false">15653@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>you know what I have just noticed - NSValue UIGeometry Additions Reference doesnt say it conforms to NSCoding!</p>
<p><a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSValue_UIKit_Additions/Reference/Reference.html" rel="nofollow">http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSValue_UIKit_Additions/Reference/Reference.html</a></p>
<p>D'oh!
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15648</link>
			<pubDate>Fri, 16 Oct 2009 14:45:52 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15648@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>sorry - i mistyped.  that's what I get for trying to make my code generic in the forums  :)</p>
<p>It's actually defined in the header as an ivar.  when i set it, it looks like this:</p>
<p><code>myArray = [blah blah blah]</code></p>
<p>I've got about 8 other BOOL and Int values that get declared and encoded in the same way, and those work fine.  It's something about having CGPoints in this array that's getting me stuck.
</p></description>
		</item>
		<item>
			<title>PhilM on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15646</link>
			<pubDate>Fri, 16 Oct 2009 14:43:44 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">15646@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>hmm, I've just saved CGPoints as 2 separate floats in the past when using NSCoding (mainly because I didn't know about the valueWithCGPoint method).</p>
<p>If for whatever reason an NSValue with valueWithCGPoint can't be NScoded, perhaps creating a simple custom CGPoint class that extends from NSObject &#38; conforms to NSCoding would do? Not ideal, but might be workable...
</p></description>
		</item>
		<item>
			<title>Codemattic on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15645</link>
			<pubDate>Fri, 16 Oct 2009 14:39:00 +0000</pubDate>
			<dc:creator>Codemattic</dc:creator>
			<guid isPermaLink="false">15645@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>in your -init myArray is a temporary variable. In -encodeWithCoder: where does it get the myArray variable, where is that defined?
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15634</link>
			<pubDate>Fri, 16 Oct 2009 13:37:22 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15634@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Sorry, I thought the code I put in was enough.  Here's a more complete description:</p>
<pre><code>-(id) init {
// normal init stuff
NSArray *myArray = [[NSArray alloc] initWithObjects:[NSValue ValueWithCGPoint:CGPointMake(0, 280)], [NSValue valueWithCGPoint: CGPointMake(0, 245)], nil];</code></pre>
<pre><code>-(void)encodeWithCoder:(NSCoder *)coder {
// this line is where the debugger throws a fit
[coder encodeObject:myArray forKey:@&#34;myArray&#34;];</code></pre>
<p>Here's the line from the console:</p>
<p>[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs.</p>
<p>Obviously, encodeObject:forKey, when called on an Array goes and tries to archive each item in the array.  I think that's where it calls encodeValueOfObjCType:at:  But it should work, right?  Since I converted the CGPoints into NSValues?
</p></description>
		</item>
		<item>
			<title>Codemattic on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15618</link>
			<pubDate>Fri, 16 Oct 2009 11:39:32 +0000</pubDate>
			<dc:creator>Codemattic</dc:creator>
			<guid isPermaLink="false">15618@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>show us the line of code where you get the error!
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15593</link>
			<pubDate>Fri, 16 Oct 2009 02:13:38 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15593@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>So, NSCoding is working great for NSStrings, BOOLs and ints.  Thanks again for the help!  I'm having an issue now with encoding an array filled with points.</p>
<p>My basic need is to store CGPoints (about 20 at a time).  Naturally, I'd want to store them in an NSArray, and since I want to save that info, encode/decode with NSCoder.  CGPoint is a struct that doesn't conform to NSCoder.  NSValue does conform to NSCoder, so what I've done is something like this:</p>
<p>`NSArray *array = [[NSArray alloc] initWIthObjects:[NSValue valueWithCGPoint: CGPointMake(0,0), etc, nil];</p>
<p>Then when I try to use encodeObject:forKey: on it, the compiler gives me an error: "[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs"</p>
<p>Any ideas?
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15056</link>
			<pubDate>Sun, 11 Oct 2009 03:08:25 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15056@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Got it THANKS!  :)  I knew it had to be a retain that I missed in there.  I just retained my Strings and all is well.</p>
<p>GIve a man a fish, he eats for a day - teach a man to fish, he'll finally understand memory management.  :)
</p></description>
		</item>
		<item>
			<title>Steve Oldmeadow on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15051</link>
			<pubDate>Sun, 11 Oct 2009 02:35:55 +0000</pubDate>
			<dc:creator>Steve Oldmeadow</dc:creator>
			<guid isPermaLink="false">15051@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/1743'>eshirt</a> - decodeObjectForKey returns an autoreleased object.  BOOL and int aren't objects so they don't need to be retained.  Ringing any bells?
</p></description>
		</item>
		<item>
			<title>eshirt on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-15049</link>
			<pubDate>Sun, 11 Oct 2009 02:23:50 +0000</pubDate>
			<dc:creator>eshirt</dc:creator>
			<guid isPermaLink="false">15049@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Sorry to revive an old thread, but I'm trying to do save game data using Clain's advice.  So far, it's working great, but I'm getting an unexpected crash when trying to access a decoded NSString value.</p>
<p>In my GameData singleton class, I have declared a NSString *player.  On a new game, it's set to nil.  Upon choosing a player, it's set to the appropriate string value.  That all works fine.  When I load the game up a second time, some of the game data is showing up just fine.  But every time I try to display that string value, it crashes.</p>
<p>Here's what I have in my encodeWithCoder method:<br />
<code>[coder encodeObject:player forKey:@&#34;homeTeam&#34;];</code></p>
<p>And my initWithCoder method:<br />
<code>player = [coder decodeObjectForKey:@&#34;playerName&#34;];</code></p>
<p>Then in my GameLayer:<br />
<pre><code>GameData *myGame = [GameData sharedData];
Label *playerName = [Label labelWithString:myGame.player fontName:@&#34;Helvetica&#34; fontSize:48];
playerName.position = ccp(240, 200);
[self addChild:playerName z:5];</code></pre>
<p>That's where I get my crash.  I am able to successfully retrieve int and BOOL values from my GameData file, but the string seems to be the problem...</p>
<p>Any suggestions?
</p></description>
		</item>
		<item>
			<title>tvalleau on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-13445</link>
			<pubDate>Sat, 26 Sep 2009 22:14:05 +0000</pubDate>
			<dc:creator>tvalleau</dc:creator>
			<guid isPermaLink="false">13445@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I'll give it a go!  Thanks for the quick response.  (other suggestions welcomed, too.)
</p></description>
		</item>
		<item>
			<title>PhilM on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-13443</link>
			<pubDate>Sat, 26 Sep 2009 21:36:17 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">13443@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>If you use cocos2d as a static lib, you can flip between versions very easily, although any api changes will need to be done to your own code as normal.</p>
<p>I followed this tutorial some time ago, and its made updating a breeze:<br />
<a href="http://www.clintharris.net/2009/iphone-app-shared-libraries/" rel="nofollow">http://www.clintharris.net/2009/iphone-app-shared-libraries/</a></p>
<p>Not sure if any of the current templates take this into account.
</p></description>
		</item>
		<item>
			<title>tvalleau on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737/page/2#post-13442</link>
			<pubDate>Sat, 26 Sep 2009 21:21:46 +0000</pubDate>
			<dc:creator>tvalleau</dc:creator>
			<guid isPermaLink="false">13442@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Forgive me... I'm an old guy... and I did look around here before posting. The biggest problem I have is that I don't understand how to update a project to a new version of Cocos2D. I just took a working project and replaced the existing files with the 8.2beta... and promptly go about 6000 errors.... Lots of "can't find Support/soemthingorother... but near as I can tell, somethingorother is right where it ought to be.</p>
<p>Obviously/surely this is trivial... but I sure can't figure it out. After mucking about for several hours; looking in the forums (and apparently missing it) I've simply got to ask: what am I missing here?</p>
<p>How (preferably in simple terms) do I set up a project so that when (say) 8.2 final comes out, I'm not faced with a nightmare?</p>
<p>Sorry to be such a newb... but I guess one has to start somewhere
</p></description>
		</item>
		<item>
			<title>Clain on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737#post-10591</link>
			<pubDate>Fri, 04 Sep 2009 00:28:21 +0000</pubDate>
			<dc:creator>Clain</dc:creator>
			<guid isPermaLink="false">10591@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Conforming to the <code>NSCoding</code> protocol just means that you define the <code>initWithCoder</code> and <code>encodeWithCoder</code> methods in your class. You don't have to encode all your properties/variables. You can encode and decode whatever you want in order to recreate your object correctly in the <code>initWithCoder</code> method. What you want to do with your chipmunk stuff seems to be the way to go.
</p></description>
		</item>
		<item>
			<title>pabloruiz55 on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737#post-10585</link>
			<pubDate>Thu, 03 Sep 2009 21:25:39 +0000</pubDate>
			<dc:creator>pabloruiz55</dc:creator>
			<guid isPermaLink="false">10585@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/208'>Clain</a>: Thanks for that mini tutorial! I have one question:</p>
<blockquote><p>If I had subclassed the Sprite class to make a BubbleSprite containing the index, color and captured variables as well as the TextureNode code to draw itself, I wouldn't have been able to save the game like that because the cocos classes don't conform to the NSCoding protocol.</p></blockquote>
<p>If you had your Buble class like this:</p>
<p>@interface Bubble : NSObject &#60;NSCoding&#62; {<br />
    int index, color;<br />
    BOOL captured;<br />
    Sprite * sprite;<br />
}</p>
<p>But your encodewithcoder remained the same, would it work?<br />
I mean, do i have to encode/decode ALL of the properties of a class to make it conform to NSCoding?</p>
<p>In my game i have some class with a chipmunk body and shape, i would like to encode it, obviously i can't encode the shape/body, but i could encode the integers/floats, image path string, etc. to recreate it.</p>
<p>Thanks!
</p></description>
		</item>
		<item>
			<title>Clain on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737#post-8224</link>
			<pubDate>Sun, 16 Aug 2009 16:54:16 +0000</pubDate>
			<dc:creator>Clain</dc:creator>
			<guid isPermaLink="false">8224@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hey Phil, thanks for your feedback. I agree, there are always new ideas coming along during development. A GDD can never be totally complete and accurate. However, when you discuss new ideas with other people working on the project (especially the graphics artists and level designers), it's important to explain them how these changes would affect the code and/or how much time it would take to integrate them. Sometimes you have to reject new ideas in order to meet the deadline. You can also keep them in mind for a future update. If you're close to releasing your game, you probably don't want to add new features, see this interesting <a href="http://developer.valvesoftware.com/wiki/Making_a_Mod#Five_weeks_out">article on the Valve Developer Community's wiki</a>.</p>
<p>Back to today's topic : the Singleton design pattern. I briefly talked about it <a href="http://www.cocos2d-iphone.org/forum/topic/854#post-5179">in this post</a>. In fact, I don't have much to add ;) I don't do unit tests in my game programs so I don't really care about singletons possibly breaking them. Global state can be tricky and that's why you need to know what you're doing. By the way, an interesting discussion on the Singleton controversy can be found here : <a href="http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial">google-singleton-detector/wiki/WhySingletonsAreControversial</a> (read the comments as well).</p>
<p>As Phil said about singletons : "I've found them very useful, as long as you plan out what you want them to store in the first place". Agreed. And if you followed the steps I described in my previous post on this thread, you should be able to determine what needs to be made global (in general, it's what needs to be accessed from different places in the code). I'm not going to paste the code to create a Singleton class since it's been done more than once on this forum (and the old google one) and you can always look at the following cocos classes which are all singletons : Director, TextureMgr, TouchDispatcher, ActionManager, SimpleAudioEngine, Scheduler. Instead, I'm going to talk about how I used the singleton pattern to both hold my global variables during the game and manage save and load stuff.</p>
<p>But before that, please read this thread : <a href="http://www.cocos2d-iphone.org/forum/topic/195">Component based entity systems</a>. The topic has been quickly hijacked by ob1 and slipster216. It turned into a very interesting discussion around MVC and NSCoding. Worth reading !<br />
<pre><code>.
.
.</code></pre>
<p>Alright ! Let me explain how I wrote the save/load functions in the following game : <a href="http://www.cocos2d-iphone.org/forum/topic/310">Dr Bubbles (this links to the cocos2d games announcement forum)</a>.</p>
<p>My singleton code is in the <code>GameState</code> class. In this singleton instance, I put all the global variables needed for the game. For example : <code>level</code>, <code>shotsLeft</code>, <code>score</code>, <code>currentColor</code>, <code>bonusX2</code> and <code>bubbles</code> (of course, there are a lot more but let's keep it simple). If you read the <a href="http://www.cocos2d-iphone.org/forum/topic/195">Component based entity systems</a> thread, these variables represent my model in the MVC pattern (though it's not really MVC, as ob1 pointed out in that thread). This model is all I need to resume a game. So I want the singleton instance to be serializable to easily save and load games. To achieve that, you have it conform to the <code>NSCoding</code> protocol. This what my GameState.h file looks like :<br />
<pre><code>@interface GameState : NSObject &#60;NSCoding&#62; {
    int level, shotsLeft, score, currentColor;
    BOOL bonusX2;
    NSMutableArray *bubbles;
}</code></pre>
<p>Of course, every variable is defined as a property (eg. <code>@property BOOL bonusX2;</code>). I then define a bunch of methods. The relevant one regarding the singleton pattern is :<br />
<pre><code>+(GameState *)gameState;</code></pre>
<p>Since I want to be able to recreate the singleton from a saved file, I also have this method :<br />
<pre><code>+(void)loadGameStateFromFile:(NSString *)file;</code></pre>
<p>In my GameState.m file, I have all the <code>@synthesize</code> statements for my variable. I have the regular <code>+ (GameState *)gameState { ... }</code> and <code>+(id)alloc { ... }</code> functions for the singleton pattern. The singleton instance is defined as usual : <code>static GameState *_gameState = nil;</code></p>
<p>And the <code>loadGameStateFromFile</code> :<br />
<pre><code>+(void)loadGameStateFromFile:(NSString *)file {
    @synchronized([GameState class]) {
        [_gameState release];
        _gameState = [[NSKeyedUnarchiver unarchiveObjectWithFile:file] retain];
    }
}</code></pre>
<p>I then have the <code>- (void) dealloc { ... }</code> and <code>- (id) init { ... }</code> methods. In <code>init</code>, I initialize my variables with default values. In dealloc I release what needs to be released (in my case, I release the <code>bubbles</code> array). What's more interesting is the 2 methods of the <code>NSCoding</code> protocol :<br />
<pre><code>- (void) encodeWithCoder:(NSCoder *)coder {
    [coder encodeInt:level forKey:@&#34;level&#34;];
    [coder encodeInt:shotsLeft forKey:@&#34;shotsLeft&#34;];
    [coder encodeInt:score forKey:@&#34;score&#34;];
    [coder encodeInt:currentColor forKey:@&#34;curColor&#34;];
    [coder encodeBool:bonusX2 forKey:@&#34;bonusX2&#34;];
    [coder encodeObject:bubbles forKey:@&#34;bubblesArray&#34;];
}
- (id) initWithCoder:(NSCoder *)coder {
    self = [super init];
    level = [coder decodeIntForKey:@&#34;level&#34;];
    shotsLeft = [coder decodeIntForKey:@&#34;shotsLeft&#34;];
    score = [coder decodeIntForKey:@&#34;score&#34;];
    currentColor = [coder decodeIntForKey:@&#34;curColor&#34;];
    bonusX2 = [coder decodeBoolForKey:@&#34;bonusX2&#34;];
    bubbles = [[coder decodeObjectForKey:@&#34;bubblesArray&#34;] retain];
    return self;
}</code></pre>
<p><strong>Important : </strong><br />
My <code>bubbles</code> array contains objects of type <code>Bubble</code>. This <code>Bubble</code> class needs to conform to the <code>NSCoding</code> protocol, otherwise the above function won't be able to serialize the array. For example, in Bubble.h :<br />
<pre><code>@interface Bubble : NSObject &#60;NSCoding&#62; {
    int index, color;
    BOOL captured;
}
@property int index, color;
@property BOOL captured;
@end</code></pre>
<p>And in Bubble.m :<br />
<pre><code>@implementation Bubble

@synthesize index, color, captured;

- (void) encodeWithCoder:(NSCoder *)coder {
    [coder encodeInt:index forKey:@&#34;index&#34;];
    [coder encodeInt:color forKey:@&#34;color&#34;];
    [coder encodeBool:captured forKey:@&#34;captured&#34;];
}
- (id) initWithCoder:(NSCoder *)coder {
    self = [super init];
    index = [coder decodeIntForKey:@&#34;index&#34;];
    color = [coder decodeIntForKey:@&#34;color&#34;];
    captured = [coder decodeBoolForKey:@&#34;captured&#34;];
    return self;
}</code></pre>
<p>To save the GameState, when the application is about to terminate, you just do :<br />
<pre><code>[NSKeyedArchiver archiveRootObject:[GameState gameState] toFile:file];</code></pre>
<p>And when the app is launched again, you check if the saved file exists, and if it does, you do :<br />
<pre><code>[GameState loadGameStateFromFile:file];</code></pre>
<p>And that's it. It works very well for a game like Dr Bubbles because it's easy to dissociate the bubble sprite from the bubble data. If I had subclassed the <code>Sprite</code> class to make a <code>BubbleSprite</code> containing the <code>index</code>, <code>color</code> and <code>captured</code> variables as well as the <code>TextureNode</code> code to draw itself, I wouldn't have been able to save the game like that because the cocos classes don't conform to the <code>NSCoding</code> protocol.
</p></description>
		</item>
		<item>
			<title>PhilM on "Things you wish you knew when you started with cocos2d?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/737#post-7247</link>
			<pubDate>Sat, 08 Aug 2009 00:45:46 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">7247@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Clain, nice post, thanks. </p>
<p>Would be interested to also hear your views on singletons, as there does seem to be a mixed reaction to their usefulness and whether they should be used at all by some users here. I've found them very useful, as long as you plan out what you want them to store in the first place, otherwise (in my first few attempts at using them), they quickly grew cluttered and hard to maintain (I tried to put everything inc. the kitchen sink at first).</p>
<p>Agree with your building small dedicated test applications. Since starting out using cocos2d (my first real attempt at developing any games more than as simple flash pong some years ago), I've built alot of little prototype apps to test certain features and learn the language and platform, whilst experimenting with various game ideas. </p>
<p>When I tried to initially build something more in depth, that combined a few different concepts, it soon grew into a mess. So I instead started from scratch again, put pen to paper with my designer, tried to get the rough key elements laid out on paper, and this process really helped us start to visualize how the game could be structured. </p>
<p>My main issue since then, and I presume this happens to a lot of newbie game developers, is your game idea can evolve, and as it evolves, you try and shoe horn in the extra features. Which if not thought out carefully (e.g. you want to experiment with new feature XYZ), you get it working, but its not extensible, or structured well, and can lead to spaghetti code. But when I started that app, I didn't think that idea would be feasible, or just hadn't thought about going down that route, so hadn't planned for it.</p>
<p>I know, I know, a good game design doc at the start is the ideal situation, but when your starting out, its hard to judge what you'll be able to physically pull off, so its hard to plan. At least thats what I found out. </p>
<p>After months of learning, and a couple of titles almost ready to ship, hopefully in the next 4 to 8 weeks, I feel more confident about starting future projects in a much more structured way. Just as I would do for my web dev work I do by day...
</p></description>
		</item>

	</channel>
</rss>

