优化DrawRect:用于蛇类游戏

时间:2016-08-05 18:14:40

标签: ios iphone optimization drawrect

我正在尝试制作一种使用蛇类的游戏。到目前为止,我得到它的工作,它很好地工作,但经过一段时间它有点迟钝,特别是在Iphone 5及更低。

我知道这是因为笔直:

-(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];
}

请记住:

    每隔0.05秒调用
  1. drawrect来重绘两条蛇
  2. 有服务器,第一部iphone和客户端,第二部iphone(多路人)
  3. 所以现在,请告诉我,如果我没有遗漏代码行,或者有不必要的行。 还有其他方法来画蛇吗?我希望蛇能够“闪耀”(每次抽奖都会改变颜色),也许会“增长/缩小”(改变溺水点的大小。

1 个答案:

答案 0 :(得分:0)

我想出了如何使用Paths,它非常有效:

    -(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);
}

现在的问题是我是否可以增加游戏循环计时器。我正在使用Multipeer Connectivity每隔0.05秒从一个iPhone发送位置到另一个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更快地发送位置吗? 其他建议?

感谢您的所有答案!

相关问题