objective-c - Cocos2d中如何实现zoom效果?
迷茫
迷茫 2017-04-22 08:59:19
0
1
631

我现在用cocos2d引擎设计游戏,并加载了全部的精灵(sprite),但是由于一部分精灵高度要大于320像素,所以很难将它们完全加载进来。为了方便,我打算实现ZOOM IN和ZOOM out效果,ZOOM IN可以瞬间让全部精灵最小化,ZOOM out则会让它们恢复最初的状态。
怎么样才能创造出这种效果吗?同时,希望大家也可以为我讲一下有关pinch zoom的内容。

原问题:Applying Zoom Effect In cocos2D gaming environment?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

membalas semua(1)
黄舟

Jawapan: Michael Fredrickson
(Jawapan terbaik)
Sangat mudah untuk mencapai kesan zum, cuma tetapkan atribut skala lapisan utama permainan, tetapi masih terdapat beberapa butiran yang perlu diberi perhatian.
Apabila menskala lapisan, kedudukan lapisan akan dinaikkan secara keseluruhan Jika anda ingin mencapai kesan tatal dalam permainan, anda perlu membuat tetapan berikut:
Anda boleh menetapkan anchorPoint dalam lapisan kepada ccp(0.0f, 0.0f), kemudian mengira jarak lapisan meningkat, dan kemudian menetapkan semula parameter untuk mengembalikannya kepada keadaan asalnya.

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

Sangat mudah untuk melaksanakan zum Pinch, hanya mengesan touchesMoved dan kemudian memanggil rutin penskalaan.

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

Jawapan: Andrew
Jika semua bunian berasal daripada ibu bapa yang sama, anda boleh menskalakan mereka untuk menyelaraskan ibu bapa yang berbeza dan memastikan perkaitan antara mereka.


Jawapan: mohammad alabid
Kod berikut boleh menskalakan kedudukan lapisan sebanyak 2 kali,

[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);
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!