Domanda

I am new to the site and programming in this language... I have gotten a basic understanding of the language and I am having this error:

assertion failed!
stack back:
[C]: ?
[C]: in function 'assert'
?: in function 'getOrCreateTable'
?: in function 'addEventListener'

and there are more lines, but you get the picure.

this is my code that is located in the area that it said to go:

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

I have no idea of what went wrong... this code is there so if I click on said image it will do what I want... but I am getting an error in the how_img:addEvent.. area!

if any Q's I will be happy to answer them!

here is the whole code:

 -- 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
È stato utile?

Soluzione

There is no Object called how_fun.

You should change:

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

to

function how:tap( event )
    local how_img = display.newImage("how.png",0,0)
    how_img:addEventListener("tap" ,home_switch)
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top