2014-01-27 70 views
2

我有一個小問題。在本教程How To Make a Tile-Based Game with Cocos2D 2.X中使用了cocos2d V2.0,我想在cocos2d V3.0中做這個。所以,它不起作用!謝謝! (我不會說英文)「如何使用Cocos2D 2.X製作基於平鋪的遊戲」使用cocos2d V3製作本教程

我覺得在這一行的問題 - self.position = viewPoint;

@property (strong) CCTiledMap *tileMap; 
@property (strong) CCTiledMapLayer *background; 
@property (strong) CCSprite *player; 



- (id)init 
{ 
    // Apple recommend assigning self with supers return value 
    self = [super init]; 
    if (!self) return(nil); 

    // Enable touch handling on scene node 
    self.userInteractionEnabled = YES; 

    self.tileMap = [CCTiledMap tiledMapWithFile:@"TileMap.tmx"]; 
    self.background = [_tileMap layerNamed:@"Background"]; 
    [self addChild:_tileMap z:-1]; 

    CCTiledMapObjectGroup *objectGroup = [_tileMap objectGroupNamed:@"Objects"]; 
    NSAssert(objectGroup != nil, @"tile map has no objects object layer"); 

    NSDictionary *spawnPoint = [objectGroup objectNamed:@"SpawnPoint"]; 
    int x = [spawnPoint[@"x"] integerValue]; 
    int y = [spawnPoint[@"y"] integerValue]; 

    _player = [CCSprite spriteWithImageNamed:@"Player.png"]; 
    _player.position = ccp(x,y); 

    [self addChild:_player]; 
    [self setViewPointCenter:_player.position]; 

    // done 
    return self; 
} 


- (void)setViewPointCenter:(CGPoint) position { 

    CGSize winSize = [CCDirector sharedDirector].viewSize; 

    int x = MAX(position.x, winSize.width/2); 
    int y = MAX(position.y, winSize.height/2); 
    x = MIN(x, (_tileMap.mapSize.width * _tileMap.tileSize.width) - winSize.width/2); 
    y = MIN(y, (_tileMap.mapSize.height * _tileMap.tileSize.height) - winSize.height/2); 
    CGPoint actualPosition = ccp(x, y); 

    CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2); 
    CGPoint viewPoint = ccpSub(centerOfView, actualPosition); 

    self.position = viewPoint; 
} 

-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView:touch.view]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

    CGPoint playerPos = _player.position; 
    CGPoint diff = ccpSub(touchLocation, playerPos); 

    if (abs(diff.x) > abs(diff.y)) { 
     if (diff.x > 0) { 
      playerPos.x += _tileMap.tileSize.width; 
     } else { 
      playerPos.x -= _tileMap.tileSize.width; 
     } 
    } else { 
     if (diff.y > 0) { 
      playerPos.y += _tileMap.tileSize.height; 
     } else { 
      playerPos.y -= _tileMap.tileSize.height; 
     } 
    } 

    CCLOG(@"playerPos %@",CGPointCreateDictionaryRepresentation(playerPos)); 

    // safety check on the bounds of the map 
    if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) && 
     playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) && 
     playerPos.y >= 0 && 
     playerPos.x >= 0) 
    { 
     [self setPlayerPosition:playerPos]; 
    } 

    [self setViewPointCenter:_player.position]; 
    NSLog(@"%@", NSStringFromCGPoint(touchLocation)); 
} 

-(void)setPlayerPosition:(CGPoint)position { 
    _player.position = position; 
} 
+2

人,那就是* -touchesBegan:withEvent:方法*。 – EmptyStack

+0

你可以在cocos2d V3中製作這個[tutorial](http://www.raywenderlich.com/29458/how-to-make-a-tile-based-game-with-cocos2d-2-x)嗎? –

+0

對不起。我對Cocos2D不熟悉。可能是別人可以幫助你。謝謝。 – EmptyStack

回答

2

的問題是,用戶交互的面積是由缺省綁定到場景(屏幕尺寸)的contentSize

當調用您的setViewPointCenter方法時,您正在將場景位置移出觸摸事件未處理的區域。

必須擴展這方面的瓦片地圖這樣的contentSize的:

[self setContentSize:[_tileMap contentSize]]; 
self.userInteractionEnabled = YES; 
+0

非常感謝!它正在工作! –