Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:43:43+00:00 2026-06-04T21:43:43+00:00

Well i have some trouble removing an object from the game, the thing is

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:43:44+00:00Added an answer on June 4, 2026 at 9:43 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some login code that works well for Firefox and other browsers, but
I have an application written in C# that invokes some C code as well.
I've got some 3rd party beans that have method signatures that fit quite well
I'm having some trouble understanding how codeigniters loading works. Well first you have the
I'm having some trouble figuring this out. I have the following CSV string hello
I have some queries that are working well separately. Each of these queries relates
I have some non well-formed xml (HTML) data in JAVA, I used JAXP Dom,
well i have a listbox with some items inside. i want to detect a
well, i have a big file, there are some contents i needed to be
I have a settings.php file with parametric variables and a functions.php with well, some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.