1

Well i have some trouble removing an object from the game, the thing is that i have a player class (made out of a metatable), inside it i have a variable called sprite that holds the address for the image sprite i will draw onScreen, so when i create the object i don't draw the sprite right away, for that i made a function draw (this is just to explain what i have). in the game.lua i draw the player by calling that function, and afterwards i want to delete my instance of player (so that way the image onscreen dissapears also)... thats all, i tried player:removeSelf(), display.remove(player) and one of them threw me an error (attemp to call field 'removeSelf' (a nil value)) and the other one runs fine but it doesn't change the fact that the player is still there (i can acces it's functions and the sprite is still shown onscreen... well here is my code:

**********************************************************
game.lua:
**********************************************************

---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

local _W, _H = display.contentWidth * 0.5, display.contentHeight * 0.5
local background, player, land
local spriteSizeX, spriteSizeY = 60,70

-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
local BG = display.newGroup()
local stage = display.newGroup()
local foreground = display.newGroup()

group:insert(BG)
group:insert(stage)
group:insert(foreground)

-----------------------------------------------------------------------------

--  CREATE display objects and add them to 'group' here.
--  Example use-case: Restore 'group' from previously saved state.

-----------------------------------------------------------------------------

background = display.newImage("assets/backgrounds/oliveBackground.png", true)
background.x, background.y = _W, _H
BG:insert( background )

player = playerClass.new()
player:draw(15,57.5,foreground)
--player:movePlayer(300,140)

terrain = {}
local m,j = 0,0

for i = 1, 16 do
local l = 1
for k = 3, 10 do
land = landClass.new({posx = i, posy = k})
table.insert(terrain, land)
m = m +1
terrain[m]:draw( spriteSizeX/4 + ((spriteSizeX/2) * j), spriteSizeY/4 + ((spriteSizeY/2) * l) + 5, stage)
l = l + 1
end
j = j+1
end

-- remove an asset doesn't work
--display.remove(terrain[1]:getSprite())
--terrain[1].removeSelf()
-- terrain[1] = nil
player:destroy()

end

**********************************************************
player.lua:
**********************************************************

-- player class

local player = {}
local player_mt = { __index = player }

--[[
-- attributes
local sprite, coins, speed
]]--

function player.new()   -- constructor

local newPlayer = {
sprite = "assets/char/miner.png",
coins = 1000,
speed = 1
}

return setmetatable( newPlayer, player_mt )

end

-- local function, works only when called from inside this class
local function getName()
-- print("")
end

function player:draw(x,y,group)
sprite = display.newImage(self.sprite)
sprite.x, sprite.y = x, y
sprite.xScale, sprite.yScale = 0.5, 0.5
group:insert(sprite)
end

function player:movePlayer(posx,posy)
transition.to(sprite, { x = posx, y = posy, time=500, delay=0, alpha=1.0 })
end

function player:destroy()
-- none of them work
-- self.sprite = nil
-- self.sprite.removeSelf()
end

return player
ershin69
  • 96
  • 3
  • 16

2 Answers2

3

After creating your sprite using display.newImage, you did not store it in the instance. self.sprite just has the string value "assets/char/miner.png"

in your draw function add

self.spriteObject = sprite

and in your destroy function ,

self.spriteObject:removeSelf()
SatheeshJM
  • 3,575
  • 8
  • 37
  • 60
  • Omg great that works perfectly fine! ok so that's one step ahead, what i would really like to understand is how do i destroy the entire instance of the object? with this i'm destroying it's image but the object remains there, on a big project i wouldn't like to have those objects instantiated after i use them... – ershin69 Jun 03 '12 at 16:39
  • The sprite data is stored in a table. So even after the sprite is removed, the table which stored the sprite will have the original values(except some values like x, y which will become nil). So to delete the table, you just have to assign nil to all variables which the table has been stored. In this case `self.spriteObject=nil` After that lua's garbage collection does the rest. – SatheeshJM Jun 03 '12 at 17:12
2

You can use one of these functions for your related object:

  • removeSelf()
  • destroy()
Jeromy Irvine
  • 11,614
  • 3
  • 39
  • 52