我希望有人可以帮助我,因为这个问题让我在过去的几天里疯狂了。所以我刚开始使用电晕 - 制作了一些教程,并且真正发现了可能性。 问题:我想将一个简单的游戏集成到故事板中。我somehonw无法找到正确的组合,总是会弹出这个错误。
File: assertion failed!
Assertion failed!
stack traceback:
[C]: ?
[C]: in function 'assert'
?: in function 'get0rCreateTable'
?: in function 'addEventListener'
?: in function 'addEventListener'
...ik/Desktop/_DummyProjekt2/Fruit Fliesv10/level01.lua:51: in function
<. . . ik/Desktop/_DummyProjekt2/ Fruit Fliesv10/level0l. lua:44>
?: in function 'dispatchEvent'
?: in function <?:1096>
(tail call): ?
?: in function <?:466>
?: in function <?:218>
这可能是一个新手的错误,但我已经被困了几天,肯定需要一些帮助。我已经通过了大部分的教程和文档,我找不到运气。所以任何帮助都会受到欢迎:)所以这是我的等级1
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require ("widget")
local playfile = require ("play")
function scene:createScene(event)
local screenGroup = self.view
local spawnEnemy
local gameTitle
local scoreTxt
local score = 0
local hitBowl
local planet
local speedBump = 0
background = display.newImage("images/background.png")
background.y = centerY
background.x = centerX
bowl = display.newImage("images/bowl2.png")
bowl.x = centerX
bowl.y = centerY
scoreTxt = display.newText( "Score: 0", 0, 0, "orange juice", 22 )
scoreTxt.x = centerX
scoreTxt.y = 10
scoreTxt:setFillColor(1,1,1)
scoreTxt.y = 255
end
function scene:enterScene(event)
enemypics = {"images/blue.png","images/pink.png", "images/green.png"}
enemy = display.newImage(enemypics[math.random (#enemypics)])
enemy:addEventListener ( "tap", shipSmash )
if math.random(2) == 1 then
enemy.x = math.random ( -100, -10 )
else
enemy.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 )
enemy.xScale = -1
end
enemy.y = math.random (display.contentHeight)
enemy.trans = transition.to ( enemy, { x=centerX, y=centerY, time=math.random(2500-speedBump, 4500-speedBump), onComplete=hitBowl } )
speedBump = speedBump + 50
end
function startGame()
text = display.newText( "Tap here to start. Protect the Fruit Bowl!", 0, 0, "orange juice", 24 )
text.x = centerX
text.y = display.contentHeight - 30
text:setFillColor(0, 0, 0)
local function goAway(event)
display.remove(event.target)
text = nil
display.remove(gameTitle)
spawnEnemy()
scoreTxt.alpha = 1
scoreTxt.text = "Score: 0"
score = 0
bowl.numHits = 10
bowl.alpha = 1
speedBump = 0
end
text:addEventListener ( "tap", goAway )
end
local function bowlDamage()
bowl.numHits = bowl.numHits - 2
bowl.alpha = bowl.numHits / 10
if bowl.numHits < 2 then
bowl.alpha = 0
timer.performWithDelay ( 1000, startGame )
--audio.play ( sndLose )
else
local function goAway(obj)
bowl.xScale = 1
bowl.yScale = 1
bowl.alpha = bowl.numHits / 10
end
transition.to ( bowl, { time=200, xScale=1.2, yScale=1.2, alpha=1, onComplete=goAway} )
end
end
function hitBowl(obj)
display.remove( obj )
bowlDamage()
--audio.play(sndBlast)
if bowl.numHits > 1 then
spawnEnemy()
end
end
local function shipSmash(event)
local obj = event.target
display.remove( obj )
--audio.play(sndKill)
transition.cancel ( event.target.trans )
score = score + 5
scoreTxt.text = "Score: " .. score
spawnEnemy()
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
答案 0 :(得分:0)
所以LUA在很多方面很有趣,这是你很快就会学到的。所以我只认为这可能是问题,如果不是它可能会引导你走上正确的道路。
在您的热门错误消息中,您会看到它为您提供了以下信息:
...ik/Desktop/_DummyProjekt2/Fruit Fliesv10/level01.lua:51: in function
level01.lua:51
表示行号,因此第51行(或其周围)。错误上面的行提到了addEventListener。这两件事是我们的线索。
这里有一些代码:
enemy = display.newImage(enemypics[math.random (#enemypics)])
enemy:addEventListener ( "tap", shipSmash )
if math.random(2) == 1 then
看起来那里有一个addEventListener,一个参数是一个字符串,另一个是函数。那个功能在哪里?它位于117
之后,它位于51
之后,这很可能是问题所在。在LUA(以及其他一些语言)中,您必须按顺序声明事物。所以你可以做两件事。您可以将shipSmash
函数移到第51
行以上,也可以转发声明该变量。因此,您可以在文件的顶部执行local shipSmash
。这确保了该名称存在的行51
(它正在尝试检查的内容),然后您可以稍后声明它(有时我会为代码结构执行此操作)。
希望这会有所帮助。我从7月开始就一直在使用LUA / Corona SDK,我仍然习惯了很多这样的小事,我花了好几个小时把头发拉过来。