UIScrollView and Cocos2D

I created a UIScrollView in a cocos2d application. I add sprites dynamically, more than 3 pages. On the first page, the touch works fine on the sprite, however, if I use the scroll view and go to the second page, the touch does not work correctly ... the sprite will respond to the touch when I touch the screen, approximately the amount that I scroll to the left. If I go back to the first page, click "excellent" for the 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 create CocosOverlayViewController in my layer: -

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

I create a layer in my scene: -

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

I create 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 uses the TouchesDispatcher, and touches are allowed in the class.

If I use the cocos2d moveto function on the layer, I can touch the sprite and it responds so that I know that this works. When I use UIScrollView, the situation gets a little weird.

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

Charles

+5
source share
1 answer

I finally managed to implement CCLayer scrolling using the UIScrollView derived class, following the instructions mentioned in this question:

, UIScrollViews ( UIViews ) Cocos2D.

, , , : , UIScrollView CCLayer . , , UIScrollView, CCLayer , , , CCLayer (/ CCMenus) .

, Cocos2D , OpenGLView, CCNode CCMenu. 1.0 rc , OpenGLView (, UIScrollView), Cocos2D ( OpenGLView), UIScrollView -touchesBegan: method

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

( , UIScrollView Cocos2D, nextResponder: .)

Cocos2D:

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];
    ...

- UITouch locationInView: . UIView, . Cocos2D UIView: OpenGLView, OpenGLView (= touch view) . , , UIScrollView, "touch view" , OpenGLView.

+10

All Articles