<legend id='2vqpX'><style id='2vqpX'><dir id='2vqpX'><q id='2vqpX'></q></dir></style></legend>

  1. <small id='2vqpX'></small><noframes id='2vqpX'>

      <bdo id='2vqpX'></bdo><ul id='2vqpX'></ul>

    <tfoot id='2vqpX'></tfoot>
    <i id='2vqpX'><tr id='2vqpX'><dt id='2vqpX'><q id='2vqpX'><span id='2vqpX'><b id='2vqpX'><form id='2vqpX'><ins id='2vqpX'></ins><ul id='2vqpX'></ul><sub id='2vqpX'></sub></form><legend id='2vqpX'></legend><bdo id='2vqpX'><pre id='2vqpX'><center id='2vqpX'></center></pre></bdo></b><th id='2vqpX'></th></span></q></dt></tr></i><div id='2vqpX'><tfoot id='2vqpX'></tfoot><dl id='2vqpX'><fieldset id='2vqpX'></fieldset></dl></div>

      《如何使用 Cocos2D 2.X 制作基于 Tile 的游戏》使用 cocos2d V3 制作本教程

      时间:2024-08-12

        • <tfoot id='8TcrC'></tfoot>

            • <bdo id='8TcrC'></bdo><ul id='8TcrC'></ul>
                <tbody id='8TcrC'></tbody>
              • <i id='8TcrC'><tr id='8TcrC'><dt id='8TcrC'><q id='8TcrC'><span id='8TcrC'><b id='8TcrC'><form id='8TcrC'><ins id='8TcrC'></ins><ul id='8TcrC'></ul><sub id='8TcrC'></sub></form><legend id='8TcrC'></legend><bdo id='8TcrC'><pre id='8TcrC'><center id='8TcrC'></center></pre></bdo></b><th id='8TcrC'></th></span></q></dt></tr></i><div id='8TcrC'><tfoot id='8TcrC'></tfoot><dl id='8TcrC'><fieldset id='8TcrC'></fieldset></dl></div>

                <small id='8TcrC'></small><noframes id='8TcrC'>

              • <legend id='8TcrC'><style id='8TcrC'><dir id='8TcrC'><q id='8TcrC'></q></dir></style></legend>
                本文介绍了《如何使用 Cocos2D 2.X 制作基于 Tile 的游戏》使用 cocos2d V3 制作本教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个小问题.在本教程中 如何使用 Cocos2D 2.X 制作基于 Tile 的游戏,使用 cocos2d V2.0,我想在 cocos2d V3.0 中制作.所以,它不起作用!谢谢!(我不会说英语)

                I have a small problem. In this tutorial How To Make a Tile-Based Game with Cocos2D 2.X used cocos2d V2.0, I wanna make this in cocos2d V3.0. So, it doesn't work! Thanks! (I don't speak english)

                我认为这行有问题 - self.position = viewPoint;

                I think problem in this line - 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;
                }
                

                推荐答案

                问题是用户交互的区域默认绑定到场景的contentSize(屏幕大小).

                The problem is that the area of the user interaction is by default bound to the contentSize of the scene (screen size).

                当调用 setViewPointCenter 方法时,您将场景位置移出未处理触摸事件的区域.

                When calling your setViewPointCenter method, you are moving the scene position out of this area where the touch event is not handled.

                你必须像这样扩展瓦片地图的 contentSize 的这个区域:

                You have to extend this area of the contentSize of the tile map like that :

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

                这篇关于《如何使用 Cocos2D 2.X 制作基于 Tile 的游戏》使用 cocos2d V3 制作本教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 iphone 中解析推送通知 下一篇:NSArray vs NSDictionary 查找

                相关文章

              • <tfoot id='9718l'></tfoot>
                • <bdo id='9718l'></bdo><ul id='9718l'></ul>

                <small id='9718l'></small><noframes id='9718l'>

                  <legend id='9718l'><style id='9718l'><dir id='9718l'><q id='9718l'></q></dir></style></legend>

                    <i id='9718l'><tr id='9718l'><dt id='9718l'><q id='9718l'><span id='9718l'><b id='9718l'><form id='9718l'><ins id='9718l'></ins><ul id='9718l'></ul><sub id='9718l'></sub></form><legend id='9718l'></legend><bdo id='9718l'><pre id='9718l'><center id='9718l'></center></pre></bdo></b><th id='9718l'></th></span></q></dt></tr></i><div id='9718l'><tfoot id='9718l'></tfoot><dl id='9718l'><fieldset id='9718l'></fieldset></dl></div>