1

好吧,我在从游戏中删除对象时遇到了一些麻烦,问题是我有一个玩家类(由元表组成),在里面我有一个名为 sprite 的变量,它保存我将在屏幕上绘制的图像精灵的地址,所以当我创建对象时,我不会立即绘制精灵,因为我做了一个函数绘制(这只是为了解释我所拥有的)。在game.lua中,我通过调用该函数来绘制玩家,然后我想删除我的玩家实例(这样屏幕上的图像也会消失)......就是这样,我尝试了player:removeSelf(),显示。 remove(player),其中一个向我抛出了一个错误(尝试调用字段'removeSelf'(一个 nil 值)),另一个运行正常,但它并没有改变玩家仍然在那里的事实(我可以访问它的功能和精灵仍然显示在屏幕上.. . 好吧,这是我的代码:

**********************************************************
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
4

2 回答 2

3

使用 display.newImage 创建精灵后,您没有将其存储在实例中。self.sprite 只有字符串值“assets/char/miner.png”

在您的绘图功能中添加

self.spriteObject = sprite

在你的销毁功能中,

self.spriteObject:removeSelf()
于 2012-06-03T16:24:12.643 回答
2

您可以对相关对象使用以下功能之一:

  • removeSelf()
  • destroy()
于 2012-12-07T14:10:44.360 回答