2014-06-20 55 views
0

我最近開始用corona sdk編程來製作一個簡單的遊戲。 我需要創建動態對象,當我移動一些對象時,它會自行刪除。我可以創建動態對象,但我無法處理每個事件。如何在corona sdk中創建,移動和移除動態對象?

我想通過功能來完成這一切。

這是我的一段代碼和上一個函數(myObject:touch)我想將它更改爲一個新函數,該函數不僅處理所有對象,而且還處理所有對象,因此我需要將對象名作爲參數發送到該函數。 你能幫忙嗎?

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) 

回答

2

我知道你在這裏尋找僅僅是改變這樣的時差過渡:

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 
+0

非常感謝你。這正是我一直在尋找的:) – Ashkan