《How To Make a Tile-Based Game with Cocos2D》

Tiled Map Editor
File->New
Orientation(Orthogonal)->Map size(50*50)->Tile size(32*32)
Map->New Tileset...
Tileset->Image(.png)->Tiles->32*32->Margin(1)->Spacing(1)

CCTMXTiledMap *tileMap;
CCTMXLayer *background;

tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"TileMap.tmx"]; //加载地图
background = [tileMap layerNamed:@"Background"]; //背景层
[self addChild:tileMap z:-1];

Object layers允许你在地图上可能发生事件的区域绘制矩形。举个例子来说,你可能需要一个怪物出生点,或者一个进入就会挂掉的区域。这里我们给player创建一个区域,作为其出生点。
Layer->Add Object Layer...
右键添加的这个灰色的矩形->properties->Name(SpawnPoint)

CCSprite *player;

CCTMXObjectGroup *objects = [tileMap objectGroupNamed:@"Objects"]; //对象层
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"]; //出生点
NSAssert(spawnPoint != nil, @"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:@"x"] intValue];
int y = [[spawnPoint valueForKey:@"y"] intValue];
 
player = [CCSprite spriteWithFile:@"Player.png"];
player.position = ccp(x, y);
[self addChild:player];

为移动地图而视点中心

-(void)setViewpointCenter:(CGPoint) position {
 
    CGSize winSize = [[CCDirector sharedDirector] winSize];
 
    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;
 
}

使用ccTouchBegan/ccTouchEnded的特点是:
1. “你不再需要处理NSSets对象了,每次被调用时,dispatcher(分发器)会帮你选择你需要的那个UITouch对象。”
2. “你可以在ccTouchBegan中返回YES来认领一个UITouch,只有这样,属于该代理的触摸事件才能继续下去,你可以明确的知道这个触摸事件是属于你的。这种机制可以让你从繁琐的多点触摸逻辑中解放出来,也可以理解为一种为单点触摸设计的便捷方式。”

self.isTouchEnabled = YES;
 
-(void) registerWithTouchDispatcher //重写registerWithTouchDispatcher方法来注册触摸事件。原来的ccTouchesBegan/ccTouchesEnded(多点触摸)被替换,取而代之的是ccTouchBegan/ccTouchEnded(单点触摸)
{
  [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self 
            priority:0 swallowsTouches:YES];
}
 
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
  return YES;
}

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

使用meta层和layer属性实现阻挡和拾取。
Layer->Add Tile Layer...->Map->New Tileset...
人为地给红色tile定义为具有“阻挡”属性,在要有阻挡的位置绘制这些tile。
右键红色tile->Tile Properties...->Name(Collidable)->Value(True)

CCTMXLayer *meta;

meta = [tileMap layerNamed:@"Meta"];
meta.visible = NO; //只起阻挡作用,不可见

CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [meta tileGIDAt:tileCoord]; //GID,相当于每块tile的标识

CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [meta tileGIDAt:tileCoord];
if (tileGid) {
    NSDictionary *properties = [tileMap propertiesForGID:tileGid];
    if (properties) {
        NSString *collision = [properties valueForKey:@"Collidable"];
        if (collision && [collision compare:@"True"] == NSOrderedSame) {
            return; //精灵不能到达该位置
        }
    }
}

Layer->Add Tile Layer...,添加可以拾取的物品。
修改meta层,在每个想设置为可拾取的区域绘制绿色tile。
右键绿色tile->Tile Properties...->Name(Collectable)->Value(True)

CCTMXLayer *foreground;

foreground = [tileMap layerNamed:@"Foreground"];

NSString *collectable = [properties valueForKey:@"Collectable"];
if (collectable && [collectable compare:@"True"] == NSOrderedSame) {
    [meta removeTileAt:tileCoord]; //物品被拾取
    [foreground removeTileAt:tileCoord];
}

计算玩家相对于敌人的方向,旋转敌人以对准玩家。

CGPoint diff = ccpSub(player.position,enemy.position);
float angleRadians = atanf((float)diff.y / (float)diff.x);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
if (diff.x < 0) {
  cocosAngle += 180;
}
enemy.rotation = cocosAngle;

 

posted @ 2013-02-14 14:20  joojoosue  阅读(282)  评论(1编辑  收藏  举报