Help with arrays with timers?

I’ve been sketching out an app i want to make and one has come across my mind, which involves arrays and timers. Problem is i don’t know where to start.

I want to make some items pop up on the screen for 1 second receive a touch event then go to the NEXT array table and any pictures that aren’t touched from the previous array count as +1 for the score. On this map 0 should be empty space and 1 should be an image that i have made. It would last for 1 minute so 1 second per array would be 60 arrays?

local Map = {{},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}}

Where should i start?

 I dont get when you say “any pictures that aren’t touched from the previous array count as +1 for the score”

but you can go with something like

local count = 0

local mapArray = {the one you’ve made}

local image

local function showOrNot(number)

   local val1 = math.floor(number/10) + 1

   local val2 = number%10

   if(mapArray[val1][val2] == 1)then

      --show the Image

      image = display.newImage(“image.png”)

   elseif(mapArray[val1][val2] == 0)then

      --remove the Image

      image:removeSelf()  

   end     

end

timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60)

the functions seems a lot more complicated since you wanted it to be a two dimensional array :slight_smile:

and also there are a lot of options. you can just adjust the alpha value to hide the image rather than literally destroying it, like above

-Storyboard local storyboard = require ( "storyboard" ) local scene = storyboard.newScene() local count = 0 local mapArray = {{}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}} local image local function showOrNot(number) local val1 = math.floor(number/10) + 1 local val2 = number%10 if(mapArray[val1][val2] == 1)then --show the Image image = display.newImage("BlueSquare.png") elseif(mapArray[val1][val2] == 0)then --remove the Image image:removeSelf() end end timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60) function scene:createScene(event) local group = self.view background = display.newImage( "Background.png", true) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 end function scene:enterScene(event) local group = self.view end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

I’m getting an error, attempt to index up value “image” (a nil value)

first when you construct the array you should just use
local array1 = {
{
 i don’t know if you have used
local array1 = {{},
}
 
your wanting to add a small array in the first index?  
if you’re intending to do this than add a constant value to val1 in the function
  local val1 = math.floor(number/10) + 2
 
 
 
and also  on top of the image:removeSelf()  add if(image)then
so the’ll check if an image exists or not, Im sorry i was in a rush so I left that part out :)  :slight_smile:
 
but on top of that creating and destroying image that frequently is relatively inefficient so I reccomend you do this
 
local image = display.newImage(“BlueSquare”)
local function showOrNot(number)
local val1 = math.floor(number/10) + 1
local val2 = number%10
if(mapArray[val1][val2] == 1)then
image.alpha = 1
else
--since the value is always 1 or 0 let’s just use else
–dont remove the just change the alpha
image.alpha = 0
end
end

or even this will work

local image = display.newImage(“BlueSquare”)
local function showOrNot(number)
local val1 = math.floor(number/10) + 1
local val2 = number%10
if(mapArray[val1][val2] == 1)then
image.isVisible = true
else

image.isVisible = false
end
end

Ok now I’m getting it so i would make 60 arrays for the 60 seconds and use the timer perform with delay right? also using this code i now get an error saying attempt to index field “?” (a nil value)

local count = 0

local mapArray = {},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

               

local image = display.newImage(“BlueSquare.png”)

local function showOrNot(number)

local val1 = math.floor(number/10) + 1

local val2 = number%10

if(mapArray[val1][val2] == 1)then

image.isVisible = true

else

image.isVisible = false

end

end

timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60)

[lua]

local count = 0
local mapArray = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}
}
local image = display.newImage(“BlueSquare.png”)

local function showOrNot(number)
    local val1 = math.floor(number/10) + 1
    local val2 = number%10
    
    if(mapArray[val1][val2] == 1)then
        image.isVisible = true
    else
        image.isVisible = false
    end
end

timer.performWithDelay(1000,function() count = count +1; showOrNot(count) end,60)

[/lua]

try this, i havent checked it tho, but it should work.

the nil value comes from your first check of showOrNot with a count of 1,

your first array inside your mapArray is empty:
[lua]

local mapArray = {}, – << THIS empty {} is your first array

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

[/lua]

so, your first row inside mapArray is an empty array (nil), in conclusion: there is no second dimension but if(mapArray[val1][val2] == 1)  is trying trying to index one. that leads to the error.

oh i found another problem

[lua]

local val2 = number%10

[/lua]

as soon as your number will have no remainder, your val2 will be 0,
so each time number hits 10, 20, 30 and so on, your indexing a table with 0, and lua tables always start with 1.

if you would print your values you would see that after checking
mapArray[1][9]
you are looking for
mapArray[1][0]
instead of [1][10]

i would fix that with an if quick and dirty
[lua]
local function showOrNot(number)
    local val1 = math.floor(number/10) + 1
    local val2 = number%10
    if(val2 == 0)then val2 = 10 end --here we go
    
    if(mapArray[val1][val2] == 1)then
        image.isVisible = true
    else
        image.isVisible = false
    end
end

[/lua]

ok thanks, my games starting to piece together now, i got some errors fixed and used your code and it works fine, problem is that the pictures don’t appear where they should, all i get is 1 square in the top left corner, yes my background is inserted into a group.  

show us your current complete code, and please use the [lua] tags, its painful to read such a big codeblock otherwise.
the last complete one from you i am seeing here is the one from Yesterday, and that one doesnt change the image.x and image.y properties at all, so of course they’re positioned at the 0,0 coordinate of your screen, what is the top left corner.


if your maparray shall be some tiled pixel grid, you would do it like that:
 

local positionX = (val1 - 1) * image.width

local positionY = (val2 - 1) * image.height

image.x, image.y = positionX, positionY

you need to subtract 1 because you want a startposition of zero, and not of one width or one heigt, that would make your whole tile grid being offset by one tiles size.

(sorry had to edit this a million times, because my code was showing up as one line when i put the lua tags around it)

ah okay, so with your code they appear at different spots but only 1 at a time and want them in 1 whole second to show how the table shows it. anyway here is the current code i have got.

[lua]

–Storyboard

local storyboard = require ( “storyboard” )

local scene = storyboard.newScene()

local count = 0

local mapArray = {

    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

    {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

    {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

}

 local image = display.newImage(“BlueSquare.png”)

local function showOrNot(number)

    local val1 = math.floor(number/10) + 1

    local val2 = number%10

    local positionX = (val1 - 1) * image.width

    local positionY = (val2 - 1) * image.height

    image.x, image.y = positionX, positionY

    

    

    if(val2 == 0)then val2 = 10 end

    

    if(mapArray[val1][val2] == 1)then

        image.isVisible = true

    else

        image.isVisible = false

    end

end

timer.performWithDelay(1000,function() count = count +1; showOrNot(count) end,60)

function scene:createScene(event) 

     local group = self.view

    

     background = display.newImage( “Background.png”, true)

     background.x = display.contentWidth / 2

     background.y = display.contentHeight / 2

    

     group:insert(background)

end

function scene:enterScene(event)

     local group = self.view

end

function scene:exitScene( event )

     

     local group = self.view

    

end

function scene:destroyScene( event )

     

     local group = self.view

    

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

[lua]

ok, if you want them to be shown directly, just handle the placement with a for loop outside of your timed loop like this

[lua]

local myMap = display.newGroup()

for a=1, #mapArray do

    for b=1, #mapArray[a] do

        local image = display.newImage(“BlueSquare.png”)

        local positionX = (b - 1) * image.width

        local positionY = (a - 1) * image.height

        image.x, image.y = positionX, positionY

        myMap:insert(image)

    end

end

[/lua] 

thats the way you iterate through a 2 dimensional array (or in lua’s case: table).

with that loop you end up having 110 instances of your BlueSquare.png on the screen.

keep in mind that you won’t be able to access the objects properties via image.x, image.y and so on anymore.

thats because image has become a local variable of the loop and doesnt exist outside it’s scope.

so whenever you want to change an explicit tile you need to use myMap[n] (n representing the tile’s id here, so any number between 1 and 110).

you could of course also put all the images into another 2 dimensional array instead of a display group.

that way their quasi id would stay the same as the quasi id of the mapArray.

that would look like this:

[lua]

local myMapTiles = {}

for a=1, #mapArray do

     

    myMapTiles[a] = {}

    for b=1, #mapArray[a] do

        local image = display.newImage(“BlueSquare.png”)

        local positionX = (b - 1) * image.width

        local positionY = (a - 1) * image.height

        image.x, image.y = positionX, positionY

        myMapTiles[a][b] = image

   end

   

end

[/lua]

example:

now the corresponding displayobject to mapArray[10][5] is myMapTiles[10][5].

so if you want to change that tile’s x position you would do it that way:

myMapTiles[10][5].x = 200 --for example

the beauty of this is that you can iterate through your tiles imageobjects like you did with your array.

in your case:

[lua]

local count = 0

local function showOrNot(number)

    

    local val1 = math.floor(number/10) + 1

    local val2 = number%10

    if(val2 == 0)then val2 = 10 end

    

    if(mapArray[val1][val2] == 1)then

        myMapTiles[val1][val2].isVisible = true

    else

        myMapTiles[val1][val2].isVisible = false

    end

end

timer.performWithDelay(1000,function() count = count +1; showOrNot(count) end,60)

[/lua]

Ok i’m starting to get the hang of it now. but it didn’t link with the array instead places repeating, i think I’ll do some of my own research too :slight_smile: but i am still open for help.

sorry, but i cant figure out what you’re trying to say with that. no offense.

 I dont get when you say “any pictures that aren’t touched from the previous array count as +1 for the score”

but you can go with something like

local count = 0

local mapArray = {the one you’ve made}

local image

local function showOrNot(number)

   local val1 = math.floor(number/10) + 1

   local val2 = number%10

   if(mapArray[val1][val2] == 1)then

      --show the Image

      image = display.newImage(“image.png”)

   elseif(mapArray[val1][val2] == 0)then

      --remove the Image

      image:removeSelf()  

   end     

end

timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60)

the functions seems a lot more complicated since you wanted it to be a two dimensional array :slight_smile:

and also there are a lot of options. you can just adjust the alpha value to hide the image rather than literally destroying it, like above

-Storyboard local storyboard = require ( "storyboard" ) local scene = storyboard.newScene() local count = 0 local mapArray = {{}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}} local image local function showOrNot(number) local val1 = math.floor(number/10) + 1 local val2 = number%10 if(mapArray[val1][val2] == 1)then --show the Image image = display.newImage("BlueSquare.png") elseif(mapArray[val1][val2] == 0)then --remove the Image image:removeSelf() end end timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60) function scene:createScene(event) local group = self.view background = display.newImage( "Background.png", true) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 end function scene:enterScene(event) local group = self.view end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

I’m getting an error, attempt to index up value “image” (a nil value)

first when you construct the array you should just use
local array1 = {
{
 i don’t know if you have used
local array1 = {{},
}
 
your wanting to add a small array in the first index?  
if you’re intending to do this than add a constant value to val1 in the function
  local val1 = math.floor(number/10) + 2
 
 
 
and also  on top of the image:removeSelf()  add if(image)then
so the’ll check if an image exists or not, Im sorry i was in a rush so I left that part out :)  :slight_smile:
 
but on top of that creating and destroying image that frequently is relatively inefficient so I reccomend you do this
 
local image = display.newImage(“BlueSquare”)
local function showOrNot(number)
local val1 = math.floor(number/10) + 1
local val2 = number%10
if(mapArray[val1][val2] == 1)then
image.alpha = 1
else
--since the value is always 1 or 0 let’s just use else
–dont remove the just change the alpha
image.alpha = 0
end
end

or even this will work

local image = display.newImage(“BlueSquare”)
local function showOrNot(number)
local val1 = math.floor(number/10) + 1
local val2 = number%10
if(mapArray[val1][val2] == 1)then
image.isVisible = true
else

image.isVisible = false
end
end

Ok now I’m getting it so i would make 60 arrays for the 60 seconds and use the timer perform with delay right? also using this code i now get an error saying attempt to index field “?” (a nil value)

local count = 0

local mapArray = {},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

               

local image = display.newImage(“BlueSquare.png”)

local function showOrNot(number)

local val1 = math.floor(number/10) + 1

local val2 = number%10

if(mapArray[val1][val2] == 1)then

image.isVisible = true

else

image.isVisible = false

end

end

timer.performWithDelay(1000,function() count = count +1 showOrNot(count) end,60)

[lua]

local count = 0
local mapArray = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}
}
local image = display.newImage(“BlueSquare.png”)

local function showOrNot(number)
    local val1 = math.floor(number/10) + 1
    local val2 = number%10
    
    if(mapArray[val1][val2] == 1)then
        image.isVisible = true
    else
        image.isVisible = false
    end
end

timer.performWithDelay(1000,function() count = count +1; showOrNot(count) end,60)

[/lua]

try this, i havent checked it tho, but it should work.

the nil value comes from your first check of showOrNot with a count of 1,

your first array inside your mapArray is empty:
[lua]

local mapArray = {}, – << THIS empty {} is your first array

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 1, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

[/lua]

so, your first row inside mapArray is an empty array (nil), in conclusion: there is no second dimension but if(mapArray[val1][val2] == 1)  is trying trying to index one. that leads to the error.

oh i found another problem

[lua]

local val2 = number%10

[/lua]

as soon as your number will have no remainder, your val2 will be 0,
so each time number hits 10, 20, 30 and so on, your indexing a table with 0, and lua tables always start with 1.

if you would print your values you would see that after checking
mapArray[1][9]
you are looking for
mapArray[1][0]
instead of [1][10]

i would fix that with an if quick and dirty
[lua]
local function showOrNot(number)
    local val1 = math.floor(number/10) + 1
    local val2 = number%10
    if(val2 == 0)then val2 = 10 end --here we go
    
    if(mapArray[val1][val2] == 1)then
        image.isVisible = true
    else
        image.isVisible = false
    end
end

[/lua]

ok thanks, my games starting to piece together now, i got some errors fixed and used your code and it works fine, problem is that the pictures don’t appear where they should, all i get is 1 square in the top left corner, yes my background is inserted into a group.