在cocos2D游戏环境中应用缩放效果?

时间:2011-05-07 04:48:17

标签: iphone objective-c cocos2d-iphone game-engine

我正在开发一款带有cocos2D游戏引擎的游戏,并在加载关卡的同时加载所有sprites,因为有些sprites(障碍物)高于320像素,因此似乎很难检查出来。因此,为了方便起见,我想应用ZOOM INZOOM out效果,这会立即最小化整个级别的所有精灵,而在缩小的情况下,这些将存在于旧位置。

我可以实现吗?

如果是,那怎么样?

请告诉捏缩放。

4 个答案:

答案 0 :(得分: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];
    }    
}

答案 1 :(得分:1)

如果所有的精灵都有相同的父级,你可以缩放它们的父级,然后用它们缩放它们,保持它们相对于父级的坐标。

答案 2 :(得分:0)

此代码将我的图层按比例缩放到特定位置

    [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);

答案 3 :(得分: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;
        }
    }
}