Jonathon,
Here is a quick and dirty way to do what I think you are trying to do. The method (of sorta OOP) that I use I picked up from someone who posted it on the forum a long time ago. I adapted it a little to fit my needs. I am not saying it is the best way or right way or only way. It is just a way that works for me.
Again this is the bare bones. I cannot ‘ever’ get the ‘formatting’ code tags on the forum post to show my code properly formatted. So cut and paste the code below into a new project on your editor to see the code formatted. And it will be eraser to read.
I only did the piece class and main. I did not bother with a puzzle class, which you may be able to figure how to do that from this code.
Code makes a single puzzle of 4 pieces, 2x2.
You will need 2 lua files (main.lua and piece.lua) to test this. And you will need to crate 4 images name p1.png, p2.png, p3.png, p4.png… they should each be the same size … and a background image to use as a frame for the puzzle.
********* piece.lua *******
– sample code from corona tutorial on collisions
local function hasCollided( obj1, obj2 )
if ( obj1 == nil ) then --make sure the first object exists
return false
end
if ( obj2 == nil ) then --make sure the other object exists
return false
end
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
local M = {}
function M.new(_id)
local self = { id = _id, image, home, origX, origY, storePos }
self.image = display.newImage(“Textures/Puzzles/Puzzle1/p” … self.id … “.png”)
self.home = display.newRect(0,0,self.image.width, self.image.height)
self.storePos = {x =0, y = 0}
local function getHome()
return self.home
end
local function setPosition(_x, _y)
self.image.x = _x
self.image.y = _y
– shift image; adjust for reference point
self.image.x = self.image.x - (self.image.width * .5)
self.image.y = self.image.y - (self.image.height * .5)
– this rect is the home spot for the piece.
self.home.x = self.image.x
self.home.y = self.image.y
self.home.isVisible = false
end
local function getImage()
return self.image
end
local function store(_pos)
self.image.x = _pos.x
self.image.y = _pos.y
self.storePos.x = self.image.x
self.storePos.y = self.image.y
end
local function onTouch(e)
– you may find better ‘dragging’ code on the forum
– this is just a simple version of dragging object
if e.phase == “began” then
display.getCurrentStage():setFocus( e.target )
self.origX = self.image.x
self.origY = self.image.y
elseif e.phase == “moved” then
local x = (e.x - e.xStart) + self.origX
local y = (e.y - e.yStart) + self.origY
self.image.x, self.image.y = x, y
elseif e.phase == “ended” or e.phase == “cancelled” then
– now check for collision with the home spot
– if collided with home rectangle, move piece into place
if hasCollided( self.image, self.home ) then
– let main know piece fits, let main insert piece into the puzzle frame
G_setPiece(self.id)
else
– move piece back to the side of screen, it’s original store position
transition.to(self.image, {time = 500, x = self.storePos.x, y = self.storePos.y})
end
display.getCurrentStage():setFocus(nil)
end
return true
end
self.image:addEventListener(“touch”, onTouch)
return { getHome = getHome, setPosition = setPosition, store = store, getImage = getImage }
end
return M
********* main.lua ********
display.setStatusBar(display.HiddenStatusBar)
local _W = display.contentWidth
local _H = display.contentHeight
local MID_W = _W * .5
local MID_H = _H * .5
local Piece = require(“piece”)
local pieces = {}
local pieceCnt = 1
local bkgd = display.newImage(“Textures/bkgd.png”)
local puzzleFrame = display.newGroup()
puzzleFrame:insert(bkgd)
– 2X2 PUZZLE … total 4 pieces
for i = 1, 2 do
for ii = 1, 2 do
table.insert(pieces, Piece.new(pieceCnt) )
puzzleFrame:insert(pieces[pieceCnt].getHome())
local width = pieces[pieceCnt].getImage().width
local height = pieces[pieceCnt].getImage().height
pieces[pieceCnt].setPosition( (ii - 1) * width, (i-1) * height )
pieces[pieceCnt].store( {x =50, y = pieceCnt * 40} )
pieceCnt = pieceCnt + 1
end
end
puzzleFrame.x = MID_W
puzzleFrame.y = MID_H
function G_setPiece(_id)
puzzleFrame:insert(pieces[_id].getImage())
pieces[_id].getImage().x = pieces[_id].getHome().x
pieces[_id].getImage().y = pieces[_id].getHome().y
end