Help with arrays with timers?

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.