首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lua中的tap方法不起作用

lua中的tap方法不起作用
EN

Stack Overflow用户
提问于 2013-09-17 22:54:44
回答 1查看 139关注 0票数 1

我对这个网站和语言编程都很陌生.我对这门语言有了基本的理解,我犯了这个错误:

代码语言:javascript
复制
assertion failed!
stack back:
[C]: ?
[C]: in function 'assert'
?: in function 'getOrCreateTable'
?: in function 'addEventListener'

还有更多的台词,但你可以看到照片。

这是我的代码,位于它说要去的区域:

代码语言:javascript
复制
title:addEventListener("tap",title)
how:addEventListener("tap",how_fun)

function how_fun:tap( event )
    local how_img = display.newImage("how.png",0,0)
    how_img:addEventListener("tap" ,home_switch)
end

local function home_switch( event )
    local title = display.newImage(mainGroup, "title.png",0,0)
    local how = display.newImage(mainGroup, "how_button.png",0,0)
end

我不知道哪里出了问题..。这个代码就在那里,所以如果我点击上面的图像,它会做我想做的.但我在how_img:addEvent中有个错误..。区域!

如果有任何问题,我会很乐意回答他们!

以下是整个代码:

代码语言:javascript
复制
 -- Hide status bar
 display.setStatusBar(display.HiddenStatusBar)

 -- main game group
 mainGroup = display.newGroup()

 local title = display.newImage(mainGroup, "title.png",0,0)
 local how = display.newImage(mainGroup, "how_button.png",70,200)

 function title:tap()
-- sets settings for cats
local cat_health = 10
local cat_hunger = 10
local cat_voice = 10
local cat_hygiene = 10
local your_score = 0

local cat = display.newImage(mainGroup, "bg.png",0,0)

local food = display.newImage(mainGroup, "food.png",0,0)

local bath = display.newImage(mainGroup, "bath.png",200,0)

local hunger = display.newText(mainGroup,"Hunger "..cat_hunger.."/10",display.contentWidth/2+15,display.contentHeight-40, native.systemFont, 18)
hunger:setTextColor(255,255,255)

local Score = display.newText(mainGroup,"Your Score: "..your_score,20,display.contentHeight-40, native.systemFont, 18)
Score:setTextColor(255,255,255)

local health = display.newText(mainGroup,"Health "..cat_health.."/10",display.contentWidth/2+15,display.contentHeight-65, native.systemFont, 18)
health:setTextColor(255,255,255)

local hygiene = display.newText(mainGroup,"hygiene "..cat_hygiene.."/10",20,display.contentHeight-65, native.systemFont, 18)
hygiene:setTextColor(255,255,255)

function food:tap( event )
    if (cat_hunger<10) then
        cat_hunger = cat_hunger+1
        hunger.text = "Hunger "..cat_hunger.."/10"
    end
    return true
end

local function trigger()
    if (cat_hunger>0) then
        cat_hunger = cat_hunger-1
    end
    if (cat_hygiene>0) then
        cat_hygiene = cat_hygiene-1
    end
    hunger.text = "Hunger "..cat_hunger.."/10"
    hygiene.text = "Hygiene "..cat_hygiene.."/10"
    return true
end

local function your_score_func()
    your_score = your_score+1
    Score.text = "Your Score: "..your_score
end

local function health_func( event )
    if (cat_hunger==2) then
        cat_health = cat_health-1
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_hunger==1) then
        cat_health = cat_health-2
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_hunger==0) then
        cat_health = cat_health-3
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_hygiene==2) then
        cat_health = cat_health-1
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_hygiene==1) then
        cat_health = cat_health-2
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_hygiene==0) then
        cat_health = cat_health-3
        health.text = "Health: "..cat_health.."/10"
    end
    if (cat_health<1) then
        local bkgd = display.newImage("over_bg.png",0,0)
    end
    return false
end

function bath:tap( event )
    if (cat_hygiene<10) then
        cat_hygiene = cat_hygiene+1
        hygiene = "hygiene "..cat_hygiene.."/10"
    end
    return true
end

timer.performWithDelay(4000, trigger,0)
timer.performWithDelay(2000, your_score_func,0)
timer.performWithDelay(3000, health_func,0)
bath:addEventListener("tap",bath)
food:addEventListener("tap",food)
timer.performWithDelay(1000, music_loop)

local function music_loop( event )
audio.play(main_mus)
timer.performWithDelay(1000, music_loop,0)
end
 end

 title:addEventListener("tap",title)
 how:addEventListener("tap",how_fun)

function how_fun:tap( event )
local how_img = display.newImage("how.png",0,0)
how_img:addEventListener("tap" ,home_switch)
 end

 local function home_switch( event )
local title = display.newImage(mainGroup, "title.png",0,0)
local how = display.newImage(mainGroup, "how_button.png",0,0)
 end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-17 23:38:00

没有名为how_fun的对象。

你应该改变:

代码语言:javascript
复制
function how_fun:tap( event )
    local how_img = display.newImage("how.png",0,0)
    how_img:addEventListener("tap" ,home_switch)
end

代码语言:javascript
复制
function how:tap( event )
    local how_img = display.newImage("how.png",0,0)
    how_img:addEventListener("tap" ,home_switch)
end
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18861208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档