I've been scouring the web for the past 2 days trying to find a complete example of how to use the built-in game loop features in Sparrow for ios development. I am working on a tower defense type of game for a school project. The player controls a gun turret in the center of the screen while tanks enter the screen randomly from all 4 sides. Here is the link to a screen shot:
I have a "TankManager" class that creates and handles instances of my "Tank" class by storing instances of Tank in an NSMutableArray
called "activeTanks", and I put my "tankImage" into a "tankSprite" which is nested in a "tankManagerSprite" which is nested in my main "_contents" sprite. As you can see in the image at the link above, they show up fine.
I want to animate the tanks. From my reading I understand that I need to have a game loop which calls a method every fraction of a second. According to the Sparrow wiki (http://wiki.sparrow-framework.org/manual/animation), to use Sparrow's built-in game loop feature I do something similar to the following:
Add this code to the init function in the class where I want to use it(the class needs to be an SPEvent
:)
// e.g. in the init-method
[self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
And then put this code somewhere (I chose to put it in my TankManager.m file, but I don't know if that is right):
- (void)onEnterFrame:(SPEnterFrameEvent *)event
{
for (Tank *thisTank in activeTanks)
{
[thisTank move:event.passedTime];
}
}
And then in the "-(void)move:(double)timeElapsed;
" function in my "Tank" class:
-(void)move:(double)timeElapsed{
NSLog(@"xCoord: %f", tankVector.vertex.xCoord);
NSLog(@"yCoord: %f", tankVector.vertex.xCoord);
tankVector.vertex.xCoord += (timeElapsed *20);
tankVector.vertex.yCoord += (timeElapsed *20);
}
As a result I get NSLog()
output that looks like this (only one tank being generated):
2013-04-20 00:35:43.449 Tank Turret Game[7815:c07] xCoord: 172.677765
2013-04-20 00:35:43.450 Tank Turret Game[7815:c07] yCoord: 172.677765
2013-04-20 00:35:43.482 Tank Turret Game[7815:c07] xCoord: 173.343170
2013-04-20 00:35:43.483 Tank Turret Game[7815:c07] yCoord: 173.343170
2013-04-20 00:35:43.516 Tank Turret Game[7815:c07] xCoord: 174.010696
2013-04-20 00:35:43.516 Tank Turret Game[7815:c07] yCoord: 174.010696
2013-04-20 00:35:43.549 Tank Turret Game[7815:c07] xCoord: 174.679245
2013-04-20 00:35:43.550 Tank Turret Game[7815:c07] yCoord: 174.679245
2013-04-20 00:35:43.583 Tank Turret Game[7815:c07] xCoord: 175.349762
2013-04-20 00:35:43.584 Tank Turret Game[7815:c07] yCoord: 175.349762
2013-04-20 00:35:43.616 Tank Turret Game[7815:c07] xCoord: 176.020950
2013-04-20 00:35:43.616 Tank Turret Game[7815:c07] yCoord: 176.020950
2013-04-20 00:35:43.649 Tank Turret Game[7815:c07] xCoord: 176.679245
2013-04-20 00:35:43.650 Tank Turret Game[7815:c07] yCoord: 176.679245
So far so good. But when I look at the screen, nothing is moving. I'm at wits end. I know that the solution to this is probably super easy, but for the life of me I can't find it. Any help will be greatly appreciated.