Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 02-09-2010, 05:16 PM   #4 (permalink)
smasher
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Professional games don't depend on what's happening on the screen to figure out what action happens next. You can get away with it in simple arcade games, and I've even suggested it for simple games in other threads, but it makes the game difficult to port to other platforms and difficult to change resolution, etc. In more complicated games you need to separate the drawing from the model.

The "model" is the computer's mental model of what's happening; you update the model each turn and then you draw what's on the screen later based on the model. So, you need to think in terms of what the computer needs to know in order to make decisions, *not* in terms of what appears on the screen.

A good way to store a map is a two-dimensional array, or an "array of arrays." You could use NSArrays, but it's easier to use C arrays. This gives you a grid in the computer's memory, with rows and columns like a spreadsheet; it's easy to check if there's a tree at x=7 and y=4 is by typing "if (myMap[7][4] == TREE)"

I'd start with a 2d array of ints, with mostly zeroes and a few ones. A zero would represent a clear area, and a one would represent a tree or wall or blocked area.

When it comes to drawing this map, I'd start by having your character stuck to the center of the screen, and move one square at a time. You'll have a grid of UIImageViews too, in a different 2d array; every time you move the character, you'll change the image in each imageView to match the one

Code:
//this code would go inside your "update" method that moves the player
//and redraws the screen

//TODO: move the character, changing playerx and playery
//TODO: this is where you'd check if myMap[newPlayerx][newPlayery]==1 before allowing the move.

//update the grid of imageViews on the screen based on the myMap array.
for (x=0;x<16;i++)
     for (y=0;y<16;i++){
          int tileNum = myMap[x+playerx][y+playery];
          UIImageView imageView =myimageViews[x][y];
          //TODO: replace with an array of images instead of "if" statements,
          //use initWithContentsOfFile, etc.
          if (whatTile==0)
               imageView.image = [UIImageNamed @"grass.png"];
          if (whatTile==1)
               imageView.image = [UIImageNamed @"tree.png"];
     }
}
Hope I didn't start over your head. If you need the code to initialize the arrays and add the imageViews to the screen I can add that too.

BTW in my model everything is measured in map squares, and I convert to pixels when drawing to the screen. So a tree is one map square wide, although it may be 16 pixels on the screen. That makes the math easier inside the model.

PS - Professional games usually use OpenGL or possibly Cocos2D instead of imageViews. I think imageViews are good for a first game / learning experience though. You can still draw about 80 moving imageViews to the screen if you'll settle for 15FPS, or about 50 items at 20 FPS.
__________________

Free Games!

Last edited by smasher; 02-09-2010 at 05:20 PM.
smasher is offline   Reply With Quote
 

» Advertisements
» Online Users: 263
16 members and 247 guests
ADY, ckgni, IphoneSdk, iph_s, jakerocheleau, karunaskj, MarkC, marshusensei, michelle, peterkessler45, rob.mccarthy, rodrigozanatta, Rudy, Speed, Xaron
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,877
Threads: 89,218
Posts: 380,695
Top Poster: BrianSlick (7,129)
Welcome to our newest member, peterkessler45
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:29 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.