Problem with my simple 2D Game engine. Help! - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Problem with my simple 2D Game engine. Help! (/thread-513.html)
Problem with my simple 2D Game engine. Help! - SamBaylus - Dec 15, 2009 01:05 AM
I'm building a 2D game engine for learning purposes. (I know OpenGL ES is the way to go, but baby steps first!)
So I've got a class called "GameObject". In the GameObject class, it's got a few instance variables including "float x", "float y", "float width", "float height" and "UIImage sprite". The "sprite" variable loads a .png file upon initializing, and all this works great. Now then, I've got a class that inherits from UIView that I called "GameView". This is where the trouble begins.
In GameView's "awakeFromNib" I've got this:
So what's happening here is I'm taking all the instances of GameObject that I'm making and I'm storing them into a mutable array, "gameObjectArray". From here, when I need to draw the GameObject's sprites, I pull out each GameObject and draw it's sprite on the screen with the GameObject's given x, y, width, and height. Here's the code for that method:
And this all works fine. Since the x and y variables initialize to 0,0, the correct sprite is shown at the correct spot and all seems to be working great. However, a glimpse into the console window tells me otherwise. I get an error:
I get this over and over and over and over and over for each repeat of the timer. Why is this? Everything is working correctly at the surface, but I'd like to nip this in the bud. Also, is there an easier way to do what I'm doing? Is the drawRect: method the only method I can put the drawing code?
Thanks in advance!
Problem with my simple 2D Game engine. Help! - ThemsAllTook - Dec 15, 2009 07:45 AM
Problem with my simple 2D Game engine. Help! - johncmurphy - Dec 15, 2009 08:02 AM
I know I have encountered this problem before. I recall it had something to do with looping through an array that displayed an image, but I forget the details.
Can you post the code for the GameObject? I will try to reproduce the error.
Problem with my simple 2D Game engine. Help! - johncmurphy - Dec 15, 2009 08:25 AM
Well the array is not the problem.
I get the same error even without the array:
I will continue to look into it.
Problem with my simple 2D Game engine. Help! - johncmurphy - Dec 15, 2009 08:59 AM
I think the simplest answer to this problem is to make your GameObject a subclass of UIImageView, set its image property to your UIImage, and position it by moving its frame around.
Something like this:
Problem with my simple 2D Game engine. Help! - longjumper - Dec 15, 2009 10:06 AM
You should be using CALayer and sending it the setContents message with the image.
Then you should be positioning it by moving its position around.
In fact, this is exactly what CALayer is for.
Back to drawRect:, it is just a method. Writing drawing code in drawRect: doesn't just make it work, there are a lot of other things going on. When an iteration of the run loop is ending, the run loops checks to see if any views need to be redrawn. If they do, a context is prepared and activated. Then the view object is sent the message drawRect: where its drawing code acts on the current context.
You can't just send drawRect: to a view and expect drawing to happen.
(And by the way, drawRect: is invoked by the view's layer thru delegation on the iPhone)