I’m building a children’s book that has a page turn effect. This is to be viewed in landscape mode only.
The pages are loaded into two arrays, “oddPgs” and “evenPgs”. The first two pages are loaded at start up with the right page (page 1, the cover) loaded into the “oddPgs” array at position 1 and being displayed on the right half of the screen. The left page (page 2) is loaded into the “evenPgs” array at position 1 and displayed at the far left of the screen with a width of 0.
When page 1 is clicked it transitions to a width of 0 while page 2 transitions to the left half of the screen while it “unfurls” to it’s full width. The next 2 pages are loaded into the “oddPgs” and “evenPgs” arrays.
Once 4 pages have been loaded I begin removing the pages that are no longer needed. These would be the even page “evenPgs[2]” below the currently displayed even page and the odd page “oddPgs[3]” that has been made transparent.
My problem is i get the error “WARNING: Attempting to set property(2) with nil” when I try to give “evenPgs[2]” a nil value. Everything else seems to work fine. I have read and re-read the section on using arrays but do not understand why the lines:
oddPgs:remove(oddPgs[3])
oddPgs[3] = nil
works but the lines:
oddPgs:remove(evenPgs[2])
evenPgs[2] = nil
gives an error.
Here is the complete code:
-- Load the relevant LuaSocket modules (no additional files required for these)
local http = require("socket.http")
local ltn12 = require("ltn12")
-- load external libraries (should be in the same folder as main.lua) --------------------
local sprite = require("sprite")
--local animate = require("animations")
display.setStatusBar( display.HiddenStatusBar )
-- Page Flip Variables -------------------------------------------------------------------
local pgTurn = "page-flip-2.mp3" -- Sound of page turning
local turnSpeed = 800
local fullH = display.contentHeight
local fullW = display.contentWidth
local halfW = display.contentWidth / 2
-- Add Display Groups ----------------------------------------------------------
local oddPgs = display.newGroup()
local evenPgs = display.newGroup()
local aniBtns = display.newGroup()
local foreground = display.newGroup()
-- Parse images into page array--------------------------------------------------
local pages = {"Page01.jpg", "Page02.jpg", "Page03.jpg", "Page04.jpg", "Page05.jpg", "Page06.jpg", "Page07.jpg", "Page08.jpg", "Page09.jpg", "Page10.jpg", "Page11.jpg", "Page12.jpg", "Page13.jpg", "Page14.jpg", "Page15.jpg", "Page16.jpg", "Page17.jpg", "Page18.jpg", "Page19.jpg", "Page20.jpg", "Page21.jpg", "Page22.jpg", "Page23.jpg", "Page24.jpg", "Page25.jpg", "Page26.jpg", "Page27.jpg", "Page28.jpg", "Page29.jpg", "Page30.jpg", "Page31.jpg", "Page32.jpg", "Page33.jpg", "Page34.jpg", "Page35.jpg", "Page36.jpg", "Page37.jpg", "Page38.jpg", "Page39.jpg", "Page40.jpg", "Page41.jpg", "Page42.jpg", "Page43.jpg", "Page00.jpg"}
local image = {} -- Create the image array
local ttlPgs = #pages -- Get the total number of objects in the array
-- Add 1st even pags to display ---------------------------------------------------------
image[2] = display.newImage(pages[2], fullW, 0) -- Display 1st Even page
evenPgs:insert(1, image[2])
image[2]:setReferencePoint(display.TopLeftReferencePoint) -- Use the top left reference
image[2].x = fullW
image[2].y = 0
image[2].xScale = 0.00001 -- Set the width to 0
-- Add 1st odd pags to display ---------------------------------------------------------
image[1] = display.newImage(pages[1], halfW, 0) -- Display 1st Odd page
oddPgs:insert(1, image[1])
image[1]:setReferencePoint(display.TopLeftReferencePoint) -- Use the top left reference for positioning
image[1].x = halfW
image[1].y = 0
-- Page Flip Functions -------------------------------------------------------------------
local page = 1
-- Turn Page -----------------------------------------------------------------------------
function nextPage()
if ( page \< ttlPgs - 1) then -- If page is not last page
media.playSound( pgTurn ) -- Sound of page turning
transition.to( oddPgs[1], { xScale=0, x=halfW, time=turnSpeed } ) -- Furl the odd page
transition.to( evenPgs[1], { xScale=1, x=0+(page\*15), time=turnSpeed} ) -- Unfurl even page "(page\*15)" is for testing
transition.to( oddPgs[1], { alpha = 0, time = 0, delay=turnSpeed} ) -- Make odd page transparent (Corona bug scale = 0 doesn't actually make the width 0)
-- Load Next 2 Pages --------------------------------------------------------------
page = page + 2
-- Add next even page
image[page + 1] = display.newImage(pages[page + 1], fullW, 0) -- Display Even page
image[page + 1]:setReferencePoint(display.TopLeftReferencePoint) -- Use the top left reference for positioning
image[page + 1].xScale = 0.00001 -- Set the width to 0
evenPgs:insert( 1, image[page + 1] ) -- Insert it into the evenPgs array
evenPgs:insert( evenPgs[2] ) -- Move it to the begining of the array (index 1)
-- Add next odd page
image[page] = display.newImage(pages[page], halfW, 0) -- Display Odd page
image[page]:setReferencePoint(display.TopLeftReferencePoint) -- Use the top left reference
oddPgs:insert( 1, image[page] ) -- Insert it into the oddPgs array
-- Remove Previous 2 Pages --------------------------------------------------------
if(evenPgs.numChildren == 4) then
-- Remove even page that was viewed prior to the current page being displayed
evenPgs:remove(evenPgs[2]) -- Remove the even page below the one crrently being displayed from the evenPgs array
evenPgs[2] = nil -- Remove it from memory
end
-- Remove odd page that was viewed prior to the current page being displayed
oddPgs:remove(oddPgs[3]) -- Remove the odd page below the one crrently being displayed from the oddPgs array
oddPgs[3] = nil -- Remove it from memory
end
end
-- Page Turn Event -----------------------------------------------------------------------
local function printTouch2( event )
if event.phase=="began" then
if event.x \> 600 then
if ( page \< ttlPgs - 1) then
nextPage()
else
-- End of book
end
end
if event.x \< 400 then
prevPage()
end
end
end
Runtime:addEventListener( "touch", printTouch2 )
I’ve been struggling with this for several hours to no avail. My understanding is that when you put a new object into an array at index one, the indexs of all the rest of the objects in the array are incremented by one. Is this not true?
Thanks in advance! [import]uid: 6397 topic_id: 1517 reply_id: 301517[/import]
[import]uid: 6928 topic_id: 1517 reply_id: 4522[/import]