Hello guys,
I'm starting to work on my first game optimization. Before I added the feature I'm gonna post about, I had 30fps all the time. Now I can't take further than 20 on device.
Picture of the game before I explain to you:
http://postimg.org/image/8jwazzkit/
The game consists on a lot of targets coming on screen and the player must collide with them. Very simple and old mechanic. The speed changes within time.
There is like a "tail" following the player movements, making drawings like if it was a electrocardiogram. Every time the player hits the screen, it moves and I add a new point to the drawing. If the point reaches a certain point, I delete it, so I keep my points number not so big. It consists in usually 8 points maximun on screen.
Lets go to the code, which is called by enterframe event
///the extreme left value is -Sparrow.stage.width/4, just to let you guys know.
-(void)registerPoint:(int)atX :(int)atY :(int)withSpeed
{
///Player has moved, register new point
PlayerTailPoint * point = [[PlayerTailPoint alloc]init];
point.x = atX;
point.y = atY;
point.speed = withSpeed;
[_points addObject:point];
}
///this method is called by enterframe event
-(void)onSync :(SPEvent *)event
{
[self draw];
}
-(void)draw
{
NSMutableArray * points = [[NSMutableArray alloc]init];
int gameSpeed = _player.speed;
int index = -1;
///create first point, the further left that will always exist
PlayerTailPoint * leftPoint = [[PlayerTailPoint alloc]init];
leftPoint.x = _extremeLeft;
leftPoint.y = Sparrow.stage.height/2;
leftPoint.speed = gameSpeed;
[points addObject:leftPoint];
///create intermediary points and remove if has reached the extremeLeft
for (PlayerTailPoint *point in _points) {
if (point.x < _extremeLeft) {
index = [_points indexOfObject:point];
continue;
}
point.x -= gameSpeed/20;
[points addObject:point];
}
if (index != -1) {
[_points removeObjectAtIndex:index];
}
///create last point, where the player is
PlayerTailPoint * rightPoint = [[PlayerTailPoint alloc]init];
rightPoint.x = _player.x;
rightPoint.y = _player.y;
rightPoint.speed = gameSpeed;
[points addObject:rightPoint];
///draw line
_customTexture = [[SPTexture alloc]
initWithWidth:Sparrow.stage.width/2 height:Sparrow.stage.height
draw:^(CGContextRef context)
{
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, _extremeLeft,Sparrow.stage.height/2); //start at this point
for (PlayerTailPoint *point in points) {
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextStrokePath(context);
}];
////show texture
if(!_lineContainer){
_lineContainer = [[SPMovieClip alloc] initWithTexture:_customTexture];
_lineContainer.alpha = 0.2;
[self addChild:_lineContainer];
}else{
_lineContainer.texture = _customTexture;
}
}
Do you guys have any hints of how could I optmize the drawing of this "tail"?
Thank you so much in advance.
All the best,
Epa