Search code examples
dictionarycocos2d-iphonespritetile

How to animate a tilemap


I'm new to cocos2d and coding and I'm sure this question was asked many times already, but I want to animate a ghost in a tilemap to go up about 150 pixels when the player is next to to it. I have

    `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]];`

which animates a sprite but it stays on the map. If I add it to a tilemap it will only show when the player is close. But I'm not exactly sure how to do that. Thanks in advance


Solution

  • Don't get it on a tilemap then, you're coding custom behavior for that specific character, work it apart from the TileMap. You'll have to work on sensors if you're planning on getting efficient code, maybe Box2D or the built in Chipmunk engine that CocosV3 has recently fully integrated, or an "easy" way out of this, if you're not planning on making a sliding scenario you could work simple coordinates and an event listener on the update method, so when the "character" reaches the point you wish the ghost to appear, well then you can make that method happen.

    Wrap the custom behavior on a class that you may further reuse , maybe a class named sensorSprite , once you code the default behavior for the class, you could create methods to instantiate the object with specific coordinates to generate the sensor around, or some other cool stuff.

    Here's what your class could look like.

    Header File

    @interface SensorSprite : CCSprite {
        kCharacterState currentState;
    }
    
    
    -(id)initWithSpriteFrame:(CCSpriteFrame *)spriteFrame inCoordinates:(CGPoint)spawnLocation withParent:(id)theParent;
    
    
    -(void)updateSensorSpriteWithCharacters:(NSMutableArray*)charactersArray;
    
    @end
    

    Implementation File

    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
    

    Notice my comments on the code, it could be improved in huge ways, but this should be your base to understand what's going on here.