As I said earlier I’m a complete novice so apologies. My mind has not grasped many of the fundamental concepts of the code or of modern programming languages…bring back the vic20! Adventureland nice!
I’ve purchased the programming in lua 5.2 manual and have been skimming through that, along with the operating manual from the internet and have read some of the very helpful codea documentation provided by Ignatz.
I’m trying to understand the relationship between the setup function and the draw function. It is stated that the draw function is running approx 60 times a second which I assume means that it’s on perpetual loop, whilst when I’m reading about efficiencies in coding , things are mentioned such as load a sprite from documents via setup so it only occurs once (as if you load it up in draw then it’s attempting to do it 60 times a second) etc. But setup isn’t just occurring once is it? Because the parameter functions are defined there and can be freely manipulated and even receive information back from the draw function…or are these separately existing functions which are just being setup and called from setup before continuing to exist independently from the setup function?
Is there a correct way of conceptualising the relationship?
I’m trying to understand this as this conceptualisation informs how one would use loops correctly and efficiently. I’ve already been helped several times and am probably annoying everyone so I’m trying to pin point the underlying cause. (My stupidity is the obvious answer…and perhaps a couple years study of object oriented programming) At the moment I’m trying to get codea to generate a set of systematic images, and then store these in documents, the problem being to do this I have to use loops in the draw function…or do I? I can achieve my aim semi-automatically via a parameter in setup which can then be moved backwards and forwards via touch and each point of the parameter then saves an image, but how do I then suspend the parameter and get the computer to do it automatically as the semi automatic method jumps and misses images due to the sensitivity of the slide bar…where does the loop go…but most importantly why does it go where it goes?
-- TileConstructor
--# Main
--to convert the numbers 0 to 255 into binary
--and store the binary code for each number in a table
--to generate a graphical image based on each binary code
function setup()
-- parameter.action("Get matrix",devtab)
-- parameter.integer("k",1,512,109)
-- parameter.action("Clear Output", function() output.clear() end )
screen = image(WIDTH,HEIGHT)
basetile=readImage("Documents:newtile") --these are to be used in the draw function section
shady=readImage("Documents:newshade") -- ditto
xcoord={0,41,82,0,41,82,0,41,82} -- ditto
ycoord={0,0,0,41,41,41,82,82,82} -- ditto
m={}
for ident = 1, 512 do
m[ident]={}
for y=1,9 do
m[ident][y]=0
end
binary(ident,ident-1,1)
end
end
--function devtab()
-- print("new matrix")
-- print(m[k][7],m[k][8],m[k][9])
-- print(m[k][4],m[k][5],m[k][6])
-- print(m[k][1],m[k][2],m[k][3])
-- end
function binary(ident,rntot,i)
rntot,r=math.modf(rntot/2)
if r>0 then r=1 else r=0 end
m[ident][i]=r
i = i + 1
if rntot>0 then binary(ident,rntot,i) else return
end
end
function draw()
setContext(screen)
spriteMode(CORNER)
sprite(basetile,0,0)
for i = 1,9 do
if m[k][i]==1 then sprite(shady,xcoord[i],ycoord[i]) end
end
setContext()
sprite(screen)
capturedImage = screen:copy(0,0,124,124)
picturesave()
end
function picturesave()
str="tile"..k
print (str)
saveImage("Documents:"..str,capturedImage)
end
If you reinstate the parameters everythingvworks perfectly…however only semi automatically…I wish to loop through k, yet my attempts cause the program to crash, occasionally run but black out, or simply closes codea down.