Objective c 基于UIImage的分片地图移动

Objective c 基于UIImage的分片地图移动,objective-c,Objective C,我有下面的代码,每次触摸屏幕时都会在屏幕上绘制一堆uiimage瓷砖(这是一个探索瓷砖地牢的角色)。但是如果我快速点击的话,动作会非常跳跃。为什么它行动迟缓 谢谢 - (void) updateView { // lets remove all subviews and start fresh for (UIView *subview in self.view.subviews) [subview removeFromSuperview]; [self drawVisibleDunge

我有下面的代码,每次触摸屏幕时都会在屏幕上绘制一堆uiimage瓷砖(这是一个探索瓷砖地牢的角色)。但是如果我快速点击的话,动作会非常跳跃。为什么它行动迟缓

谢谢

- (void) updateView {
// lets remove all subviews and start fresh
for (UIView *subview in self.view.subviews)
    [subview removeFromSuperview];

[self drawVisibleDungeon];
[self displayMonsters];

[self drawPlayer];
[self.view setNeedsDisplay];

[self.view addSubview:messageDisplay];
[self.view addSubview:miniMap];
[miniMap setNeedsDisplay];
}   

- (void) processTouchAtX: (int)x AndY: (int)y
{
int tempx = 0;
int tempy = 0;

if (x > 4)
    tempx++;
if (x < 4)
    tempx--;
if (y > 6)
    tempy++;
if (y < 6)
    tempy--;    

[[[self _accessHook] dungeonReference]processOneTurnWithX:tempx AndY:tempy];
[self updateView];
}

- (void) displayMonsters
{
UIImage *aImg;
int x, y;
NSString *aString;
int monCount = [[[_accessHook dungeonReference]aDungeonLevel]monsterCount];
NSMutableArray *monArray = [[[_accessHook dungeonReference]aDungeonLevel]mArray];
int player_x_loc = [[[self _accessHook] dungeonReference] thePlayer].x_location;
int player_y_loc = [[[self _accessHook] dungeonReference] thePlayer].y_location;

for (int i=0; i<monCount; i++)
{
    x = [[monArray objectAtIndex: i] x_loc];
    y = [[monArray objectAtIndex: i] y_loc];

    if ([self LOSFrom:CGPointMake(x, y)])
        if ((x >= player_x_loc-4) && (x < (player_x_loc+5)) && (y >= player_y_loc-6) && (y < (player_y_loc+6)))
        {
            UIImageView *imgView=[[UIImageView alloc]init];
            aString = [[monArray objectAtIndex:i]monsterPicture];
            aImg = [UIImage imageNamed:aString];
            imgView.frame=CGRectMake((x - (player_x_loc-4))*32, (y - (player_y_loc-6))*32, 32, 32);
            imgView.image = aImg;
            [self.view addSubview:imgView];
            [self.view bringSubviewToFront:imgView];
            [imgView release];
        }
}
}
-(无效)更新视图{
//让我们删除所有子视图并重新开始
for(UIView*self.view.subview中的子视图)
[子视图从SuperView移除];
[自绘可视地牢];
[自我展示怪物];
[自娱自乐];
[self.view setNeedsDisplay];
[self.view addSubview:messageDisplay];
[self.view addSubview:miniMap];
[小地图设置需要显示];
}   
-(void)processTouchAtX:(int)xandy:(int)y
{
int-tempx=0;
int-tempy=0;
如果(x>4)
tempx++;
if(x<4)
节拍--;
如果(y>6)
tempy++;
if(y<6)
坦比——;
[[self[u accessHook]dungeonReference]processOneTurnWithX:tempx安迪:tempy];
[自我更新视图];
}
-(空)显示怪物
{
UIImage*aImg;
int x,y;
NSString*aString;
int monCount=[[[u accessHook地下城参考]成人级别]怪物计数];
NSMUTABLEARRY*monArray=[[[u accessHook dungeonReference]成人级别]mArray];
int player_x_loc=[[self[u accessHook]dungeonReference]thePlayer].x_位置;
int player_y_loc=[[self[u accessHook]dungeonReference]thePlayer].y_位置;
对于(int i=0;i=player_x_loc-4)和&(x<(player_x_loc+5))&&(y>=player_y_loc-6)和&(y<(player_y_loc+6)))
{
UIImageView*imgView=[[UIImageView alloc]init];
aString=[[monArray objectAtIndex:i]monsterPicture];
aImg=[UIImage ImageName:aString];
imgView.frame=CGRectMake((x-(玩家x_loc-4))*32,(y-(玩家y_loc-6))*32,32;
imgView.image=aImg;
[self.view addSubview:imgView];
[self.view将子视图带到前面:imgView];
[imgView发布];
}
}
}

这很慢,因为将UIImage用于一组地图分幅会有很多开销。最好使用OpenGL和顶点缓冲区绘制所有内容。

您使用cocos2d的想法很好,它是一个非常好的框架,可能非常适合您可能面临的其他问题。但是为什么我们不讨论一下你为什么有这么多麻烦

您正在为每个磁贴使用单独的
UIImageView
UIImage
很好,在大多数情况下都非常有效。但是每一块瓷砖的全景都是相当昂贵的。更糟糕的是,每次你调用
updateView
,你都会拿走所有东西,然后扔掉。那非常非常昂贵

就像第一步一样,因为修复起来很容易,所以在开始时设置所有图像视图并将它们存储在一个数组中。然后在
displayMonsters
中,只需更改它们的位置和图像,而不是删除它们并创建新的。我想这本身就能显著提高你的表现

如果仍然需要更高的性能,可以切换到使用
CALayer
而不是
UIImageView
。您可以非常轻松地创建
CALayer
,并将其
内容设置为
aImg.CGImage
。同样,您需要添加一组带有
[view.layer addSublayer:layer]
CALayer
对象,就像添加视图一样。图层只是比视图便宜得多,因为它们不处理触摸事件之类的事情。他们只是画画


但是,如果您预计需要更高级的2D图形,那么Cocos2D没有问题。

我明白了。非常感谢。我试试。嗯,再想想,我宁愿拔指甲。我想我会试试Cocos2d,把背景瓷砖放在一层,其他瓷砖放在其他层,看看效果如何。你好。我只是想评论一下,我已经成功地将我的项目迁移到cocos2d,它现在工作得很好。非常感谢。