Reputation: 12444
What I want to accomplish is to make my CCSprite
follow another CCSprite when it is touching it. Now what I mean by follow is, lets say there is an animation of another CCSprite moving up the screen. So if this sprite hits my main sprite, my main sprite should move up the screen with it. This other sprite will be a platform, so technically in the end I would want the sprite to be on top of the other sprite but it would be moving along the top of the platform CCSprite as if the platform was carrying the main sprite.
Now I know how to do the collision detection part and make the other sprite animate but how would I just make my platform 'carry' my main CCSprite also kind of like how an elevator works?
Upvotes: 0
Views: 638
Reputation: 1303
You can move the first sprite without using action, by manually updating his position in a nextFrame:(ccTime) dt kinda function, doing so would be easy to update another sprite position based on your sprite position plus an offset to position it on top.
Another solution could be removing the follower sprite from its parent and adding as a child of the moving node, i think this can be done without flickering and performance loss.
edit: another solution (maybe the best one)
a good approach, that works great with Actions so you dont need to manually schedule movements
You can add a property to the moving sprite (you need to subclass from CCSprite, if you havent already done so), to keep a reference to the follower
@property (readwrite, nonatomic, assign) CCSprite *follower;
@property (readwrite, nonatomic) BOOL followerActive;
then in your game inits, you can create both sprites, and add them as childs of your main layer, then you add the weak reference from your object to the follower
platform.follower = followerSprite;
and when you need to enable the follow platform.followerActive = YES;
at this point in the moving sprite, you can override the setPosition property
-(void) setPosition:(CGPoint)position {
if(self.followerActive) { self.follower.position = ccpAdd(position, offset); } [super setPosition:position]; }
Upvotes: 1
Reputation: 64477
You can use the CCFollow action to have any node follow any other node.
Upvotes: 1
Reputation: 695
I think you can place flag to check the condition "when to follow" in update: method you can reposition the second sprite according to position of first Sprite.
-(void)update:(CCTime)delta
{
if(condition == YES)
{
secondSprite.position = ccp ( firstSprite.position.x + xOffSet , firstSprite.position.y + yOffSet);
}
}
Try this according to your code.. Basic logic is here..
Upvotes: 1