Hi, every one! I am new to corona. I am trying to make adventure game using corona storyboard API. I managed to create two scenes and transition beetwen them. But now I face with inventory. I found some tutorials and do all accordingly them, But I got troubles.
I write a functions for display inventory objects, add and remove thne but when I trying to add a handler for drag and drop then I got en error
Runtime error d:\corona\firsttry\inventory.lua:126: attempt to index local ‘itemInSlot’ (a nil value)
Here is my code:
scene.lua
[lua]require(“inventory”)
local storyboard = require(“storyboard”)
local ui = require(“ui”)
local scene = storyboard.newScene()
local back, tap1, tap2, tap3, textMemory
– onCreate
function scene:createScene(event)
local group = self.view
local function GoToMenu(event)
storyboard.gotoScene(true, “menu”, “fade”, 500)
end
back = display.newImageRect(“img/Scene1.png”, 1024, 768);
back.x = display.contentCenterX;
back.y = display.contentCenterY;
back.alpha = 0.9;
group:insert(back);
tap2 = ui.newButton
{
default = “img/tap2.png”,
over = “img/tap2.png”,
onRelease = GoToMenu,
}
tap2.x = display.contentCenterX;
tap2.y = 650;
group:insert(tap2)
textMemory = display.newText(“Memory:”, 0, 0, native.SystemFont, 32)
textMemory:setTextColor(0)
textMemory.x = 200
textMemory.y = 200
group:insert(textMemory)
end
–Draw
function scene:enterScene(event)
local group = self.veiw
– if storyboard.prevScene > 0 then storyboard.removeScene(“scene”…storyboard.prevScene) end-- ??? ?? ??? ??? ???
storyboard.purgeScene( “menu” )
– storyboard.prevScene = 1;
– storyboard.removeScene( “menu” );
local showMem = function()
–text3.isVisible = true
textMemory.text = textMemory.text … collectgarbage(“count”)/1000 … “MB”
textMemory.x = display.contentWidth * 0.5
end
memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
–exit
function scene:exitScene(event)
local group = self.veiw
end
–destroy
function scene:destroyScene(event)
local group = self.view
end
addItemToInvetory(1,1,true)
–displayInventory()
scene:addEventListener(“createScene”,scene)
scene:addEventListener(“enterScene”,scene)
scene:addEventListener(“exitScene”,scene)
scene:addEventListener(“exitScene”,scene)
return scene[/lua]
inventory.lua
–Inventory
[lua]local iconAcross – number of icons to display
local iconWidth – icon width
local iconHeigth – iconHeigth
local offsetX, offsetY – the position offset where the icons would be
local colNum, rowNum – the row and column from the inventory slot
local slotNumber – number of slots
local items =
{
“bag.png”,
“lamp.png”,
“glasses.png”
}
local slots = 8 – number of slots
local inventoryBag = {nil,nil,nil,nil,nil,nil,nil,nil} – this where the items are stored
–The local functions that are used to manage Inventory
local addItemToInventory
local removeItemFromInventory
local saveInventory
local loadInventory
local displayInventory
local function printTouch( event )
if event.target then
local bounds = event.target.contentBounds
– print( “event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
end
end
local function onTouch(event)
local t = event.target – ??? ??? ? ???
local phase = event.phase
if “began” == phase then
– make target top object
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
– check if player tap on the object
t.isFocus = true
– store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
end
end
return true
end
–for i = 1, 2 do
– local item = display.newImage(items[i], true)
– item:addEventListener(“touch”, onTouch)
– end
function addItemToInvetory(itemIndex, inSlot, redraw)
local itemIndex = itemIndex
local inSlot = inSlot
– local itemInSlot = InventoryBag[inSlot]
local redraw = redraw or false
if inSlot > 8 or inSlot < 1 then return end
– if itemInSlot ~= nil then
– removeFromInventory(itemIndex)
– end
inventoryBag[inSlot] = itemIndex
– inventoryBag[inSlot]:addEventListener(“touch”, onTouch)
if redraw == true then
displayInventory()
end
end
function removeItemFromInventory(itemIndex, redraw)
local i
local redraw = redraw or false
for i = 0, slots do
if inventoryBag[i] == itemIndex then inventoryBag[i] = 0 end
end
if redraw == true then
displayInventory()
end
end
function displayInventory()
– local group = self.view
inventory = display.newImageRect(“img/inventory/inventory.png”, 1024, 768)
inventory.x = 511
inventory.y = 391
inventory.alpha = 0.9
– group:insert(inventory)
local i
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch) end
end
end [/lua]
Does anyone know how to fix it? Or maybe another way to make inventory managment using corona?
Thank you! [import]uid: 216233 topic_id: 35558 reply_id: 335558[/import]