2011-05-07 41 views
20

我正在與cocos2D遊戲引擎一起製作遊戲,並在其加載水平時加載所有sprites,現在因爲sprites(障礙物)的某些高於320像素,因此似乎很難檢查出它們。因此,爲了方便起見,我想要應用ZOOM INZOOM out效果,該效果可以一次最小化整個等級的所有精靈,而在縮小情況下,這些效果將駐留在舊的位置。應用變焦效果在cocos2D遊戲環境中?

我可以做到這一點嗎?

如果是,那麼如何?

請介紹一下縮放也。

回答

27

縮放,非常簡單,只需設置主遊戲圖層的縮放屬性...但有一些捕獲。

縮放圖層時,它會移動圖層的位置。它不會自動縮放到目前正在查看的中心位置。如果您的遊戲中有任何類型的滾動,則需要對此進行說明。

爲此,請將圖層的anchorPoint設置爲ccp(0.0f, 0.0f),然後計算圖層的移動量,並相應地重新定位圖層。

- (void) scale:(CGFloat) newScale scaleCenter:(CGPoint) scaleCenter { 
    // scaleCenter is the point to zoom to.. 
    // If you are doing a pinch zoom, this should be the center of your pinch. 

    // Get the original center point. 
    CGPoint oldCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale); 

    // Set the scale. 
    yourLayer.scale = newScale; 

    // Get the new center point. 
    CGPoint newCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale); 

    // Then calculate the delta. 
    CGPoint centerPointDelta = ccpSub(oldCenterPoint, newCenterPoint); 

    // Now adjust your layer by the delta. 
    yourLayer.position = ccpAdd(yourLayer.position, centerPointDelta); 
} 

捏縮放比較容易...只是檢測touchesMoved,然後調用你的縮放例程。

- (void) ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 

    // Examine allTouches instead of just touches. Touches tracks only the touch that is currently moving... 
    // But stationary touches still trigger a multi-touch gesture. 
    NSArray* allTouches = [[event allTouches] allObjects]; 

    if ([allTouches count] == 2) {    
     // Get two of the touches to handle the zoom 
     UITouch* touchOne = [allTouches objectAtIndex:0]; 
     UITouch* touchTwo = [allTouches objectAtIndex:1]; 

     // Get the touches and previous touches. 
     CGPoint touchLocationOne = [touchOne locationInView: [touchOne view]]; 
     CGPoint touchLocationTwo = [touchTwo locationInView: [touchTwo view]]; 

     CGPoint previousLocationOne = [touchOne previousLocationInView: [touchOne view]]; 
     CGPoint previousLocationTwo = [touchTwo previousLocationInView: [touchTwo view]]; 

     // Get the distance for the current and previous touches. 
     CGFloat currentDistance = sqrt(
             pow(touchLocationOne.x - touchLocationTwo.x, 2.0f) + 
             pow(touchLocationOne.y - touchLocationTwo.y, 2.0f)); 

     CGFloat previousDistance = sqrt(
             pow(previousLocationOne.x - previousLocationTwo.x, 2.0f) + 
             pow(previousLocationOne.y - previousLocationTwo.y, 2.0f)); 

     // Get the delta of the distances. 
     CGFloat distanceDelta = currentDistance - previousDistance; 

     // Next, position the camera to the middle of the pinch. 
     // Get the middle position of the pinch. 
     CGPoint pinchCenter = ccpMidpoint(touchLocationOne, touchLocationTwo); 

     // Then, convert the screen position to node space... use your game layer to do this. 
     pinchCenter = [yourLayer convertToNodeSpace:pinchCenter]; 

     // Finally, call the scale method to scale by the distanceDelta, pass in the pinch center as well. 
     // Also, multiply the delta by PINCH_ZOOM_MULTIPLIER to slow down the scale speed.  
     // A PINCH_ZOOM_MULTIPLIER of 0.005f works for me, but experiment to find one that you like. 
     [self scale:yourlayer.scale - (distanceDelta * PINCH_ZOOM_MULTIPLIER) 
      scaleCenter:pinchCenter]; 
    }  
} 
+0

可能會聽起來很愚蠢,但我不知道如何計算[glc_ getModifiedScale],請求詳細闡述它。謝謝。 – rptwsthi 2011-05-18 07:16:49

+0

@rptwsthi對不起,那是我的錯。這是我的一些代碼,我沒有修復,以匹配其餘的。它現在應該更有意義。 – 2011-05-19 14:47:00

+0

你能否請進一步解釋你如何「將屏幕位置轉換爲節點空間」?我嘗試過使用'[self scale:(distanceDelta/PTM_RATIO)scaleCenter:pinchCenter];''還有'[self scale:distanceDelta scaleCenter:[self convertToNodeSpace:pinchCenter]];'但是他們兩個(以及兩個)每當我勉強捏住屏幕時都會狂暴。 – 2012-11-28 11:36:45

1

如果所有的精靈都有相同的父親,你可以縮放他們的父母,他們將與它一起縮放,保持他們相對於父母的座標。

+0

啊,但觸摸軌跡在這種情況下失去也就是說,如果縮小已經完成,然後觸摸一個精靈,我需要在精靈之後的50到100像素後觸摸。:( – rptwsthi 2011-05-18 07:19:53

0

這個代碼規模我的層由2至特定位置

[layer setScale:2]; 
    layer.position=ccp(240/2+40,160*1.5); 
    double dx=(touchLocation.x*2-240); 
    double dy=(touchLocation.y*2-160); 
    layer.position=ccp(inGamePlay.position.x-dx,inGamePlay.position.y-dy); 
0

我的代碼和它比其他的要好:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    NSArray* allTouches = [[event allTouches] allObjects]; 
    CCLayer *gameField = (CCLayer *)[self getChildByTag:TAG_GAMEFIELD]; 
    if (allTouches.count == 2) { 

     UIView *v = [[CCDirector sharedDirector] view]; 
     UITouch *tOne = [allTouches objectAtIndex:0]; 
     UITouch *tTwo = [allTouches objectAtIndex:1]; 
     CGPoint firstTouch = [tOne locationInView:v]; 
     CGPoint secondTouch = [tTwo locationInView:v]; 
     CGPoint oldFirstTouch = [tOne previousLocationInView:v]; 
     CGPoint oldSecondTouch = [tTwo previousLocationInView:v]; 
     float oldPinchDistance = ccpDistance(oldFirstTouch, oldSecondTouch); 
     float newPinchDistance = ccpDistance(firstTouch, secondTouch); 

     float distanceDelta = newPinchDistance - oldPinchDistance; 
     NSLog(@"%f", distanceDelta); 
     CGPoint pinchCenter = ccpMidpoint(firstTouch, secondTouch); 
     pinchCenter = [gameField convertToNodeSpace:pinchCenter]; 

     gameField.scale = gameField.scale - distanceDelta/100; 
     if (gameField.scale < 0) { 

      gameField.scale = 0; 
     } 
    } 
} 
+0

和我的代碼有什麼問題?在這個頁面上沒有可行和完整的代碼,除了我的代碼。 – user2083364 2013-07-01 07:25:31

+0

奇妙的作品,非常感謝。我一直在苦苦掙扎一段時間! +1 – stenger96 2013-12-10 03:33:03