i just started learning corona/lua yesterday, so i’m not exactly sure how to fix this. thanks for any and all help.
i finished the chapter one tutorial (the balloon one) and wanted the player to be able to collect stars to earn points. here is my code. i’m sorry there’s no tabs, i can’t seem to paste from sublime text to here with spaces.
if anyone knows how to fix that let me know.
[lua]
–balloon tap
– images
local sky = display.newImageRect(“myassets/skywatercolour.jpg”, 360, 570)
sky.x = display.contentCenterX
sky.y = display.contentCenterY
sky.id = “sky”
local balloon = display.newImageRect(“myassets/hotairballoon.png”, 300, 200)
balloon.x = display.contentCenterX
balloon.y = display.contentHeight - 170
balloon.id = “balloon”
local platform = display.newImageRect(“myassets/platform.png”, 230, 40)
platform.x = display.contentCenterX
platform.y = display.contentHeight
platform.id = “platform”
local pushes = 0
local score = display.newText("score: "…tostring(pushes), display.contentWidth -80, -10, native.systemFont, 25)
– physics
local physics = require(“physics”)
physics.start()
physics.addBody(platform, “static”, {bounce = 0.5})
physics.addBody(balloon, “dynamic”, {radius=76, bounce=0})
– walls
topWall = display.newRect(display.contentCenterX, -50, display.contentWidth, 2)
leftWall = display.newRect(0, 0, 2, display.contentHeight*2)
bottomWall = display.newRect(0, display.contentHeight*2, display.contentWidth*2, 2)
rightWall = display.newRect(display.contentWidth, 0, 2, display.contentHeight*2)
topWall.id = “topWall”
leftWall.id = “leftWall”
bottomWall.id = “bottomWall”
rightWall.id = “rightWall”
physics.addBody(topWall, “static”)
physics.addBody(leftWall, “static”)
physics.addBody(bottomWall, “static”)
physics.addBody(rightWall, “static”)
– functions
local function pushBalloon()
balloon:applyLinearImpulse(0, -1, balloon.x, balloon.y)
pushes = pushes + 1
score.text = "score: "…tostring(pushes)
end
local newStar
local stars = {}
local starAssets = {“myassets/star1.png”, “myassets/star2.png”, “myassets/star3.png”}
leftAndRight = {0, display.contentWidth}
local function spawnStar()
newStar = display.newImageRect(starAssets[math.random(#starAssets)], 20, 20)
table.insert(stars, newStar)
newStar.id = “star”…#stars
physics.addBody(newStar, “kinematic”, {bounce = 0})
newStar.x = leftAndRight[math.random(#leftAndRight)]
newStar.y = math.random(20, display.contentHeight - 200)
if (newStar.x == 0) then
newStar:setLinearVelocity(math.random(display.contentWidth/10, display.contentWidth-15), math.random(0, display.contentHeight/4))
elseif (newStar.x == display.contentWidth) then
newStar:setLinearVelocity(math.random(-display.contentWidth-15, -display.contentWidth/10), math.random(-display.contentHeight/4, 0))
end
newStar:applyTorque(math.random(-6, 6))
end
local function onCollision(self, event)
if (event.phase == “began”) then
local object = event.other.id
if string.match(object, “star”) then
pushes = pushes + 2
display.remove(event.other)
table.remove(stars, tonumber(string.sub(object, -1)))
elseif (object == “platform”) then
pushes = 0
end
score.text = "score: "…tostring(pushes)
end
end
– events
balloon.collision = onCollision
balloon:addEventListener(“collision”)
balloon:addEventListener(“tap”, pushBalloon)
– game loop
local function gameLoop()
if (#stars < 10) then
spawnStar()
end
for i = #stars, 1, -1 do
local currStar = stars[i]
– local xpos = tonumber(currStar.x)
– local ypos = tonumber(currStar.y)
– local width = tonumber(display.contentWidth)
– local height = tonumber(display.contentHeight)
print(type(currStar.x))
print(type(currStar.y))
print(type(display.contentWidth))
print(type(display.contentHeight))
if (currStar.x < -20) or (currStar.x > display.contentWidth + 20) or (currStar.y < -20) or (currStar.y > display.contentHeight + 20) then
display.remove(currStar)
table.remove(stars, i)
end
end
end
loopTimer = timer.performWithDelay(500, gameLoop, 0)
[/lua]
i get the error Attempt to compare nil with number stack traceback on line 106 or
[lua] if (currStar.x < -20) or (currStar.x > display.contentWidth + 20) or (currStar.y < -20) or (currStar.y > display.contentHeight + 20) then [/lua]
as you can see, i checked the type of all the variables and they’re all numbers. i tried creating new variables hat are numbers and the same error occurred.
am i reading the error message wrong? is there a problem elsewhere? thank you for any help