Merging

When two fruits merge we will position the new bigger fruit at average x and y position of the two fruits which originally collided.

Create two variables to store this position.

Create a new function and add it to the functions group

Name the function process collisions and enable

COPY PICKED

Why copy picked?


Normally, calling a function will run the function with picking reset. That means if an event picks some instances with conditions, then calls a function, the function runs with all instances picked again, ignoring the fact that conditions previously picked some instances.

Enabling Copy picked on the function changes this so the function keeps the same picked instances when it is called. This can be convenient for making a function that affects a single instance, for example - its actions will run on the instance picked by the caller, rather than having to pick the instance another way (e.g. by its UID).

This means that any objects that were picked in the event that called the function, will also be picked in the function, and you don't have to pick them again!

It should have some conditions:

That the two colliding fruits are the same (based on their animation frames)

That neither fruit is already the biggest fruit (watermelon)

That neither fruit has already collided.

Remember that Copy Picked ensures that our function ONLY acts on the two fruits which were picked in the collision.

The things which need to be done when two matching fruits collide are:

Record the average position of the collision

So that we have somewhere to spawn the new big fruit

Mark both fruits as having collided.

Disable physics on both fruits so they don't keep colliding.

Start the flashing effect to indicate merging

Increase the scale of the fruit

Move to the point half-way

between the two fruits

Destroy the old fruits and create

a new bigger fruit

Because we are using Templates the Fruit created is based on one of our 7 templates.

We add 1 so that the fruit created is always the next fruit up from the fruit which merged.

The final function looks like this:

SOMETHING has to CALL this function. Create a new group called Fruit Collision.

In it we check for collisions

Call the processCollision function

Implement the flashing effect