放大CCLayerPanZoom Cocos Extensions的建议

时间:2012-05-27 18:35:31

标签: cocos2d-iphone

HY,

我已经开始开发RPG游戏了。我使用了一个Isometric tyle地图,我把它放在CCLayerPanZoom上。 现在我想在瓷砖地图上定位spites。当我点击时,我得到一个偏离的位置,用于新Sprite在Map上的位置。 为什么?层之间是否有滑动?有没有办法将地图附加到缩放图层,以便点击位置对应于我想要放置新精灵的地图上的位置。

请帮忙。

这是我的init() 对于那些想要看到它的人来说,只需看看包含Xcode项目的zip。这是一个Kobold2D项目。 我有相同的项目,但没有CCLayerPanZoom图层。在那里定位工作完美。 重要提示:当地图被拖拽时,左边缘完全位于IPAD模拟器左侧的边缘,因此瓷砖放置几乎可以。 TILES在适当的位置放置在合适的位置。

   - (id) init
   {
    if ((self = [super init])) 
    {
    //touches enables
    self.isTouchEnabled = YES;

    //instantiate the PanZoomLayer
    panZoomLayer = [CCLayerPanZoom node];
    //seting poperties for the pan zoom layer
    panZoomLayer.maxScale = 1.0f;
    panZoomLayer.minScale = 0.2f;
    panZoomLayer.maxSpeed = 100.0f;
    panZoomLayer.maxTouchDistanceToClick = 1000.0f;
    panZoomLayer.panBoundsRect = CGRectNull;
    panZoomLayer.mode = kCCLayerPanZoomModeSheet;


    //add the zoom layer to the CCLayer
    [self addChild: panZoomLayer z:-1];

    //if I uncoment this it thros me an error by ccTouchesEnded
    //It has to be commented like this
            //panZoomLayer.delegate = self; 

    self.map = [CCTMXTiledMap tiledMapWithTMXFile:@"isometric-no-offset.tmx"];

    _map.anchorPoint = ccp(0,0);
            _map.scale = CC_CONTENT_SCALE_FACTOR();

    layer = [_map layerNamed:@"Ground"];
    layer.visible = YES;

    [panZoomLayer addChild: _map 
                         z :-1 
                        tag:kTileMapTag];
            // create and initialize a Label
            CCLabelTTF *label = [CCLabelTTF labelWithString: @"Zooming and scroling Demo" 
                                           fontName: @"Marker Felt" 
                                           fontSize: 30];
            label.scale = 0.7f; //< to be visible on iPod Touch screen.
            label.color = ccBLUE;
            // add the label as a child to this Layer
            [panZoomLayer addChild: label 
                          z: 1 
                        tag:kLabelTag];

    _player = [CCSprite spriteWithFile:@"ninja.png"];


    //map size 
    mapHeight = _map.contentSize.height;
    mapWidth = _map.contentSize.width;

    //screen size
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    screenCenter = CGPointMake(screenSize.width / 2, screenSize.height / 2);


    //Hide/Show character
    [_player setVisible:YES];
    _player.position = ccp(300, 400);
    [panZoomLayer addChild:_player z:2];

    //dot initialization
    self.dot = [CCSprite spriteWithFile:@"d2.png"];
    //_dot. position = ccp(200,200);
    [_map addChild:dot z:3];

    panZoomLayer.mode = kCCLayerPanZoomModeSheet;
    panZoomLayer.minScale = 0.6f;
    panZoomLayer.maxScale = 2.0f;
    panZoomLayer.rubberEffectRatio = 1.1f;



    //test different scren reshapes
    //the zooming and scroling works also without any of the following methods


    //simple. until now the most convenient
    [self updateForScreenReshape1];

    //ruber. means center on screen. zoom out more in as previous
    //[self updateForScreenReshape2];

    //with zones. zoom out more and in less
    //[self updateForScreenReshape3];

}

return self;

} gBoy 小黑客

帖子:8 加入:2012年5月10日星期四下午7:26 已经感谢:0次 感谢:0次

1 个答案:

答案 0 :(得分:0)

所以问题是你触摸/点击屏幕放置一个精灵,精灵出现在一个随机的看似的地方,而不是你想要的地方?

我没有使用过Kobold2D(我主要使用vanilla iOS cocos2D),但听起来似乎是“层间滑动”。当您将_map添加为panZoomLayer的子项时,_map将继承panZoomLayer中发生的任何移动。

要正确地将精灵放在_map图层上,你必须计算从panZoomLayer到屏幕中心的位置差异,并使用该值来调整精灵的位置。

当您制作新的精灵时,您可以在ccTouch方法中执行与此相同的操作:

CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];

[newSprite setPosition: ccpSub(screenCenter, location)];

顺便说一下,当地图向左拖动时,位置似乎没问题,因为它的原点偏移距离是(或接近)0。