Monday, March 29, 2010

Game State implementation.

Continuing where we left off from our last post after creating Game State and Game State Manager classes for our View-Based Application. Yet we have not discuss the auto-generated files App Delegate and View Controller .h & .m files. However we will discuss and work on these files later on when we are ready to work with them. We have not discuss Interface Builder either, which we will discuss later on when we have a good solid foundation to write meaningful source code that are use in our GUI for our app. In this particular case we are interested in writing and implementing the class functions for Game State and Game State Manager classes.

Our last post we talk about the header file for Game State where we describe and explain every line of the source code being presented. I have found this type of research and development to be a good method of study where I ask myself many questions as to how the particular peace of source code work in its smallest denominator. Finding and using the information provided by Apple Documentation is another great way to learn how to use iPhone SDK which are available for free. Quoting and citing specific information from these documentations will allows us to learn a bit about the why, how, and when to implement the particular peace of source code we are currently using. So, please do not be surprise if I use and quote the same information you would to found in Apple Documentation to explain a certain command. This way is how I learn how to further my programming knowledge about Cocoa programming and iPhone programming as well.

Enough blah, blah, blah... Let's get back to programming. To begin our Game State implementation:

#import "GameState.h"
#import "GameStateManager.h"

@implementation GameState

// GameState.m

-(id)initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager
{
if(self = [super initWithFrame: frame])
{
// Initialization code
m_pManager = pManager;
self. userInteractionEnabled = true;
}
return self;
}

-(void) Update
{

}

-(void) Render
{

}

-(void) drawRect:(CGRect)rect
{

}

@end

The first few lines of source code we imported our create header files from Game State and Game State Manager files into our Game State implementation source code. In out forth line we have @implementation Game State and our ending or closing @end line to tell the program this is the allocated area where we are going to create our implementation of the source code for our variables and functions we are going to create. Our first function we create is (id) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager, as mention in our last post according to Apple Documentation "Initializes and returns a newly allocated view object with the specified frame rectangle." we pass in a pointer variable into our function (GameStateManager*)pManager if you remember from our last post we have in our header file of GameState:

@interface GameState : UIView
{
GameStateManager * m_pManager;
}

Which we create a pointer variable that create an instance variable pointer of GameStateManager to switch between States. We also notice from last post that we also inherited from UIView properties which we discuss in our previous post. Now we have our if condition which test if the receiver frame would to initialized frame, if it can without a problem we then received the pManager pointer and assign it to our m_pManager we have create in our header file for Game State. The next line of code is self.userInteractionEnabled = True; as you can see it is a boolean variable pass to the receiver or to be more precise "a boolean value that determines whether user events are ignored and removed from the event queue...If NO, user events -- such as touch and keyboard -- are ignored and removed from the event queue. The default value is YES. To finish this function we return self; or we return the receiver.

The other functions we have are -(void) update, -(void) Render and -(void) drawRect: (CGRect) rect currently do not to be use and initialized for the moment we will later come back and describe them and how they fit into the overall planing of this game. This is all for the Game State implementation file if you have any suggestions, comments and questions about the overall implementation of the Game State files please email me at: julioykaly@hotmail.com.

Julio A. Cruz

No comments:

Post a Comment