Re: Cocoa Game Development
Re: Cocoa Game Development
- Subject: Re: Cocoa Game Development
- From: Brian Webster <email@hidden>
- Date: Mon, 17 Sep 2001 13:55:17 -0500
On Monday, September 17, 2001, at 01:00 PM, cocoa-dev-
email@hidden wrote:
I have run into a couple problems with game development that I thought
the list could help me with. The game is like F1 sprint which players
race against the computer around one of many tracks. I have a subclass
of NSImageView which shows the track and I add sub views for
the cars. I
have figured out a way to see if the cars are colliding and
take actions
on them but the track is a different story. The cars should follow the
outside of the track when they hit it (penaltied by a reduction in
speed).
You might want to rethink this method of drawing the cars.
Having an NSView for each graphic in your game is going to
require quite a bit of overhead and may get bogged down pretty
quickly if there are even a moderate number of graphics on the
screen, especially if they're all moving around. NSViews are
also not designed to be overlapped or layered, so drawing the
images yourself will give you control over how they are layered,
which ones appear on top, etc.
My recommendation would be to make your own custom view for the
track and override the drawRect: method to do the drawing
yourself. The drawing itself shouldn't be too hard. You can
make a class for the cars on the track, with one instance for
each car, and the instance can keep of the car's speed,
position, etc., as well as having an NSImage of the car. Your
custom view would then just draw the image at the appropriate
spot for each update.
My original thought for handling this was checking the color of the
background image at each of the corners of the cars to see if they are
on the track.
This is a method that many people think of when first designing
a game, but it is a very fragile and inflexible way of doing it,
not to mention a pain in the butt. To handle collisions, etc.,
you'll want to keep an internal representation of the shape of
the track, the bounding rectangles of the cars, and so on. Take
a look at functions like NSIntersectionRect(), NSPointInRect()
(these are documented in the Functions page of the Foundation
docs) and NSBezierPath for more complicated shapes.
I have a feeling that these are not the best methods of controlling the
game. So the overall question that I have is, how should I structure
this game? I think that using sprites as the roads would bog the game
down so I am looking for a different solution???
Using prerendered sprites (i.e. NSImages) for the roads
shouldn't lead to a significant drawing disadvantage, unless
there are many many images that need to be drawn. If you don't
need to have super high quality graphics, you could possibly
draw them using NSBezierPath, which can draw all sorts of lines,
rectangle, polygons, etc.
--
Brian Webster
email@hidden
http://www.owlnet.rice.edu/~bwebster