我編碼在Lua與科羅娜SDK和我在得到這個錯誤「試圖指數全球‘前’(一個零值)」 75號線試圖指數全球「前」(一個零值)
我在game.lua中執行此操作,從main.lua重定向(此部分工作正常) ....鏈接是pastebin,因爲stackoverflow混淆了代碼!
--requires
local storyboard = require "storyboard"
local scene = storyboard.newScene()
local physics = require "physics"
physics.start()
function scene:createScene(event)
local screenGroup = self.view
local bkg = display.newImage("bkg.png")
local back = display.newImage("back.png")
back.y=450
back.speed = 1
back:setReferencePoint(display.BottomLeftReferencePoint)
local back1 = display.newImage("back.png")
back1.y=450
back1.x=2400
back1.speed = 1
back1:setReferencePoint(display.BottomLeftReferencePoint)
local front = display.newImage("front1.png")
front.y=470
front.speed = 4
front:setReferencePoint(display.BottomLeftReferencePoint)
local front1 = display.newImage("front1.png")
front1.y=470
front1.x=2400
front1.speed = 4
front1:setReferencePoint(display.BottomLeftReferencePoint)
local bird = display.newImage("bird.png")
bird.y = 285; bird.x= 200
physics.addBody(bird, "dynamic",
{density=0.1, bounce=0.1, friction=0.2, radius=10})
end
function scrollHill(self,event)
if self.x < -1800 then
self.x = 1400 - self.speed * 2
else
self.x = self.x - self.speed
end
end
function activateBird(self,event)
self:applyForce(0, -1.5, self.x, self.y)
end
function touchScreen(event)
print("touch_ok")
if event.phase =="began" then
bird.enterFrame = activateBird
Runtime:addEventListener("enterFrame", bird)
end
if event.phase =="ended" then
Runtime:removeEventListener("enterFrame", bird)
end
end
function scene:enterScene(event)
Runtime:addEventListener("touch", touchScreen)
front.enterFrame = scrollHill
Runtime:addEventListener("enterFrame", front)
front1.enterFrame = scrollHill
Runtime:addEventListener("enterFrame", front1)
back.enterFrame = scrollHill
Runtime:addEventListener("enterFrame", back)
back1.enterFrame = scrollHill
Runtime:addEventListener("enterFrame", back1)
end
function scene:exitScene(event)
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
儘量讓你的變量(前,FRONT1等),全球因爲你是從兩個不同的函數訪問它們。 –
謝謝你,大師!我讓他們全球化,現在完美! – user2735374