Hey, I have a timer that triggers the spawnObject function, which determines if it should spawn 1,2 or 3 items (To save space I only posted one of the three.) However sometimes it works, but most of the time the object will spawn at 0x instead of 1000x on the x axis. It will spawn at 0x then move instantly to 1000x, but I need it to only spawn at 1000x.
What is weird, sometimes it works fine. I cant figure out if its a timer issue, or if its an issue creating the object.
local function removeObject(thisObject) display.remove( thisObject ) thisObject = nil end local function generateItem() local chance = math.random(100) if chance \< 4 then -- name, damage, stackable item = {"cheese", 0, "yes"} else if chance \< 7 then item = {"switchIcon", 0, "yes"} else if chance \< 63 then item = {"apple", 0, "yes"} else if chance \< 93 then item = {"crate", 50, "no"} else if chance \< 101 then item = {"steelCrate", 400, "no"} end end end end end end
local function spawnRandomThree() generateItem() local object = createObject(item[1],item[2],1000) generateItem() local object2 = createObject(item[1],item[2],1000) object.y = lanes[1] object.Stackable = item[3] object.x = transition.to( object, { time=travelTime, x= -100, y = object.y, onComplete=removeObject} ) object2.y = lanes[2] object2.Stackable = item[3] object2.stackable = item.stackable object2.x = transition.to( object2, { time=travelTime, x= -100, y = object2.y, onComplete=removeObject} ) if object.Stackable == "no" and object2.Stackable == "no" then repeat generateItem() until item[3] ~= "no" end local object3 = createObject(item[1],item[2],1000) object3.y = lanes[3] object3.x = transition.to( object3, { time=travelTime, x= -100, y = object3.y, onComplete=removeObject} ) objectGroup:insert(object) objectGroup:insert(object2) objectGroup:insert(object3) playerGroup:toFront() hudGroup:toFront() end
local function spawnObject() local rNumber = math.random(99+difficultyChance) if (rNumber) \< 35 then spawnRandomOne() else if (rNumber) \< 70 then spawnRandomTwo() else spawnRandomThree() end end end
local function advancedBtnRelease(event) if ( "ended" == event.phase ) then currentMode = "advancedMode" difficultyChance = 30 travelTime = 3000 restartBtn.isVisible= true Var.health = Var.healthMax healthCounter.text = "Health: "..Var.health advancedTimer = timer.performWithDelay( 650, spawnObject, 0 ) kidsBtn.isVisible = false easyBtn.isVisible = false advancedBtn.isVisible = false end physics.start() playerMoveIn() return true -- indicates successful touch end
-----------------------------------Below is object.lua------------------------------------------------------------------------------
function createObject(id,damage,objectX) local object if Properties[id] then objectProperties = Properties[id] end if objectProperties then local sheetInfo = require("sprites." .. objectProperties.sprite.imageSheetInfo) local imageSheet = graphics.newImageSheet("sprites/" .. objectProperties.sprite.imageSheet, sheetInfo:getSheet()) local sequenceData = {} for sequence in pairs(objectProperties.sprite.sequenceData) do --DEBUG --print(sequence) local insertSequence = {} --REQUIRED DATA insertSequence.name = sequence insertSequence.sheet = imageSheet local tableData = objectProperties.sprite.sequenceData[sequence] --OPTIONAL DATA if(tableData.start)then insertSequence.start = sheetInfo:getFrameIndex(tableData.start) --DEBUG --print(insertSequence.start) end if(tableData.count)then insertSequence.count = tableData.count --DEBUG --print(insertSequence.count) end if(tableData.time)then insertSequence.time = tableData.time --DEBUG --print(insertSequence.time) end if(tableData.loopCount)then insertSequence.loopCount = tableData.loopCount --DEBUG --print(insertSequence.loopCount) end table.insert(sequenceData, insertSequence) end object = display.newSprite(imageSheet, sequenceData) object.x = objectX object:setSequence("default") end