obj touch function

Trying to get my obj(robber.PNG) to spawn at a set location and basically want to be able to touch him and he vanishes…

[lua]
local function trackRobber(obj)
obj:remove self();
end

local function spawnRobber()
  local robber = display.newImageRect(“Robber.png”, 45, 45);
  robber:setReferencePoint(display.CenterReferencePoint);
  robber.x = mRand(50, _W-50); robber.y = mRand(50, _H-50);

  function robber:touch(e)
    if(e.phase == “ended”) then
    track robber(self);
  end

return true;

end

robber:addEventListener(“touch”, robber);

end

[/lua]

Hi @tj.reis,

I’d actually set it up like this:

[lua]

local function touchRobber( event )

  if ( event.phase == “ended” ) then

    event.target:removeSelf() ; event.target = nil

  end

  return true

end

local function spawnRobber()

  local robber = display.newImageRect(“Robber.png”, 45, 45);

  robber.x = mRand(50, _W-50); robber.y = mRand(50, _H-50);

  robber:addEventListener( “touch”, touchRobber )

end

[/lua]

I’m using a different image atm that I already have running so I replaced robber with ghost and also have a cloud image scrolling above.

So its still not working and there is no error, everything works and shows up but the ghost is not clickable and will not remove it self when clicked/touch. Ive read many many tutorials but cant seem to find one that works so something is obviously not right, here is my main.lua file.

display.setStatusBar(display.HiddenStatusBar)

– Setup Graphics

local background = display.newImage(“background.jpg”)

local ghost = display.newImage(“ghost.png”)
ghost.x = 300
ghost.y = 590

local cloudwhite = display.newImage(“cloudwhite.png”)
cloudwhite:setReferencePoint(display.BottomLeftReferencePoint)
cloudwhite.x = 940

function scrollcloudwhite(self,event)
 if self.x < -220 then
 self.x = 940
 else
     self.x = self.x - 1
 end
 end
 
 cloudwhite.enterFrame = scrollcloudwhite
 Runtime:addEventListener(“enterFrame”, cloudwhite)
 
 
   local function touchGhost( event )
 
    if ( event.phase == “ended” ) then
      event.target:removeSelf() ; event.target = nil
    end
    return true
  
   end
  
   local function spawnGhost()
 
     local Ghost = display.newImageRect(“Ghost.png”, 45, 45);
     ghost.x = mRand(50, _W-50); ghost.y = mRand(50, _H-50);
     ghost:addEventListener( “touch”, touchGhost )
 
  end

You have defined the ghost as ‘Ghost’ - i.e. with a capital letter G. Then applied properties and the touch listener to ‘ghost’, with a lower case G. 

fixed and still not working

Hi @tj.reis,

I’d actually set it up like this:

[lua]

local function touchRobber( event )

  if ( event.phase == “ended” ) then

    event.target:removeSelf() ; event.target = nil

  end

  return true

end

local function spawnRobber()

  local robber = display.newImageRect(“Robber.png”, 45, 45);

  robber.x = mRand(50, _W-50); robber.y = mRand(50, _H-50);

  robber:addEventListener( “touch”, touchRobber )

end

[/lua]

I’m using a different image atm that I already have running so I replaced robber with ghost and also have a cloud image scrolling above.

So its still not working and there is no error, everything works and shows up but the ghost is not clickable and will not remove it self when clicked/touch. Ive read many many tutorials but cant seem to find one that works so something is obviously not right, here is my main.lua file.

display.setStatusBar(display.HiddenStatusBar)

– Setup Graphics

local background = display.newImage(“background.jpg”)

local ghost = display.newImage(“ghost.png”)
ghost.x = 300
ghost.y = 590

local cloudwhite = display.newImage(“cloudwhite.png”)
cloudwhite:setReferencePoint(display.BottomLeftReferencePoint)
cloudwhite.x = 940

function scrollcloudwhite(self,event)
 if self.x < -220 then
 self.x = 940
 else
     self.x = self.x - 1
 end
 end
 
 cloudwhite.enterFrame = scrollcloudwhite
 Runtime:addEventListener(“enterFrame”, cloudwhite)
 
 
   local function touchGhost( event )
 
    if ( event.phase == “ended” ) then
      event.target:removeSelf() ; event.target = nil
    end
    return true
  
   end
  
   local function spawnGhost()
 
     local Ghost = display.newImageRect(“Ghost.png”, 45, 45);
     ghost.x = mRand(50, _W-50); ghost.y = mRand(50, _H-50);
     ghost:addEventListener( “touch”, touchGhost )
 
  end

You have defined the ghost as ‘Ghost’ - i.e. with a capital letter G. Then applied properties and the touch listener to ‘ghost’, with a lower case G. 

fixed and still not working