Hi, I'm hoping for some help in coding strategy here. At the heart, my current problem has to do with low frame rates. However, even at 15-20fps, people seem to be telling me that the game play isn't really compromised, so I am focusing on other issues which are definitely problematic. I have a sprite (a sponge) which gets touched to activate it (squeeze). It then essentially auto-fires water (creating other sprites) until the touch is let go. My problem is that I am creating the new sprite in my step loop, and I want it to be able to create these new sprites quicker as my "auto-fire" is supposed to look like water as opposed to a bunch of little particles (I want to minimize the spaces between the sprites as they are fired). Right now during a touch, my code creates one sprite on each iteration of step:. Obviously, I could create several sprites on each iteration, even checking delta-time to take an intelligent guess at how many to create on each iteration. Problem is that there is always a noticeable delay in between the iterations, breaking up my water stream. I want to spawn a thread or a timer (looking for suggestions) somewhere so that this new sprite code is activated on ccTouchesBegan and deactivated on ccTouchesEnded.
Here's what I have now:
In my GameLayer class:
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
...
[self squeezeSponge: location start:YES]; // See if any sponges were touched and process accordingly
}
-(BOOL) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
...
// if touch up on sponge squeeze, stop squeezing
if ([self squeezeSponge:touchLocations[i] start:NO])
; // squeezeSponge already handled stop if it returned YES
else if (spongeCount > 0) // If more sponges, drop one
[self addNewSpongeAt:location];
...
}
-(BOOL) squeezeSponge:(CGPoint)point start:(BOOL)start
{
for (int i = 0; i < spongesUsed; i++)
if (cpPolyShapeContainsVert((cpPolyShape *)sponges[i].pShape, (cpVect)point))
{
sponges[i].isSqueezing = start;
return YES;
}
return NO;
}
-(void) step:(ccTime)delta
{
...
// Process sponge actions
for (int i = 0; i < spongesUsed; i++)
{
if (sponges[i].isSqueezing && sponges[i].volume > 0)
{
// Release water in sponge, if any
[sponges[i] squeeze];
[sponges[i] zoom:YES];
}
else
{
// unzoom when sponge is not squeezing (or just went empty)
[sponges[i] zoom:NO];
sponges[i].isSqueezing = NO;
}
}
In my emitted sprite class:
-(WaterSprite *) squeeze
{
if (volume <= 0)
{
isSqueezing = NO; // Make sure to unzoom sponge once it's empty!
return nil; // Nothing released
}
volume--; // Count less water volume in sponge
WaterSprite *sprite = [[WaterSprite alloc] volume:1 randomPositionDelta:NO];
return sprite;
}
My aim is to remove the call to [sponges[i] squeeze] from step: and replace it with some asynchronous mechanism so that squeeze can be invoked more often.
Thanks in advance!!
Scott