Tween question

Continuing my puzzle game Ive managed make some progress but have run into a question about tweens.

When you move a brush to create three in a row the game will detect which these brushes are and then:

  • find the next brush which should not be removed (ie generate score)
  • copy the color of this brush
  • set the x,y values to this brush
  • using tween it animates the brush falling down to the correct position.

The problem is (I think) the brush which you copied the color and position from will also fall down creating a hole in the palette. So i would like these brushes to remain its current position.

Note: the top half of the palette is not supposed to be visible to the player. I am just displaying to be able to debug. Also, when you score the invisible top part of the palette will just get new colors randomly so we dont always have the same colors falling down.

Code can be found here.

https://gist.github.com/fikabord/3193164cddcf40505616

-- animate brush falling down
                            endPos = palette[j][i].pos
                            palette[j][i].pos = palette[jj][i].pos
                            palette[j][i].col = palette[jj][i].col
                            tween(3, palette[j][i].pos, {x=endPos.x, y=endPos.y})

This is the code i think isnot doing what i expect it todo. It tweens this brush
palette[jj][i]
When it should only tween
palette[j][i]

Any idea what i am missing here?

@fikabord, when you set palette[j][i].pos = palette[jj][i].pos, you don’t transfer the value, you set them to the same memory register. This actually means that they point to the same register, resulting in what you do to one you do to both.

For whole tables, you need to perform a deep copy to avoid this. For individual values like your palette positions, I’m not sure what is best to do. Have a look at Ignatz’s blogs https://coolcodea.wordpress.com/2013/03/28/17-things-i-would-have-liked-to-have-known-sooner/ and https://coolcodea.wordpress.com/2013/06/14/84-tables-and-pointers/ for explanations.