0

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?

3 Answers 3

1

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..

Sign up to request clarification or add additional context in comments.

2 Comments

One question though, since I am setting the CCSprite's position here in the game loop, it would become unresponsive to my accelerometer movement. I already have a variable in my game loop though that controls the sprite on the X axis so I guess with your code example, I would just substitute in my accelerometer variable for the X and I would use your code for the Y. Correct?
One final question, I am using a b2ContactListener and I am trying to get this to work in a if statement if 2 sprites collide. I only have a ivar for my follower sprite. The problem is that I do not have any reference to that particular CCSprite but I only have a tag to it. So how would I get a proper reference to the CCSprite with the tag I have?
1

You can use the CCFollow action to have any node follow any other node.

1 Comment

Thanks will look into that also :P
1
  • 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]; }

1 Comment

Yes, @Haroon illustrates this perfectly! :) Thanks for the info!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.