2016-08-05 38 views
0

我正在尝试创建一种使用蛇的游戏。到目前为止,我得到了它的工作,它很好地工作,但过了一段时间它会有点滞后,特别是在iPhone 5和更低。优化DrawRect:对于Snake排序的游戏

我知道这是因为drawRect中的:

-(void) drawRect:(CGRect)rect{ 
    NSDate *start = [NSDate date]; 
    CGFloat rr, rg, rb, ra; 
    [redcolorr getRed:&rr green:&rg blue:&rb alpha:&ra]; 
    CGFloat gr, gg, gb, ga; 
    [greencolorr getRed:&gr green:&gg blue:&gb alpha:&ga]; 
    CGFloat br, bg, bb, ba; 
    [bluecolorr getRed:&br green:&bg blue:&bb alpha:&ba]; 

    if ([myName isEqual:@"Server"]) { 
     for (int i= 0; i<ServerBody.count; i++) { 
      Snake *a; 
      a = [ServerBody objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, rr, rg, rb, ra); 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x,a.y,7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 

    } 
    else{ 

     for (int i= 0; i<IntelligentServerBody.count; i++) { 
      Snake *a; 
      a = [IntelligentServerBody objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, rr, rg, rb, ra); 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 
    } 
    if ([myName isEqual:@"Client0"]) { 
     for (int i= 0; i<Client0Body.count; i++) { 
      Snake *a; 
      a = [Client0Body objectAtIndex:i]; 
      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 
    } 
    else{ 

     for (int i= 0; i<IntelligentClient0Body.count; i++) { 
      Snake *a; 
      a = [IntelligentClient0Body objectAtIndex:i]; 

      CGContextRef ctx = UIGraphicsGetCurrentContext(); 
      UIGraphicsPushContext(ctx); 
      CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color 
      CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee)); 
      UIGraphicsPopContext(); 
     } 

    } 
    NSTimeInterval timeInterval = [start timeIntervalSinceNow]; 
} 

记住:

  1. 的drawRect被称为每0.05秒,以重绘两条蛇
  2. 有一台服务器,第一代iPhone,和一个客户端,第二个iphone(multipeer)

所以现在,请告诉我,如果我不是米发行代码行,或者有不必要的行。 有没有其他的方式来绘制蛇?我想让蛇“发光”(改变每个画的颜色),也许“增长/缩小”(改变溺水点的大小)。

+0

那么'UIGraphicsPush/Pop'调用对于一个是多余的......尽管'drawRect:'不利用GPU,但经常调用它时总会有点迟缓。您可能应该考虑尝试使用[Core Animation](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html)或[ Sprite Kit](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html),因为两者都利用GPU,所以效率更高。 – Hamish

+0

是的,你并不是真的想要使用这样的重复绘制调用,你正在每个刷新间隔创建一个图形上下文。例如,您可以通过为每个蛇创建小“片段”,然后使用SpriteKit通过片段组合来渲染它们,从而获得更好的结果。这样,每个细分都是以最佳方式渲染的GPU纹理。您需要编写代码来使用X,Y坐标和角度连接段,而不是使用此绘制逻辑,但需要进行一些重构,但这样可以更好地使用图形子系统。 – MoDJ

回答

0

我想出了如何使用路径,它是超级有效:

-(void) drawRect:(CGRect)rect{  
     a = [ServerBody objectAtIndex:0]; 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGContextSetLineWidth(ctx, 7.5*k*sizee); 
     CGContextMoveToPoint(ctx, a.x, a.y); 
     CGContextSetStrokeColorWithColor(ctx, redcolorr.CGColor); 
     for (int i= 0; i<ServerBody.count; i++) { 
      a = [ServerBody objectAtIndex:i]; 
      CGContextAddLineToPoint(ctx,a.x, a.y); 
     } 
     CGContextStrokePath(ctx); 
} 

现在的问题是,如果我能增加gameloop计时器我使用Multipeer连接发送位置每0.05秒内从一个iPhone到另一个,但即时通讯发送此:

NSMutableArray *Messagee = [[NSMutableArray alloc] initWithCapacity:10]; 
     [Messagee addObject:x]; 
     [Messagee addObject:y]; 
     [Messagee addObject:ang]; 
     [Messagee addObject:ind]; 


     NSData* Message = [NSKeyedArchiver archivedDataWithRootObject:Messagee]; 
     [self.partyTime sendData:Message 
         withMode:MCSessionSendDataUnreliable 
          error:nil]; 

不幸的是,其他iPhone需要一些时间才能收到。 我的数据包太大了吗? 我应该使用Streams更快发送位置吗? 其他建议?

感谢您的所有答案!