How to replace an image within a table but keep its positioning?

The simplest way to describe the problem is that I have a map of tiles.

Step on a tile, and I need it to change from its original image to an “exhausted” tile image.

Since there’s no easy way to just specify tile.image (at least, I understand that’s not possible with display.newImage?)

Running a secondary layer and toggling visibility is probably out of the question due to the number of tiles needed. (It works in small doses, but at 400? 4000? I’m not sure.)

I was thinking about using removeSelf() and then table.insert(), but there are some problems…

  1. The table goes several layers deep.

--This is where I would have to remove the image and reinsert it. Correct index is important! tilemap[tileX][tileY][1]:removeSelf() table.insert(tilemap[tileX][tileY], 1, newimage)

  1. It’s a display group, so I’m not sure if table.insert is meant to work with that? (If I use the above code, it does not report that the new table.insert is part of tilemap[tileX][tileY]…)

  2. The table is also within a scrollView. table.insert doesn’t add the image to the scrollView. (Although for all I know it’s failing because of #2?) I can manually add it to the scrollView, but that means its sitting in the wrong place in the hierarchy again.

Any ideas on how to approach this? [import]uid: 41884 topic_id: 19537 reply_id: 319537[/import]

Just brain storming here… what about using a sprite table of 2 images, a map tile and exhausted map tile. Just advance the Sprite in the cell to the 2nd exhausted image in the Sprite table as needed?

-David [import]uid: 96411 topic_id: 19537 reply_id: 75474[/import]

Sprite…table?

Like, another displayGroup layer down? That’s effectively loading both images though, which I was hoping to avoid. I mean, it’s somewhat trivial to just have a matching hidden tile grid with substitute images, but that’s double the amount of memory and a bunch of other stuff that could cause performance issues, which is why I was hoping there was a realistic way to reinsert into a table position. [import]uid: 41884 topic_id: 19537 reply_id: 75484[/import]

At the risk of making a fool of myself, I think I’ve found the solution. (Thanks for the thought though, David)

[code] – Save the tile image’s x/y
local xPos, yPos = tilemap[tileX][tileY][1].x, tilemap[tileX][tileY][1].y

– Remove the tile image from inside the tile group.
tilemap[tileX][tileY]:removeSelf()

– Create the new image
local tile = display.newImage(“tile.png”)

– Insert (yay, API docs saving my butt again) using the displayGroup insert options.
tilemap[tileX][tileY]:insert(1, tile, true)

– Set Reference Point (In my case, the image is defaulted to centre, so the tile is not offset properly)
tilemap[tileX][tileY][1]:setReferencePoint(display.TopLeftReferencePoint)

– Set X/Y
tilemap[tileX][tileY][1].x, tilemap[tileX][tileY][1].y = xPos, yPos[/code]

The special commands are:

1 (insert at index 1 - something I didn’t think the displayGroup insert could do.)
true (resets all transforms. Normally bad but within tables makes perfect sense.)

With that, I seem to have perfectly changeable tiles that properly get inherited into the various groups and scrollView! [import]uid: 41884 topic_id: 19537 reply_id: 75495[/import]

Why not just making it like:

local oldX,oldY = tilemap[tileX][tileY][1].x, tilemap[tileX][tileY][1].y  
tilemap[tileX][tileY][1] = display.newImage(newimage)  
tilemap[tileX][tileY][1].x, tilemap[tileX][tileY][1].y = oldX,oldY   

? [import]uid: 13097 topic_id: 19537 reply_id: 75515[/import]

I could try, I suppose. Certainly more straight-forward :slight_smile:

But my understanding with tables (and maybe I’m wrong) is that upon removing the initial [1], everything else slides down the chart ([2] becomes [1], etc.) So in this case I’d just be blowing away whatever [2] was. [import]uid: 41884 topic_id: 19537 reply_id: 75527[/import]