Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

i copied a word to word code but still get an error
Started by oceanisdeep95 Aug 27 2018 04:16 PM

- - - - -
1 reply to this topic
code error help

Best Answer Rob Miracle , 28 August 2018 - 06:37 AM

What is line 64?

Looking in the console log window, are there any other error or warning messages like an image not found?

 

I suspect this is the line of code:

Runtime:addEventlistener( "enterFrame", balloon )

Lua is a case-sensitive language. There is no API call named "addEventlistener", but there is "addEventListener". Notice the difference in capitalization?

 

Rob

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

oceanisdeep95

[GLOBAL: userInfoPane.html]
oceanisdeep95
  • Observer

  • 2 posts
  • Corona SDK

i was following a guide of a youtuber and copied all of his code but i keep getting an error in the end 

 

main.lua:64:attempt to call method 'addEventlistener' (a nil value)

 

stack traceback:

 

main.lua:64: in function 'addNewBalloonOrBomb'

main.lua:70: in main chuck 

 

 

plz help this was my first time coding and i have no idea whats going on 

 

 

code 

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
 
-- Your code here
local physics = require( "physics" )
physics.start()
 
-- start physics engine 
 
halfw = display.contentWidth*0.5
halfh = display.contentHeight*0.5
 
--set location of halfw2 at 0.5 of screen width 
-- set location of halfh at 0.5 of screen height both at the center 
 
local bkg = display.newImage("sky.jpg", halfw, halfh)
-- loads the image sky into the backgorund 
score = 0
scoretext = display.newText(score, halfw ,10)
-- sometimes its better to rewrite the code
--displays a score of zero on the screen 
local function balloonTouched(event)
  if ( event.phase == "began" ) then
    Runtime:removeEventListener( "enterFrame", event.self )
    event.target:removeSelf()
    score = score + 1
    scoreText.text = score
  end
end
 
local function bombTouched(event)
  if ( event.phase == "began" ) then
    Runtime:removeEventListener( "enterFrame", event.self )
    event.target:removeSelf()
    score = math.floor(score * 0.5)
    scoreText.text = score
  end
end
 
local function offscreen(self, event)
  if(self.y == nil) then
    return
  end
  if(self.y > display.contentHeight + 50) then
  Runtime:removeEventListener( "enterFrame", self )
  self:removeSelf()
end 
end
-- hmmmmmmmm
local function addNewBalloonOrBomb()
  local startX = math.random(display.contentWidth*0.1,display.contentWidth*0.9)
  if(math.random(1,5)==1) then
    -- BOmb!
  local bomb = display.newImage( "bomb.jpg", startX, -300)
    physics.addBody( bomb )
    bomb.enterFrame = offscreen
    Runtime:addEventListener( "enterFrame", bomb )
    bomb:addEventListener( "touch", bombTouched )
  else
    -- Balloon
    local balloon = display.newImage( "red_balloon.jpg", startx, -300)
    physics.addBody( balloon )
    balloon.enterFrame = offscreen
    Runtime:addEventlistener( "enterFrame", balloon )
    balloon:addEventListener( "touch", balloonTouched )
  end
end
 
    
addNewBalloonOrBomb()
timer.performwithdelay( 500, addNewBalloonOrBomb, 0 )
 
 
 
 

Attached Thumbnails

  • red_balloon.jpg
  • bomb.jpg


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,263 posts
  • Enterprise

  Best Answer

What is line 64?

Looking in the console log window, are there any other error or warning messages like an image not found?

 

I suspect this is the line of code:

Runtime:addEventlistener( "enterFrame", balloon )

Lua is a case-sensitive language. There is no API call named "addEventlistener", but there is "addEventListener". Notice the difference in capitalization?

 

Rob




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: code, error, help