簡體   English   中英

在帶有透明背景的Cocos2D中的iOS 7中放慢速度

[英]Slow down in iOS 7 in Cocos2D with transparent background

我已經在Cocos2D中開發了一個游戲約3年,該游戲利用透明背景顯示UIView。 這樣做的原因是使視差背景仍然像Cocos2D進行場景轉換一樣運行。

更新到iOS 7時,我遇到了一個新問題。這些情況的結合會導致速度變慢:

-僅當視差背景的框位置已更改時。

-如果我消滅了一個會發出小精靈和粒子效果的敵人。

因此,這是這兩件事的結合,並且只會偶爾發生。 當速度變慢時,幀速率的調試值不會下降。 如果加載新場景,它將恢復正常。 有時,當我消滅另一個敵人時,減速也消失了。

我的視差UIView中有代碼,幾乎可以在游戲中的每個幀上運行。 我將問題歸納為一行:

-(void)updateImagePosWithPos:(CGPoint)pos{ // in game

    // create vel based on last currentPos minus new pos

    CGPoint vel = CGPointMake(currentPos.x-pos.x, currentPos.y-pos.y);

    // init variables tmpVel and tempTotalImages

    CGPoint tmpVel = CGPointZero;

    int tmpTotalImages = 0;

    // create indexLayerArr

    NSMutableArray *indexLayerArr = [NSMutableArray array];

    // for every parallax layer, add the number of images horizontally minus 1 to indexLayerArr

    for (int j=0; j<totalLayers; ++j){

        [indexLayerArr addObject:[NSNumber numberWithInt:[[totalImagesArr objectAtIndex:j] intValue]-1]];

    }

    int i = 0;


    for (UIImageView *imageView in self.subviews) {

        CGRect tmpRect = CGRectZero;

        NSMutableArray *tmpRectArr = [rectContainer objectAtIndex:imageView.tag];

        float speed = 0.00;


        tmpTotalImages = [[totalImagesArr objectAtIndex:imageView.tag] intValue];

        speed = [[speedArr objectAtIndex:imageView.tag] floatValue];

        tmpVel = CGPointMake(vel.x*speed, vel.y*speed);

        i = [[indexLayerArr objectAtIndex:imageView.tag] intValue];

        tmpRect = [[tmpRectArr objectAtIndex:i] CGRectValue];


        if(tmpRect.origin.x - tmpVel.x > wins.width){

            tmpRect.origin.x -= (tmpTotalImages)*tmpRect.size.width;

        }
        else if(tmpRect.origin.x - tmpVel.x < -tmpRect.size.width){

            tmpRect.origin.x += (tmpTotalImages)*tmpRect.size.width;

        }

        tmpRect.origin.x -= tmpVel.x;
        tmpRect.origin.y += tmpVel.y;

        [tmpRectArr replaceObjectAtIndex:i withObject:[NSValue valueWithCGRect:tmpRect]];


        imageView.frame = [[tmpRectArr objectAtIndex:i] CGRectValue]; // <-- slow down cause



        i--;

        [indexLayerArr replaceObjectAtIndex:imageView.tag withObject:[NSNumber numberWithInt:i]];


    }

    currentPos = CGPointMake(pos.x, pos.y);

}

參見注釋行imageView.frame = [[tmpRectArr objectAtIndex:i] CGRectValue];

因此,如果我對此進行評論,該問題將永遠不會發生。 如果我堅持這一點,並且不更改tempRect的值,那么該問題也不會發生。

似乎在iOS 7中更改UIImageView的框架位置存在問題,但僅在某些情況下。 只是想知道我還可以使用其他哪些選擇? 還是我在iOS 7中肯定做錯了什么?

不是解決問題的方法,而是解決方法。 我將從可能需要最多代碼更改的代碼開始。

  1. 您實際上不必具有UIView即可保持過渡的背景。 相反,如果您完全在cocos2d中實現背景(作為場景的一部分),則可以實現相同的效果,而不是替換場景而將圖層移入和移出。 場景過渡在大多數情況下都使用與節點上相同的動作。

  2. 使用cocos2d節點實現背景,並具有一個父節點作為背景節點的容器(即“層”)。 您可以使用該節點執行以下兩項操作之一:

    一種。 編輯CCDirectorIOS的代碼,並添加對您的后台節點的引用。 通過在[_runningScene visit]之前調用后台節點上的visit ,在drawScene方法中的所有其他節點之前更新該節點。

    過渡到新場景時,請從當前場景中刪除背景節點並將其添加到新場景,或者使用所有相同的設置創建背景副本並將其添加到新場景。 確保副本的開始狀態與原始狀態完全相同。 盡管由於其動畫的性質(例如,移動/翻轉/縮放),這不適用於大多數過渡。

  3. 如果在過渡運行時需要背景進行動畫處理,則有一個簡單的技巧。 計划對具有全局生存期的非CCNode對象(即AppDelegate)進行更新。 然后,將更新手動發送給所有在過渡期間(僅在過渡期間)應繼續更新其狀態的節點。

您可以像這樣在非節點對象上注冊更新:

[_director.scheduler scheduleUpdateForTarget:self priority:0 paused:NO];

即使在場景轉換期間也將調用此更新方法。 替代地,也應該有可能通過改變它們的paused狀態並因此恢復調度程序和動作來繼續更新節點,方法是要么重寫暫停的屬性,要么在發生過渡時通過顯式取消暫停特定的節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM