如何为tilemap设置动画

时间:2014-02-09 06:39:51

标签: map cocos2d-iphone sprite tile

我是cocos2d和编码的新手,我确信这个问题已经被多次询问了,但我想在一个tilemap中设置一个幽灵,当玩家紧挨着它时,它会增加大约150个像素。我有

    `CCSprite* sprite1 = [CCSprite spriteWithFile:@"enemy.png"];
    sprite1.position = ccp(464, 80);
    [self addChild:sprite1];
    [sprite1 runAction:[CCSequence actions:
                        [CCMoveBy actionWithDuration:1 position:ccp(0, 150)],
                        [CCMoveBy actionWithDuration:1 position:ccp(0, -200)], nil]];`

动画精灵,但它保留在地图上。如果我将它添加到tilemap,它将仅在玩家关闭时显示。但我不确定如何做到这一点。提前致谢

1 个答案:

答案 0 :(得分:0)

不要在tilemap上获取它,然后,您要为该特定字符编写自定义行为,将其与TileMap区分开来。如果您计划获得高效代码,可能需要使用传感器,可能是Box2D或CocosV3最近完全集成的内置Chipmunk引擎,或者如果您没有计划的话,可以采用“简单”方式制作一个滑动场景,你可以在更新方法上使用简单的坐标和一个事件监听器,所以当“角色”到达你希望鬼魂出现的点时,那么你可以使该方法发生。

将自定义行为包装在您可能进一步重用的类上,可能是一个名为sensorSprite的类,一旦您编写了该类的默认行为,您就可以创建方法来实例化具有特定坐标的对象以生成传感器,或者其他一些很酷的东西。

这是你班级的样子。

标题文件

@interface SensorSprite : CCSprite {
    kCharacterState currentState;
}


-(id)initWithSpriteFrame:(CCSpriteFrame *)spriteFrame inCoordinates:(CGPoint)spawnLocation withParent:(id)theParent;


-(void)updateSensorSpriteWithCharacters:(NSMutableArray*)charactersArray;

@end

实施档案

typedef enum {
    kCharacterStateAlive,
    kCharacterStateDead,
    kCharacterStateAppearing,
    kCharacterStateDissapearing

}kCharacterState;


#import "SensorSprite.h"

@implementation SensorSprite

-(id)initWithSpriteFrame:(CCSpriteFrame *)spriteFrame inCoordinates:(CGPoint)spawnLocation withParent:(id)theParent{
    self = [CCSprite spriteWithImageNamed:@"enemy.png"];
    self.position = ccp(464, 80);
    [theParent addChild:self];
    return self;

}


-(void)updateSensorSpriteWithCharacters:(NSMutableArray *)charactersArray {

    //If you're planning on having different characters to update your state from then you should use tags.

    for (int i=0; i<=charactersArray.count-1; i++) {
        CCSprite *characterInArray = [charactersArray objectAtIndex:i];
        if (CGRectIntersectsRect([characterInArray boundingBox], [self boundingBox])) {
            //What happens when the CG Rects from this class and another one intersects.
            //For making reactive to encountering different sprites, you should use tags , and on each cycle detect what sprite is the one colliding by looking at it's tag.


        }
    }

}


-(void)changeStateTo:(kCharacterState)theState {
    if (currentState==theState) {
        //It's the same state.
        return;
    }

    switch (theState) {
        case kCharacterStateAlive:
            //What happens when character state alive.
            break;
        case kCharacterStateDead:
            //What happens when character state dead
            break;
        case kCharacterStateAppearing:
            //What happens when character state reapearing
            break;
        case kCharacterStateDissapearing:
            //What happens when character state dissapearing.

        default:
            break;
    }

    //After doing the required we set our current state to the changed state.
    currentState = theState;
}

@end

请注意我对代码的评论,可以通过大量方式进行改进,但这应该是了解这里发生了什么的基础。

相关问题