Board game logic help

Hello. I need some help with drag & drop and board game functionality. I’m hoping it’s just something stupid I’m missing, and I’d like your help to figure out the stupid mistake I’ve made!

I’ve got a grid based board. If a blue piece wants to jump over a red piece, he can, provided that there’s an empty space there. If not, he bounces back.

I have it mostly working except for the fact that, while I’m dragging a piece around, collision detection is working (but I can’t really shut if off since I need it to decide where to place the piece!), causing spaces to change colors. Then, for some reason, those spaces turn to “nil” after another turn. The “nil” thing is the real problem, because it’s allowing pieces to jump and land on top of other pieces. (Not cool.)

Setting up a board game should be easy! :frowning: lol. Here’s the code…

[lua]–set up board spaces
–set up player pieces

function onTouch(event)
local t = event.target

local phase = event.phase
if “began” == phase then
BoardSet = 0
–boring drag & drop stuff
if “moved” == phase then
–more boring drag & drop stuff

elseif “ended” == phase or “cancelled” == phase then

–Snap back or move to new space

BoardSet = 1
return true
end
end
end

function onLocalCollisionBoard(self, event) --Board space collision detection
if (event.phase == “began”) then
if(BoardSet == 1) then
–Set hidden nil areas to color
self.color = event.other.color
end
print(Game.t[47].color)
return false
else --IF NOT COLLIDING
if(BoardSet == 1) then
self.color = nil
end
return true
end
end

function onLocalCollisionPieces(self, event) --Player piece collision (red/blue)

if (event.phase == “began”) then
self.changeX = 0
self.changeY = 0

if(event.other.id ~= nil) then --set ID of the space 2 spaces ahead (to be checked later)
Game.eid = event.other.id
Game.eid = Game.eid
end

if(event.other.color == nil) then --move to new space if it’s empty
self.NewX = event.other.x
self.NewY = event.other.y
end

if(event.other.color ~= nil) then --JUMP CODE (if there’s a free space 2 spaces ahead)

if(self.YDragging == 1) then --I’m just showing the vertical jumping code

if(event.other.color == “red”) then --RED
if(self.y > event.other.y) then
if(event.other.id ~= nil) then
if(Game.t[Game.eid-1].color == nil) then
self.changeY = -150
print(“LOOOOOOOOL”)
self = display.newImage(“art/blue.png”)
else
self.changeY = 150
end
end
end
if(self.y < event.other.y) then
if(event.other.id ~= nil) then
if(Game.t[Game.eid+1].color == nil) then
self.changeY = 150
else
self.changeY = -150
end
end
end
elseif(event.other.color == “blue”) then --BLUE
if(self.y > event.other.y) then
if(event.other.id ~= nil) then
if(Game.t[Game.eid-1].color == nil) then
self.changeY = -150
else
self.changeY = 150
end
end
end
if(self.y < event.other.y) then
if(event.other.id ~= nil) then
if(Game.t[Game.eid+1].color == nil) then
self.changeY = 150
else
self.changeY = -150
end
end
end
–etc…
end
end
self.NewX = event.other.x + self.changeX
self.NewY = event.other.y + self.changeY
BoardSet = 1

if(event.other.isBoard == true) then
if(BoardSet == 1) then
event.other.color = self.color
end
end

return true
end
end[/lua]

I’ve been putting the BoardSet variable in various places all day, but I just can’t figure it out. Thanks for your help. [import]uid: 191140 topic_id: 35648 reply_id: 335648[/import]

I guess my first question would be why you are using collision detection for this? Secondly, I don’t see any physics code to where you are defining your physics bodies to take place in collision detection. I’ve done a similar game recently and I used a 2D array to represent each spot on the game board. Each piece is represented in this array. When I want to drag my display object, I calculate the grid position then look up in that 2D array to see what’s in the spot I’m trying to drop my piece on. If it’s empty, I allow the move. If it’s occupied, I do a transition.to to put it back. Then I check the total distance moved and if its two spots, then I jumped the other player’s piece and handle removing it. [import]uid: 199310 topic_id: 35648 reply_id: 141783[/import]

Thanks for the response! I’m using for physics for no reason; maybe switching would help.

Everything is in an array and physics added to it; I just didn’t include the code since it would be a lot of extra stuff to read. I know the issue is somewhere in here. I’d rather fix the issue than have to rewrite everything… but it may come to that. Thanks for the tips. :slight_smile: [import]uid: 191140 topic_id: 35648 reply_id: 141793[/import]

I guess my first question would be why you are using collision detection for this? Secondly, I don’t see any physics code to where you are defining your physics bodies to take place in collision detection. I’ve done a similar game recently and I used a 2D array to represent each spot on the game board. Each piece is represented in this array. When I want to drag my display object, I calculate the grid position then look up in that 2D array to see what’s in the spot I’m trying to drop my piece on. If it’s empty, I allow the move. If it’s occupied, I do a transition.to to put it back. Then I check the total distance moved and if its two spots, then I jumped the other player’s piece and handle removing it. [import]uid: 199310 topic_id: 35648 reply_id: 141783[/import]

Thanks for the response! I’m using for physics for no reason; maybe switching would help.

Everything is in an array and physics added to it; I just didn’t include the code since it would be a lot of extra stuff to read. I know the issue is somewhere in here. I’d rather fix the issue than have to rewrite everything… but it may come to that. Thanks for the tips. :slight_smile: [import]uid: 191140 topic_id: 35648 reply_id: 141793[/import]