Placing a tile on the map

I just bought Lime and I’m a newbie to both Corona and Lime. I’m trying to make a simple five-in-a-row game but have encountered a problem I just can’t get my head around. I made a tileset consisting of 3 tiles; a gridshaped tile, an X tile and an O tile. I then made a map in Tiled and filled it entirely with the grid-tile. Next I made a drag and a touch routine so that I could drag the map around and touch the square/grid anywhere to place an X or an O. And here is the problem. I can’t figure out how to place the X/O tile at the given map/grid position where the user touches the screen. I’ve tried different commands like createTileAt, createAndBuildTile, createAndBuildTileAt with no results. I have added the bug-fix to the lime-tile.lua code (http://developer.anscamobile.com/forum/2012/01/03/cant-set-tile-gid), but I still can’t get any tile-manipulating routines to work. (removeTileAt, swapTiles or any of the above) What am I doing wrong and how do I accomplish this. I guess the optimal solution for this problem would be the hopefully? upcoming TileObjects functions, but I still can’t see why I can’t get any of the createTile functions to work.

Here’s the code:

io.output():setvbuf('no')  
display.setStatusBar( display.HiddenStatusBar )  
  
local lime = require("lime")  
  
lime.disableScreenCulling()  
  
local map = lime.loadMap("MyMap.tmx")  
  
local visual = lime.createVisual(map)  
  
local onUpdate = function(event)  
 map:update(event)  
end  
local onTap = function(event)  
 local screenPosition = { x = event.x, y = event.y }  
 local worldPosition = lime.utils.screenToWorldPosition(map, screenPosition)  
  
-- This is where I want my code to go. Something like:  
-- map:createAndBuildTileAt(2, "MyTileLayer1", {row = worldPosition.x, column = worldPosition.y} )  
-- If I understand this correctly, the 2 (gid) above is the second tile in my tilesheet, which is the X-tile.  
  
end  
  
Runtime:addEventListener("tap", onTap)  
  
local onTouch = function(event)  
 map:drag(event)  
end  
  
Runtime:addEventListener("touch", onTouch)  
Runtime:addEventListener("enterFrame", onUpdate)  
  

I would appreciate any help. [import]uid: 129287 topic_id: 22817 reply_id: 322817[/import]

Are you getting an error in the terminal or anything like that or is the tile seemingly just disappearing?

setTileAt() might work better for you in this case - http://justaddli.me/api.php?c=tileLayer&m=setTileAt

If you like, feel free to send your code ( or a simple demo of the issue ) to support@justaddli.me and I will take a look at it.
[import]uid: 119420 topic_id: 22817 reply_id: 91155[/import]

Actually, nothing happens. The tile does not disappear, nor does the new tile appear. The screen is not changing at all.
I didn’t get any error messages in the terminal. However, when using the setTileAt() function which I’ve also tried earlier, the following error message pops up when I click on a grid-tile:


Windows simulator build date: Dec 9 2011 @ 14:01:29

Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
WARNING: Cannot create path for resource file ‘ui.lua’ b/c it does not exist.

Runtime error
d:\programming\corona projects\tut\main.lua:29: attempt to index global
’MyTileLayer1’ (a nil value)
stack traceback:
[C]: ?
d:\programming\corona projects\tut\main.lua:29: in function <d:>ing\corona projects\tut\main.lua:20>
?: in
If this doesn’t help, I’ll send you a copy of my code.
Thanks. [import]uid: 129287 topic_id: 22817 reply_id: 91164[/import] </d:>

You are trying to access an object called “MyTileLayer1” however this object doesn’t exist. Are you sure you have used the correct name when calling map:getTileLayer() ? [import]uid: 119420 topic_id: 22817 reply_id: 91174[/import]

Just got your email and figured I will put the solution here in case anyone else finds the answer useful.

You were passing in the position like so: {row = worldPosition.x, column = worldPosition.y }

Grid and world positions are not interchangeable like that, you would need to convert the world position to a grid position first. Or you could just pass the world position as is. You would need to call the function like so:

map:createAndBuildTileAt(5, “MyTileLayer1”, worldPosition )

Or even easier you can call the setTileAt function like so:

tileLayer:setTileAt( 5, worldPosition )

I have tested this and it works my end and also, that is the biggest tic-tac-toe board I think I have ever seen :slight_smile: [import]uid: 119420 topic_id: 22817 reply_id: 91175[/import]

This is strange, but I can’t get any of your solutions to work. I’ve now tried both functions above, I’ve tried converting the world position to grid position and I’ve tried to pass integers instead of variables to the functions. But nothing happens. Are you sure this works with the 3.4 version of Lime?
Note: I’m using the free version of Corona (build 2011.704 (2011.12.8)) on a Windows platform.

Thank you for the quick reply.

(PS! The large board was just for testing-purposes and I did’t care to change it before this is working) [import]uid: 129287 topic_id: 22817 reply_id: 91183[/import]

I have tested it against the 3.4 version of Lime however I did have to put in the changes from the link you posted above. This is using a daily build of a few weeks ago.

Are you able to send me your code again but with your copy of the Lime code too thanks. [import]uid: 119420 topic_id: 22817 reply_id: 91241[/import]

Great, it’s working!

By replacing

map:createAndBuildTileAt(5, “MyTileLayer1”, {row = worldPosition.x, column = worldPosition.y} )

with

local layer = map:getTileLayer( “MyTileLayer1” )
layer:setTileAt( 5, worldPosition )

as you told me to do, it’s now working like a charm.

Many thanks for your kind support. [import]uid: 129287 topic_id: 22817 reply_id: 91293[/import]

Awesome, glad it’s all working! [import]uid: 119420 topic_id: 22817 reply_id: 91448[/import]

I’m still having trouble understanding the syntax of the different tile-placement routines. I need to change the layer:setTileAt routine to the layer:createAndBuildTileAt or map:createAndBuildTileAt routine because I want to include all the properties with the tile I’m placing on my map. I’ve tried the following 2 ways of doing this, with no luck:

layer:createAndBuildTileAt( 5, worldPosition )
map:createAndBuildTileAt( 5, “MyTileLayer1”, worldPosition )

In the first example I also tried to set
local layer = map:getTileLayer( “MyTileLayer1” )
before calling the routine.

Absolutely nothing happens. No tiles are placed on the map and I don’t get any errors in the terminal.

Any ideas? [import]uid: 129287 topic_id: 22817 reply_id: 94340[/import]

I believe the issue you are experiencing is actually connected to the map:hide() issue you also noticed. If so then it too has been fixed in 3.5 so until that comes out you should be able to solve it by following this thread - http://developer.anscamobile.com/forum/2012/01/03/cant-set-tile-gid [import]uid: 119420 topic_id: 22817 reply_id: 94563[/import]

As mentioned in my first post, I’ve already apllied the bug-fix to the lime-tile.lua code, so this is not what causes the problem. [import]uid: 129287 topic_id: 22817 reply_id: 94991[/import]

Sorry, I must have missed that part. Are you able to send me your code as is ( or a small demo that uses your copy of Lime ) and I will take a look. [import]uid: 119420 topic_id: 22817 reply_id: 96278[/import]

Couldn’t reach you at support@justaddli.me so I mailed it to support@glitchgames.co.uk. Hope that’s ok. [import]uid: 129287 topic_id: 22817 reply_id: 96559[/import]

Yup I got it but was away all weekend, getting close to catching up on emails now so will be at yours very soon. [import]uid: 119420 topic_id: 22817 reply_id: 96697[/import]

Found it :slight_smile:

In TileLayer:createTile( gid ) in lime-tileLayer.lua you will need to add “tile:show()” around line 551 after the grid position is updated.

What you will find is that your new tile isn’t aligned to the grid, if this isn’t intentional then you can fix this by converting the position to a grid position and using that in the createAndBuildTileAt function. [import]uid: 119420 topic_id: 22817 reply_id: 96702[/import]

Ah, brilliant! It works and finally I’m back on track.
And once again, thanks for your eminent support. [import]uid: 129287 topic_id: 22817 reply_id: 96727[/import]

Awesome! [import]uid: 119420 topic_id: 22817 reply_id: 96965[/import]