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!
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]
[import]uid: 191140 topic_id: 35648 reply_id: 141793[/import]