如何在corona sdk中创建、移动和删除动态对象?

最近我开始用 corona SDK 编写一个简单的游戏。我需要创建动态对象,当我将某个对象移出时,它会自动删除自身。我可以创建动态对象,但我无法处理每个对象上的事件。我想全部都用函数来实现。这是我的一段代码,在最后一个函数(myObject:touch)中,我想改成一个新的函数,它将处理所有对象而不仅仅是 myObject,因此我需要将对象名称作为参数发送到该函数。您能帮忙一下吗?

function create_obj(img,xpos,ypos)
    myObject = display.newImage(img)
    myObject.x=xpos
    myObject.y=ypos
end

function move_out(obj)
    transition.to( obj, { time=2000, alpha=1, x=60, y=60, width=1 ,height=1, onComplete= remove_obj(obj) } )
end

function remove_obj(obj)
    obj:removeSelf()
    obj=nil
end

--create 1st object
local img1="icon1.png"
create_obj(img1,50,50)

--create 2nd object
local img2="icon2.png"
create_obj(img2,100,100)

--create 3rd object
local img3="icon3.png"
create_obj(img3,150,150)

function myObject:touch( event )
    if event.phase == "began" then
        self.markX = self.x -- store x location of object
        self.markY = self.y -- store y location of object
    elseif event.phase == "moved" then
        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY
        self.x, self.y = x, y
    elseif event.phase == "ended" or event.phase == "cancelled" then
        move_out(myObject)
    end
    return true
end

   myObject:addEventListener( "touch", myObject )
点赞
用户2895078
用户2895078

我认为你想要的只是像这样改变 moveOut 中的 transition:

function move_out(obj)
    transition.to( obj, { time=2000, alpha=1, x=60, y=60, width=1 ,height=1, onComplete=function() remove_obj(obj) end } )
end
2014-06-20 15:33:33