lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Everybody,
 
I am very new to programming.  Currently I am using Corona SDK which uses the Lua language.  I am trying my hand at my first game and have ran into a problems with arrays/tables.  My game game screen is a follows.  I have a background, with a 360 x 216 pixel gameboard with 16 spaces, 16 gamepieces that are located to the left of the board and are draggable, and a back button.
 
What I am trying to do is move the 16 pieces to the gameboard and the game recognize when I have placed a gamepiece on a gamespace and once all 16 pieces are placed, if I have them in the right spot.
 
I was told I should use multidimensional arrays as well as tables.  I don't know how to use either.  Can somebody help me start the code on this?  I just need the coding for beginning to end and then I can replicate that for the other pieces.  I have placed what I have below but I am going to only show one game piece because the code is very long with all 16 pieces.
 
Thanks in advance for any help you can provide.
 

<lua>

module(..., package.seeall)

 

-->main function - MUST return a display.newGroup()

function new()

local localGroup = display.newGroup()

--> This is how we start every single file or "screen" in our folder, except for main.lua

            -- and director.lua

            --> director.lua is NEVER modified, while only one line in main.lua changes, described in that file

------------------------------------------------------------------------------

------------------------------------------------------------------------------

local physics = require ("physics")

physics.start ()

 

-->Background

local background = "" ("blackbackground.png")

localGroup:insert(background)

--> This sets the background

 

-->Gameboard

local gameboard = display.newImage ("gameboard.png")

gameboard.x = 310

gameboard.y = 200

localGroup:insert(gameboard)

-->This sets the gameboard

 

-->Table Array Begins

local gameboard = (1, 2, 3, 4, 

5, 6, 7, 8, 

9, 10, 11, 12, 

13, 14, 15, 16)

 

-->This creates the Table data

 

local redblock = display.newImage ("redblock.png")

localGroup:insert(redblock)

redblock.x = 70

redblock.y = 50

 

local redblockx0, redblocky0

 

local function startDrag( event )

        local redblock = event.target

        local phase = event.phase

        if "began" == phase then

                         

                -- Store initial position

                x0 = event.x 

                y0 = event.y 

                

                redblockx0 = redblock.x

                redblocky0 = redblock.y

                

                print(x0, y0)

          

                -- Stop current motion, if any

                redblock:setLinearVelocity( 0, 0 )

                redblock.angularVelocity = 0

 

        elseif "moved" == phase or "ended" == phase or "cancelled" == phase then

                redblock.x = redblockx0 + event.x - x0

                redblock.y = redblocky0 + event.y - y0

        end

 

 

        -- Stop further propagtion of touch event!

        return true

end

 

        redblock:addEventListener( "touch", startDrag );

              

-->This ends the redblock drag code

 

local backbutton = display.newImage ("backbutton.png")

localGroup:insert(backbutton)

backbutton.x = 75

backbutton.y = 300

backbutton.xScale = .5

backbutton.yScale = .5

 

local function touchedBackbutton (event)

        if ("ended" == event.phase) then

                director:changeScene( "titlescreen", "fade" )   

        end     

end

 

backbutton:addEventListener ("touch", touchedBackbutton)

 

                       

-->MUST return a display.newGroup()

 

 

------------------------------------------------------------------------------

------------------------------------------------------------------------------

           

--> This is how we end every file except for director and main, as mentioned in my first 

 

return localGroup

 

end

</lua>



--
Michelle Jenkins
Ultra Luxury Life