1

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

.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;
        }
    }
4

1 回答 1

0

试试 CCScrollLayer 扩展。这是链接

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

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

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

更多细节参考 cocos2d-iphone-extensions 中的 CCScrollLayerTestLayer.m

于 2013-02-05T18:35:58.640 回答