如何顺利滚动CCLayer?

时间:2013-02-05 14:43:40

标签: cocos2d-iphone ccscrolllayer

我想触摸滚动我的地图。你能告诉我如何达到它的基本概念吗?这是我丑陋的简单代码。只需通过每次触摸移动来修改图层的位置。它有效,但它可能不可爱。

.h
CGPoint *touchBegin;
CGPoint *touchmove;

.m


    -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {


    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector]
                     convertToGL:touchLocation];
    touchBegin=new CGPoint(touchLocation);
}



  -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint touchLocation = [touch locationInView:[touch view]];
        touchLocation =
        [[CCDirector sharedDirector] convertToGL:touchLocation];
        touchmove = new CGPoint(touchLocation);
        [self setPosition:ccp(self.position.x/100-(touchBegin->x-touchmove->x),self.position.y/100.0f)];
        delete touchmove;
            touchmove=NULL;
}


-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        if (touchBegin)
        {
            delete touchBegin;
            touchBegin=NULL;
        }

        if (touchmove)
        {
            delete touchmove;
            touchmove=NULL;
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用CCScrollLayer Extension。 Here is link

«首先将两个文件添加到项目中

«在你的场景中导入CCScrollLayer.h

«在你的场景的init方法中构造每一层并将其传递给CCScrollLayer类

有关详细信息,请参阅cocos2d-iphone-extensions中的CCScrollLayerTestLayer.m