0
votes

I have maze game.I have main sprite that walk in the maze game. After main sprite hit one of his "friend", that "friend" will follow behind wherever the main sprite goes.

I know how to detect collision detection but I don't know how to make that "friend" sprite follow main sprite behind. There's must be a static distance between main sprite and "friend" sprite

Please share some code for this problem

2

2 Answers

0
votes

You didn't give a language, but it can be fairly simple or fairly complex. If this is a =hard= distance, you can express it like:

friend x := main x - fixedDistanceX. friend y := main y - fixedDistanceY.

(Smalltalk type syntax.)

This will look kind of stiff, and presents problems if the relative positions between the main sprite and the friend should be able change, i.e., the friend is sometimes north of the main sprite, but other times might be south due to some obstacle.

A more natural approach is to do something like this, after the main sprite moves:

(friend fartherThan: fixedDistance from: Main) ifTrue: [friend moveToward: Main).

Then, whatever AI you have for the friend sprite moving independently can be utilized in this context for keeping it close to the main sprite.

Hope that helps.

0
votes

You can use blake's code, but put it into an update method instead of a touch callback. Have a look at the 'Making Things Move' section of the Cocos2D tutorial called 'Lesson 2: Your First Game'.