搜尋

首頁  >  問答  >  主體

objective-c - Cocos2d中如何實現zoom效果?

我現在用cocos2d引擎設計遊戲,並加載了全部的精靈(sprite),但是由於一部分精靈高度要大於320像素,所以很難將它們完全加載進來。為了方便,我打算實現ZOOM IN和ZOOM out效果,ZOOM IN可以瞬間讓全部精靈最小化,ZOOM out則會讓它們恢複最初的狀態。
怎麼樣才能創造出這種效果嗎?同時,希望大家也可以為我講一下有關pinch zoom的內容。

原問題:Applying Zoom Effect In cocos2D gaming environment?

迷茫迷茫2881 天前670

全部回覆(1)我來回復

  • 黄舟

    黄舟2017-04-22 09:01:19

    答:Michael Fredrickson
    (最佳答案)
    想實現zoom的效果很容易,只要設定遊戲的main layer的scale屬性即可,但還是有一些細節要注意。
    在縮放layer時,會整體提高layer的位置,如果在遊戲中想要實現滾動的效果,你需要做出以下設定:
    可以將layer中的anchorPoint設定成ccp(0.0f, 0.0f),然後算出layer上升的距離,再重新設定參數,讓它回復原狀。

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

    實作Pinch zoom很簡單,只要偵測touchesMoved,然後呼叫捲動路線(scaling routine)即可。

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

    答:Andrew
    如果所有的精靈都來自同一個parent,你可以將其進行縮放設置,協調不同parent,確保它們之間的關聯性。


    答:mohammad alabid
    以下程式碼可以將layer的位置縮放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
  • 取消回覆