Welcome to Troll Cave Games!

Chapter 8 - Retina patch code

E-mail Print

I knew it.  As soon as I posted out the errata / upgrade document for the book,  a small issue in Chapter 8 was pointed out to me.

Apparently, the method used to keep the hero centered as long as he is not near the edge of the map fails on Retina devices.  The game doesn't break, because the map has hard boundary walls that prevent you from moving off the map, but it does let you see black areas outside the map.  The method that was not playing nicely, in this case, was the setViewpointCenter method, originally published by Ray Wenderlich. (The fault here is mine - Ray's method was written long before Retina displays, and for some reason I didn't notice it in testing.)

The challenge is a points vs. pixels issue, so the fix is very simple.  When the MIN value is checked, the size of the map needs to be divided by the CC_CONTENT_SCALE_FACTOR() to give us a points value to compare to the screen positioning (already in points).

The corrected 2 lines of code for that method are:

x = MIN(x, ((_tileMap.mapSize.width * _tileMap.tileSize.width) / CC_CONTENT_SCALE_FACTOR()) - size.width / 2);
y = MIN(y, ((_tileMap.mapSize.height * _tileMap.tileSize.height) / CC_CONTENT_SCALE_FACTOR()) - size.height / 2);


That will correctly calculate the top and right edges of the map, so it will prevent scrolling past the map (when you see the black areas) in all directions.

If you find any other bugs unintended features, please drop me a line at paul@trollcavegames.com and let me know.

 

Creating Games, with cocos2d 2.1rc1

E-mail Print

Upgrades happen.  Printed books don't.

With the recent release of cocos2d for iphone v.2.1rc1, there are several items in the code from my book, Creating Games with cocos2d for iPhone 2, that either do not function correctly or break entirely.  This is the price we pay for rapid progress, with new features added and new releases happening frequently.  If you are following the code from my book, but want to use the latest unstable release, there are a few things you need to change.  I have put together a document that addresses all of the upgrade issues I have identified.

You can download the file here.

 

tileWidth and boardOffsetX explained (Chapter 1)

E-mail Print

I have received a question regarding the tile size and board offset calculations in Chapter 1, Thanks For the Memory Game, from Creating Games with cocos2d for iPhone 2. These calculations are not taken apart and explained within the text. Although they are similar to the tile spacing, which is explained, I will cover the “why” here. I will focus on the width calculations; the height calculations work the same way, except with height instead of width. (Due to the nature of publishing, we can’t always it every detail into the text, so some small details like this were omitted from the published version.)

The first formula is:
tileWidth = ((boardWidth - (boardColumns * padWidth)) / boardColumns) - padWidth;

Okay, this is pretty dense at first glance. For clarity, let’s review what the variables mean. boardWidth is the total number of points available for the board of tiles to be drawn. boardColumns is the number of columns of tiles we need to display in that area. padWidth is the number of points of spacing we want between our tiles.

We begin with the total boardWidth. From this we subtract (boardColumns * padWidth). Multiplied together, this is the total amount of space we need for the number of columns we want to represent, since each tile will have a padding area after it. When we subtracted that result from the boardWidth, we know that we need to fit our tiles in the remaining space available. The next step is to divide the remaining space by the number of columns we need. Finally, we subtract the padWidth at the end because we have actually include one padding space too many, since we don’t need the padding after the final tile.

Example:
If we have a total boardWidth of 400, boardColumns of 5, and a padWidth of 10, the calculation looks like this:
tileWidth = ((400 - (5 * 10)) / 5) - 10
tileWidth = ((400 – (50)) / 5) - 10
tileWidth = ((350) / 5) - 10
tileWidth = (70) - 10
tileWidth = 60

We can check this result by seeing if the tiles fit in the boardWidth. 5 (boardColumns) * 70 (tileWidth + padWidth) = 350 This means we have 350 width accounted for, with a leftover space of 50. This is where the next formula comes into play.

The second formula is:
boardOffsetX = (boardWidth - ((tileSize.width + padWidth) * boardColumns)) / 2;

I’ve cheated a bit here, as most of this formula is almost exactly what we did to validate the result of the last equation. Skipping the boardWidth for now, we calculate the total size of a tile plus padding, multiply that times the number of requested columns. This gives us the total width (in points) that is taken up by the tiles and the spacing. We subtract that from the boardWidth to see that we have 50 points extra left. We divide that by 2 to see how much of this “leftover space” should be on the left side of the board. This means that even if we do not use the exact amount of screen space we have designated for the board (the boardWidth variable), we can add this boardOffsetX to every tile position so that the complete board of tiles is perfectly centered in our playing area.

Going back to the test equation from the first formula, we already saw that we had a leftover space of 50. This means that once we divide it by 2, then the boardOffsetX would be 25. If you plug in other values into the formula, you will see that this works reliably for any sensible tile size. (Sensible, as in the tiles + padding must be smaller than the total boardWidth, but that should be obvious!)

If you have questions, feel free to drop me a line at paul@trollcavegames.com

 

Creating Games, for cocos2d v.2.1 beta 4

E-mail Print

When Creating Games with cocos2d for iPhone 2 was written, the code was based on the cocos2d v.2.0 (stable) release.  As development is always continuing on cocos2d, small changes in the design may require tweaks to the code.

A few tweaks are needed for the current version, cocos2d v. 2.1 beta 4. I have written a short Addenda for the changes needed to adapt to this latest beta version.  The upgrade document, with some adjusted methods for the 2 Box2D chapters, can be downloaded here.

 

Early Reviews & Free Example Apps

E-mail Print

The early reviews for my book are starting to come in.  Here's a couple of my favorite snippets:

"I am still reading the book and I can’t wait...I am highly recommending the book." - Emanuele Feronato at emanueleferonato.com

"If you can read this book from top to bottom as well as the code attached to the book I think another cocos2d expert will soon be born" - SuperSuRaccoon at supersuraccoon-cocos2d.com

 

All 9 games are now available as free downloads from the App Store, so you can see what we build in the book.  Of course, a lot of the "good stuff" is HOW we build it, not just WHAT we build.

Pack 1 in the App Store (includes Mole Thumper, Brick Breaker, and Pool)

Pack 2 in the App Store (includes Memory, Match 3, Snake, Shooter, Endless Runner)

Pack 3 in the App Store, iPad only (includes Cycles of Light)

 

Be sure to follow me on Twitter to keep up with the news.

 
  • «
  •  Start 
  •  Prev 
  •  1 
  •  2 
  •  3 
  •  4 
  •  Next 
  •  End 
  • »


Page 1 of 4

Book In Print

iOS Apps

Source Code For Sale

Back Burner