<tfoot id='OCVk2'></tfoot>

<i id='OCVk2'><tr id='OCVk2'><dt id='OCVk2'><q id='OCVk2'><span id='OCVk2'><b id='OCVk2'><form id='OCVk2'><ins id='OCVk2'></ins><ul id='OCVk2'></ul><sub id='OCVk2'></sub></form><legend id='OCVk2'></legend><bdo id='OCVk2'><pre id='OCVk2'><center id='OCVk2'></center></pre></bdo></b><th id='OCVk2'></th></span></q></dt></tr></i><div id='OCVk2'><tfoot id='OCVk2'></tfoot><dl id='OCVk2'><fieldset id='OCVk2'></fieldset></dl></div>
<legend id='OCVk2'><style id='OCVk2'><dir id='OCVk2'><q id='OCVk2'></q></dir></style></legend>
      <bdo id='OCVk2'></bdo><ul id='OCVk2'></ul>

    <small id='OCVk2'></small><noframes id='OCVk2'>

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

      时间:2024-08-12

        <small id='KrZT9'></small><noframes id='KrZT9'>

            <bdo id='KrZT9'></bdo><ul id='KrZT9'></ul>
            <tfoot id='KrZT9'></tfoot>

          • <legend id='KrZT9'><style id='KrZT9'><dir id='KrZT9'><q id='KrZT9'></q></dir></style></legend>
                <tbody id='KrZT9'></tbody>
            • <i id='KrZT9'><tr id='KrZT9'><dt id='KrZT9'><q id='KrZT9'><span id='KrZT9'><b id='KrZT9'><form id='KrZT9'><ins id='KrZT9'></ins><ul id='KrZT9'></ul><sub id='KrZT9'></sub></form><legend id='KrZT9'></legend><bdo id='KrZT9'><pre id='KrZT9'><center id='KrZT9'></center></pre></bdo></b><th id='KrZT9'></th></span></q></dt></tr></i><div id='KrZT9'><tfoot id='KrZT9'></tfoot><dl id='KrZT9'><fieldset id='KrZT9'></fieldset></dl></div>

                本文介绍了在 cocos2D 游戏环境中应用缩放效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在开发一个带有 cocos2D 游戏引擎的游戏,并在加载关卡时加载所有 sprites,现在因为一些 sprites(障碍物)高于 320 像素,因此似乎很难检查出来.因此,为了方便起见,我想应用 ZOOM INZOOM out 效果,它可以一次最小化整个关卡的所有精灵,并且在缩小的情况下,这些精灵将驻留在旧的位置.

                I'm working on a game with cocos2D game engine and make load all the sprites while it load the level, now as because some of sprites (obstacles) are taller than 320 pixel, thus it seems difficult to check them out. So for the convenience sake I want to apply ZOOM IN and ZOOM out effect, which minimizes entire level's all sprites at once, and in zoom out case these will resided to there old position.

                我能做到吗?

                如果是,那怎么办?

                也请说说捏缩放.

                推荐答案

                缩放,相当简单,只需设置主游戏层的 scale 属性...但有一些问题.

                Zooming, is fairly simple, simply set the scale property of your main game layer... but there are a few catches.

                当你缩放图层时,它会改变图层的位置.它不会自动缩放到您当前正在查看的内容的中心.如果您的游戏中有任何类型的滚动,您需要考虑这一点.

                When you scale the layer, it will shift the position of the layer. It won't automatically zoom towards the center of what you're currently looking at. If you have any type of scrolling in your game, you'll need to account for this.

                为此,请将图层的 anchorPoint 设置为 ccp(0.0f, 0.0f),然后计算图层移动了多少,并重新定位它相应地.

                To do this, set the anchorPoint of your layer to ccp(0.0f, 0.0f), and then calculate how much your layer has shifted, and reposition it accordingly.

                - (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,然后调用您的缩放例程.

                Pinch zoom is easier... just detect the touchesMoved, and then call your 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];
                    }    
                }
                

                这篇关于在 cocos2D 游戏环境中应用缩放效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:什么是“陷阱"?在开发 iPhone 游戏时? 下一篇:是否可以实现 MAX(As,Ad) openGL 混合?

                相关文章

                1. <i id='1gEbu'><tr id='1gEbu'><dt id='1gEbu'><q id='1gEbu'><span id='1gEbu'><b id='1gEbu'><form id='1gEbu'><ins id='1gEbu'></ins><ul id='1gEbu'></ul><sub id='1gEbu'></sub></form><legend id='1gEbu'></legend><bdo id='1gEbu'><pre id='1gEbu'><center id='1gEbu'></center></pre></bdo></b><th id='1gEbu'></th></span></q></dt></tr></i><div id='1gEbu'><tfoot id='1gEbu'></tfoot><dl id='1gEbu'><fieldset id='1gEbu'></fieldset></dl></div>
                2. <tfoot id='1gEbu'></tfoot>
                    <bdo id='1gEbu'></bdo><ul id='1gEbu'></ul>
                3. <legend id='1gEbu'><style id='1gEbu'><dir id='1gEbu'><q id='1gEbu'></q></dir></style></legend>

                  <small id='1gEbu'></small><noframes id='1gEbu'>