Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Error addEventListener: listener cannot be nil: nil
Started by ConveyedRex7592 Mar 22 2020 05:12 AM

9 replies to this topic
error addeventlistener eventlistener
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

Why I am getting error?

 

What I am doing wrong?

 

local composer = require "composer"
local joystickPlugin = require "plugin.joystick"
local physics = require "physics"
local scene = composer.newScene();
 
function scene:create(event)
  physics.start()
  physics.setGravity(0,0)
  local sceneGroup = self.view;
  system.activate("multitouch")

  local spaceBackground = display.newImageRect("Images/SpaceBackground.png",
  display.actualContentWidth, display.actualContentHeight)
  spaceBackground.x = display.contentCenterX
  spaceBackground.y = display.contentCenterY

  local loading = display.newText("Loading...", display.contentCenterX - 230, 330,
  "Lobster-Regular", 40)

  local map = display.newImage("Images/survivalModeMap.png")
  map.isVisible = false
  map.x = display.contentCenterX
  map.y = display.contentCenterY

  local hero = display.newImage("Images/mehaHat.png")
  hero.isVisible = false
  hero.x = display.contentCenterX
  hero.y = display.contentCenterY
  physics.addBody(hero,"static")
  hero.direction = "up"
  hero.hp = 100

  local hp = display.newText("Hp: "..hero.hp,display.contentCenterX,300,
  "Lobster-Regular", 50)

  local zombie = display.newImage("Images/zombie.jpg")
  zombie.isVisible = false
  zombie.x = display.contentCenterX
  physics.addBody(zombie,"dynamic")
  zombie.y = -100
  zombie.hp = 10
  zombie.phase = "ended"
 
  local function zombieCollision(self, event)

    if event.phase == "began" then

      zombie.phase = "touching"
      timer.performWithDelay( 1, function()
      local function damage()

        if zombie.phase == "touching" then

          if hero.hp > 0 then
            hero.hp = hero.hp - 10
            hp.text = "Hp: "..hero.hp
          end
        end
      end
 
      timer.performWithDelay(1000,damage,-1)
      end)
    elseif event.phase == "ended" then
      zombie.phase = "ended"
    end
  end

  zombie.collision = zombieCollision
  zombie:addEventListener("collision", zombie)

  local AttackButton = display.newImage("Images/mehaAttackButton.png")
  AttackButton.isVisible = false
  AttackButton.x = 480
  AttackButton.y = 270

  local AttackAnimationSheetData = {
    width = 400,
    height = 400,
    numFrames = 10,
    sheetContentWidth = 800,
    sheetContentHeight = 2000
  }

  local AttackAnimationSheet = graphics.newImageSheet("Images/mehaAttackAnimation.png", AttackAnimationSheetData)
  local sequenceData = {
    { name = "Attack", start = 1, count = 10, time = 500, loopCount = 1}
  }

  local props = {
    x = 10,
    y = 270,
    backgroundRadius = 50,
    movedStickRadius = 25,
  }

  function loadingEnd()
    spaceBackground.isVisible = false
    loading.isVisible = false
    map.isVisible = true
    hero.isVisible = true
    zombie.isVisible = true
    AttackButton.isVisible = true
    function enemy(event)

      if zombie.x < display.contentCenterX then
        zombie.x = zombie.x + 1
      end

      if zombie.y > display.contentCenterY then
        zombie.y = zombie.y - 1
      end

      if zombie.x > display.contentCenterX then
        zombie.x = zombie.x - 1
      end

      if zombie.y < display.contentCenterY then
        zombie.y = zombie.y + 1
      end

      zombie.rotation = 90 + math.deg(math.atan2(zombie.y - hero.y, zombie.x - hero.x))

      end
    end
 
    local Joystick = joystickPlugin.newJoystick(props)

    Joystick.background:setFillColor(0.5)

    Joystick.movedStick:setFillColor(0.7)

    local function enterFrame(event)

      if Joystick.isActivated() then

        local vector = Joystick.getVector()

        if vector.x < -0.5 then
          map.x = map.x + 2
          zombie.x = zombie.x + 2
          hero.rotation = 270
          hero.direction = "left"

        elseif vector.x > 0.5 then
          map.x = map.x - 2
          zombie.x = zombie.x -2
          hero.rotation = 90
          hero.direction = "right"
        end

        if vector.y < -0.5 then
          map.y = map.y + 2
          zombie.y = zombie.y + 2
          hero.rotation = 0
          hero.direction = "up"

        elseif vector.y > 0.5 then
          map.y = map.y - 2
          zombie.y = zombie.y-2
          hero.rotation = 180
          hero.direction = "down"
        end
      end
    end

    local AttackCoolDown = 0
    local function Attack(event)

      if(event.phase == "began") then

        if AttackCoolDown == 0 then
          hero.isVisible = false

          local mehaAttackAnimation = display.newSprite( AttackAnimationSheet, sequenceData )
          mehaAttackAnimation.x = display.contentCenterX
          mehaAttackAnimation.y = display.contentCenterY
          if hero.direction == "up" then
            mehaAttackAnimation.rotation = 0

          elseif hero.direction == "right" then
            mehaAttackAnimation.rotation = 90

          elseif hero.direction == "down" then
            mehaAttackAnimation.rotation = 180

          elseif hero.direction == "left" then
            mehaAttackAnimation.rotation = 270

          end
          mehaAttackAnimation:play()
          AttackCoolDown = 1

          timer.performWithDelay( 500, function()
          hero.isVisible = true
            mehaAttackAnimation:removeSelf()
            AttackCoolDown = 0

          end)
        end
      end
    end
 
    function hpDetection(event)

      if hero.hp == 0 then

        physics.stop()

        Runtime:removeEventListener("enterFrame",hpDetection)
        Runtime:removeEventListener("enterFrame", enterFrame)
        AttackButton:removeEventListener("touch", Attack)
        Runtime:removeEventListener("enterFrame", enemy)

        composer.gotoScene("scenes.lobby")

        zombie = nil
        hero = nil

        Joystick.setCoords( 10, 1000)
        AttackButton:removeSelf()
        hp:removeSelf()
        map:removeSelf()
      end
    end
 
    Runtime:addEventListener("enterFrame", enemy)
    Runtime:addEventListener("enterFrame",hpDetection)
    Runtime:addEventListener("enterFrame", enterFrame)
    AttackButton:addEventListener("touch", Attack)
 
  timer.performWithDelay( 2000, loadingEnd )
end

scene:addEventListener("create", scene);
return scene;
 

Attached Files



[TOPIC: post.html]
#2

schizoid2k

[GLOBAL: userInfoPane.html]
schizoid2k
  • Contributor

  • 578 posts
  • Corona SDK

You have all your code in the create() function.

 

create() is used to initialize your scene.  I think you should go back and read this to understand how to manage a scene properly.



[TOPIC: post.html]
#3

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

I need to put these listeners into show() function?



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,974 posts
  • Corona SDK

No, they don't need to go inside any of the Composer functions. They go above all of those. 



[TOPIC: post.html]
#5

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

I put them out of Composer function, but I still getting an error.



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,974 posts
  • Corona SDK

It tells you what the error is. You're not adding the listener in the same way the docs state:

local object = display.newImage( "image.png" )
 
function object:touch( event )
    if event.phase == "began" then
        print( "You touched the object!" )
        return true
    end
end
 
object:addEventListener( "touch", object )

You can also add a listener like this:

local object = display.newImage( "image.png" )
 
local touchObject = function (event)

    if event.phase == "began" then
        print( "You touched the object!" )
        return true
    end

end

object:addEventListener( "touch", touchObject )


[TOPIC: post.html]
#7

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

I changed Runtime to object name, but I got an error what says what object is nil,

because object is in create() function. I need to replace object outside Composer function?



[TOPIC: post.html]
#8

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK



 

It tells you what the error is. You're not adding the listener in the same way the docs state:

local object = display.newImage( "image.png" )
 
function object:touch( event )
    if event.phase == "began" then
        print( "You touched the object!" )
        return true
    end
end
 
object:addEventListener( "touch", object )

You can also add a listener like this:

local object = display.newImage( "image.png" )
 
local touchObject = function (event)

    if event.phase == "began" then
        print( "You touched the object!" )
        return true
    end

end

object:addEventListener( "touch", touchObject )

And I checked docs it says what enterFrame must be with Runtime:

Docs



[TOPIC: post.html]
#9

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,974 posts
  • Corona SDK

Well, what line is the error actually occurring on? We don't know what line 179 is in your code from your initial post. 



[TOPIC: post.html]
#10

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

Now I just get an error what doesn't say line...(I put all listeners outside composer functions)

 

In SpaceMap.lua 5th line there is goto scene function

And in lobby.lua 41st line there is same goto scene function

Attached Files




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: error, addeventlistener, eventlistener