I have called the images 000.png up to 015.png, it is much easier to load the images through a loop.
I started from a 1024x1024 image and divided it into 16 equal pieces (256x256). Help to place the puzzle pieces on the screen.
The numbers (ids) of my objects range from 1 to 16, consecutive ids help to work with them in a loop.
Otherwise you must rewrite the script.
I suppose you want to place all 16 objects in different places in the game, but I don't know how many levels your game will have, and how many objects you want to place per level.
I have not tried the map as standalone yet.
A drawback that could be experienced could be the global variables not retaining their value between levels. In addition you should add the prefix (g_) to the global variables.
If this is the case we will try to solve it, in case it is not possible, then we will resort to generating a text file as you mentioned before.
Anyway this is a good starting point.
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Player Collects puzzle Items - 3com
imgPieces = {}
sprPieces = {}
Pieces = {}
imgDirPath = "scriptbank\\images\\Puzzle16\\" -- Type here your image directory path
local keypressed = 0
function collect_puzzleparts_init(e)
for i = 0,15,1 do
if i <= 9 then
imgPieces[i+1] = LoadImage(imgDirPath .."00" ..i ..".png")
else
imgPieces[i+1] = LoadImage(imgDirPath .."0" ..i ..".png")
end
end
for i = 0,15,1 do
sprPieces[i+1] = CreateSprite ( imgPieces[i+1] )
SetSpriteDepth ( sprPieces[i+1] , 100 )
SetSpritePosition (sprPieces[i+1] , 200,200)
end
end --init
function collect_puzzleparts_main(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist < 80 then
PlaySound(e,0)
Collected(e)
Pieces[e] = 1
Prompt ("Press E key To see puzzle, Q key To hide puzzle")
end
if g_KeyPressE == 1 and keypressed == 0 then
keypressed = 1
for i = 1,#Pieces,1 do
if Pieces[i] == 1 then
myWidth = GetImageWidth ( imgPieces[i] )
myHeight = GetImageHeight ( imgPieces[i] )
if i == 1 then
SetSpritePosition (sprPieces[i],0,0)
elseif i == 2 then
SetSpritePosition (sprPieces[i],myWidth,0)
elseif i == 3 then
SetSpritePosition (sprPieces[i],myWidth*2,0)
elseif i == 4 then
SetSpritePosition (sprPieces[i],myWidth*3,0)
elseif i == 5 then
SetSpritePosition (sprPieces[i],0,myHeight)
elseif i == 6 then
SetSpritePosition (sprPieces[i],myWidth,myHeight)
elseif i == 7 then
SetSpritePosition (sprPieces[i],myWidth*2,myHeight)
elseif i == 8 then
SetSpritePosition (sprPieces[i],myWidth*3,myHeight)
elseif i == 9 then
SetSpritePosition (sprPieces[i],0,myHeight*2)
elseif i == 10 then
SetSpritePosition (sprPieces[i],myWidth,myHeight*2)
elseif i == 11 then
SetSpritePosition (sprPieces[i],myWidth*2,myHeight*2)
elseif i == 12 then
SetSpritePosition (sprPieces[i],myWidth*3,myHeight*2)
elseif i == 13 then
SetSpritePosition (sprPieces[i],0,myHeight*3)
elseif i == 14 then
SetSpritePosition (sprPieces[i],myWidth,myHeight*3)
elseif i == 15 then
SetSpritePosition (sprPieces[i],myWidth*2,myHeight*3)
elseif i == 16 then
SetSpritePosition (sprPieces[i],myWidth*3,myHeight*3)
end
end
end
end -- g_KeyPressE == 1
if g_KeyPressQ == 1 and keypressed == 0 then
keypressed = 1
for i = 1,#Pieces,1 do
if Pieces[i] == 1 then
SetSpritePosition (sprPieces[i] , 200,200)
Hide(i)
CollisionOff(i)
end
end
end -- g_KeyPressQ == 1
if g_KeyPressE == 0 and g_KeyPressQ == 0 then keypressed = 0 end
end --main
Attach script to each object you has to collect in order to get corresponding puzzle pieces on screen.
I ignore whether you want to show the whole image once, or whenever the player want to.
You should keep in mind that when you hide an object it is still active, and therefore continues to execute the attached script. When the object is destroyed, the attached script stops executing.
If you convert the object to "AlwaysActive = 1" then the player will be able to access the full image whenever he wants, otherwise he will only be able to access when the player is within his range of action. This may be relevant if, for example, you intend to use the complete image as a clue to find something, for example a hidden key.
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz
OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4