Dragging a Tile

Thanks to whoever is watching this for taking some of his/her time to help me out.

Here’s the thing, I want to drag a tile(a tree tile from the IsometricAndPlayer sample), but it does not drag the tile(or at least doesnt drags the tree), it just kind of like presses the tiles Im dragging it to, but not the tile.

--screenToGrid is a function that takes a screen position, transforms it to a world position and then to a grid position  
local startGridPos = screenToGridPosition(event)  
--getTileFromScreen is a function that takes a screen OR grid position and returns the tile at that position  
local tile = getTileFromScreen(startGridPos)  
  
if not tile then  
 --alert() its like native.showAlert(), just made simpler and similar to the JS alert() call  
 alert('Tile == NIL before event.phases')  
end  
  
if event.phase == 'began' then  
  
 if tile then  
 --sets the starting tile image to the tree tile  
 tile:setImage(1)  
 end  
  
elseif event.phase == 'moved' then  
  
 if tile then  
 --Attempting to drag it  
 tile:drag( event )  
 else  
 alert('No tile Object')  
 end   
elseif event.phase == 'ended' then  
  
 --Getting the tile from the ending position  
 --local tmpTile = getTileFromScreen(event)  
  
 --Setting the image to the tree tile  
 --if tmpTile then  
 -- tmpTile:setImage(1)  
 --end  
  
end  

I dont know if I explained myself correctly. Thanks again! [import]uid: 109816 topic_id: 19213 reply_id: 319213[/import]

Try moving the “tile:drag( event )” code to just before the first event.phase if statement.

If that doesn’t help, please send a zipped up project/demo showing the problem in action to support@justaddli.me and I will take a look at it tomorrow. I would look now but it’s 3am and I’ve been trying, and apparently failing, to sort out my sleep schedule. [import]uid: 5833 topic_id: 19213 reply_id: 74091[/import]

Moving the “tile:drag( event )” to before the first event.phase if statement did not work.

I zipped it up and send it. Seriously thank you for checking this at 3AM…you’re really dedicated. Hope you have a good night sleep, Im sure you deserve it [import]uid: 109816 topic_id: 19213 reply_id: 74097[/import]

No worries. You should have an email now with what I believe is a solution. Hopefully it gives you the desired effect. [import]uid: 5833 topic_id: 19213 reply_id: 74199[/import]

Hi, thanks for the code. It kind of works, but not as I want it, maybe I explained myself wrong. The problem right now, is that it destroys the original tile, and that I cant drag an already placed object. It just makes a copy and I drag the copy, but the original tile stays in the same position.

I changed the code from “tile:destroy” to “tile:setImage(2)”(to change the tree image to a grass image), but the originial tile(set to image grass) is not the same as all others, it has no borders, so it looks weird. And it becomes kind of like destroyed(I see the background) while dragging the object. I dont know if im explaining myself correctly.

I dont know if it’s too much to ask but how can I know if the tile is being occupied and change the tile background color( for example it’s transparent while dragging-with alpha I guess-, and if Im hovering over an occupied tile, the background of the tile becomes red, so the user knows it cant be placed there ) and if the tile is tried to placed on an occupied tile it cancels the drag, and everything remains the same. [import]uid: 109816 topic_id: 19213 reply_id: 74203[/import]

My bad :slight_smile:

The problem with dragging and already dragged tile is that the getTileAt family of functions work off the original unchanged tile positions. This is for speed otherwise it would have to do a whole lot of checks. I am trying to find a better solution for this.

At present the Map:getTileAt function does have a second paramater that allows a full search, this can take longer and isn’t fully tested.

To find out if a tile already exists you could use the getTileAt function with whatever position you are hovering over and if it returns a tile then that space must be occupied, otherwise it is empty.
[import]uid: 5833 topic_id: 19213 reply_id: 74206[/import]

Whenever I put the argument true to the getTileAt() function, the Ipad dies:

Here is the error code:

[code]

Runtime error
…n_2011/Samples/IsometricAndPlayer/lime-tileLayer.lua:156: attempt to index field ‘?’ (a nil value)
stack traceback:
[C]: ?
…n_2011/Samples/IsometricAndPlayer/lime-tileLayer.lua:156: in function <…n_2011>
(tail call): ?
…3_4_Jun_2011/Samples/IsometricAndPlayer/lime-map.lua:857: in function ‘getTilesAt’
…3_4_Jun_2011/Samples/IsometricAndPlayer/lime-map.lua:879: in function <…3_4_jun_2011>
(tail call): ?
…ime_3_4_Jun_2011/Samples/IsometricAndPlayer/main.lua:96: in function <…ime_3_4_jun_2011>
?: in function <?:215>
[/code]

And about the hovering over a tile, the problem is that the grass tile is a tile, so it is always defined and returns the tile, is there a way what gid the tile has(so I know if its a grass tile or not)? Maybe I should make a property and check it(something like ‘itsGrass’). What do you suggest.

Again, thank you very much for your help [import]uid: 109816 topic_id: 19213 reply_id: 74222[/import] </…ime_3_4_jun_2011></…3_4_jun_2011></…n_2011>

That looks like a bug, will look into that.

I would go for checking if there is the property yes, nice easy way to do it and allows you to have different “grass” tiles that have different IDs but still the same property. [import]uid: 5833 topic_id: 19213 reply_id: 74229[/import]

Thanks for the suggestion, I still cant make it work(probably Im not using the function right or doing something bad), but Ill work on that on my own, because Im afraid im taking too much of your time. Thank you very much for having helped me. [import]uid: 109816 topic_id: 19213 reply_id: 74234[/import]

No worries, if you continue to struggle don’t worry about contacting me again. [import]uid: 5833 topic_id: 19213 reply_id: 74458[/import]