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
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()