• <small id='bEneN'></small><noframes id='bEneN'>

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

      • <bdo id='bEneN'></bdo><ul id='bEneN'></ul>
      <tfoot id='bEneN'></tfoot>

      1. <legend id='bEneN'><style id='bEneN'><dir id='bEneN'><q id='bEneN'></q></dir></style></legend>

        UIScrollView 和 Cocos2D

        时间:2024-08-12

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

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

          • <bdo id='EAIUa'></bdo><ul id='EAIUa'></ul>
                1. 本文介绍了UIScrollView 和 Cocos2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have created a UIScrollView in a cocos2d application. I am adding sprites dynamically, over 3 pages. On the first page, touch works perfectly on a sprite, however if I use the scroll view and navigate to the second page, touch does not work quite right... the sprite will respond to the touch when I touch the screen, approximately the amount I have scrolled to the left. If I scroll back to the first page, touch works perfectly for a sprite. Any ideas? I am using the following tutorial: http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/ :)


                  I think some code might be useful:-

                  I am using the exact code from your demo...

                  CocosOverlayScrollView and CocosOverlayViewController

                  I am creating the CocosOverlayViewController in my layer:-

                  CocosOverlayViewController *scrollView = [CocosOverlayViewController alloc];
                  [[[Director sharedDirector] openGLView] addSubview:scrollView.view];
                  

                  I am creating the layer in my scene:-

                  Scene *scene = [Scene node];
                  GridLayer *layer = [GridLayer node];
                  [scene addChild: layer z:-1];
                  [scene setTag:12];
                  

                  I am creating the sprites in my layer:-

                      myImage.position = ccp(53 * (coordinate.x  + 0.52), 57 * (coordinate.y + 1.45));
                     [myImage runAction:[FadeIn actionWithDuration:0.3]];
                      myImage.relativeAnchorPoint = YES;
                     [self addChild:myImage z:-1];
                  

                  The sprite is using the TouchesDispatcher and the touches are resolved in the class.

                  If I use the cocos2d moveto function on the layer I can touch a sprite and it responds so I know it works, things just get a little odd when I use the UIScrollView.

                  I hope you understand my problem and can help, all the best :)

                  Carl

                  解决方案

                  I finally got to implementing scrolling of a CCLayer by using a UIScrollView derived class, following the tutorials mentioned in this question:

                  • http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/
                  • http://www.cocos2d-iphone.org/forum/topic/9417

                  Both of them are an excellent read, and highly recommended to get a deeper understanding about how UIScrollViews (and UIViews in general) can be made to handle touches in cooperation with Cocos2D.

                  However, upon implementing these solutions, I also experienced the bug described in the question: if you don't scroll, touches are propagated from the UIScrollView to the CCLayer correctly. If you scroll, the layer scrolls beautifully but non-scrolling touches on the UIScrollView propagate to the CCLayer with an offset that grows the more you scroll, which makes the CCLayer (and/or accompanying CCMenus) unusable.

                  I have found the cause of this problem to be a bug in how Cocos2D translates touches sent to the OpenGLView to the local CCNode or CCMenu coordinate systems. This bug is present in 1.0 rc, and only affects touches that are generated on a OpenGLView's subview (like our UIScrollView) and get propagated to the Cocos2D main touching area (namely the OpenGLView) by calling the following line inside the UIScrollView's -touchesBegan: method

                   [[[CCDirector sharedDirector] openGLView] touchesBegan:touches withEvent:event];
                  

                  (Note that calling this previous line is enough for propagating nonscrolling and nonzooming touches from the UIScrollView to Cocos2D, you do not need to call nextResponder: as the aforementioned blog posts do.)

                  The solution comes with a small modification on two Cocos2D sources:

                  CCNode.m

                  - (CGPoint)convertTouchToNodeSpace:(UITouch *)touch {
                      // cocos2d 1.0 rc bug when using with additional overlayed views (such as UIScrollView).
                      // CGPoint point = [touch locationInView: [touch view]];
                      CGPoint point = [touch locationInView: [[CCDirector sharedDirector] openGLView]];
                      point = [[CCDirector sharedDirector] convertToGL: point];
                      return [self convertToNodeSpace:point];
                  }
                  
                  - (CGPoint)convertTouchToNodeSpaceAR:(UITouch *)touch {
                      // cocos2d 1.0 rc bug when using with additional overlayed views (such as UIScrollView).
                      // CGPoint point = [touch locationInView: [touch view]];
                      CGPoint point = [touch locationInView: [[CCDirector sharedDirector] openGLView]];
                      point = [[CCDirector sharedDirector] convertToGL: point];
                      return [self convertToNodeSpaceAR:point];
                  }
                  

                  CCMenu.m

                  -(CCMenuItem *) itemForTouch: (UITouch *) touch {
                      // cocos2d 1.0 rc bug when using with additional overlayed views (such as UIScrollView).
                      // CGPoint touchLocation = [touch locationInView: [touch view]];
                      CGPoint touchLocation = [touch locationInView: [[CCDirector sharedDirector] openGLView]];
                      touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
                      ...
                  

                  The key here is UITouch's locationInView: method. The argument for this method should be the UIView that you want to translate the touches coordinates into. Most Cocos2D project only have one UIView: the OpenGLView, so touches get generated in the OpenGLView (= touch view) and get translated to the same view. However, if you add overlaying subviews to receive touches, such as a UIScrollView, 'touch view' will have this value, which no longer corresponds to the desired OpenGLView.

                  这篇关于UIScrollView 和 Cocos2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何用 Cocos2d-iPhone 画线 下一篇:UIActivityViewControllerCompletionHandler 怎么做?

                  相关文章

                  <legend id='lHyoP'><style id='lHyoP'><dir id='lHyoP'><q id='lHyoP'></q></dir></style></legend>
                    <bdo id='lHyoP'></bdo><ul id='lHyoP'></ul>

                2. <tfoot id='lHyoP'></tfoot>

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

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