COCOS2D:CCScene内部的ScrollView阻止动画

时间:2011-11-13 19:34:16

标签: iphone ios cocoa-touch cocos2d-iphone

我有一个CCScene。在它的右侧,我需要一个带有一些菜单元素的UIScrollView。我按照上一个问题Cocos2d and UIScrollView

中的解释这样做了

这是创建我的场景的方法

  +(id) scene: (int) wld{
    CCScene *scene = [CCScene node];
    LevelsMenu *layer = [LevelsMenu node];
    layer = [layer init:wld];
    [scene addChild: layer];
    [layer setScrollView:[LevelMenuControlView alloc]];
    [[[CCDirector sharedDirector] openGLView] addSubview:layer.scrollView.view];
    return scene;
 }

注意LevelMenuControlView只是一个以这种方式实现的UIViewController:

 - (void)loadView{
 LevelMenuView *scrollView = [[LevelMenuView alloc] initWithFrame:[UIScreen 
    mainScreen].applicationFrame];

scrollView.contentSize = CGSizeMake(862, 480);

scrollView.delegate = scrollView;
[scrollView setUserInteractionEnabled:TRUE];
[scrollView setScrollEnabled:TRUE];
[scrollView setShowsVerticalScrollIndicator:FALSE];
[scrollView setShowsHorizontalScrollIndicator:FALSE];
self.view = scrollView;

[scrollView release];
 }

LevelMenuView是包含菜单元素的UIScrollView

它工作得很好。现在的问题是,在场景的左侧我有一个精灵动画,如果我没有触摸屏幕,但是一旦我向上或向下拖动滚动视图停止或以我的滚动手指的相同速度!!!

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我找到了这个链接,其中某人已经遇到了同样的问题,并发布了一个实际可行的解决方案

http://www.cocos2d-iphone.org/forum/topic/11645

它基本上包括将此代码添加到滚动视图:

// This should go in your interface.
 NSTimer *timer;

// Override
- (void)setContentOffset:(CGPoint)contentOffset {
// UIScrollView uses UITrackingRunLoopMode.
// NSLog([[NSRunLoop currentRunLoop] currentMode]);

// If we're dragging, mainLoop is going to freeze.
if (self.dragging && !self.decelerating) {

// Make sure we haven't already created our timer.
if (timer == nil) {

 // Schedule a new UITrackingRunLoopModes timer, to fill in for    
 CCDirector while we drag.
        timer = [NSTimer scheduledTimerWithTimeInterval:[[CCDirector sharedDirector] animationInterval] target:self selector:@selector(animateWhileDragging) userInfo:nil repeats:YES];

                    // This could also be NSRunLoopCommonModes
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopModes];
    }
}

// If we're decelerating, mainLoop is going to stutter.
if (self.decelerating && !self.dragging) {

    // Make sure we haven't already created our timer.
    if (timer == nil) {

        // Schedule a new UITrackingRunLoopMode timer, to fill in for CCDirector while we decellerate.
        timer = [NSTimer scheduledTimerWithTimeInterval:[[CCDirector sharedDirector] animationInterval] target:self selector:@selector(animateWhileDecellerating) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
    }
}

[super setContentOffset:contentOffset];
}
- (void)animateWhileDragging {

// Draw.
[[CCDirector sharedDirector] drawScene];

if (!self.dragging) {

    // Don't need this timer anymore.
    [timer invalidate];
    timer = nil;
}
}
- (void)animateWhileDecellerating {

// Draw.
[[CCDirector sharedDirector] drawScene];

if (!self.decelerating) {

    // Don't need this timer anymore.
    [timer invalidate];
    timer = nil;
}
}

非常感谢这些家伙