如何在电晕SDK中创建,移动和删除动态对象? [英] How to create, move and remove dynamic objects in corona sdk?

查看:77
本文介绍了如何在电晕SDK中创建,移动和删除动态对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用corona sdk进行编程,以制作一个简单的游戏. 我需要创建动态对象,当我将某些对象移出时,它会自行删除.我可以创建动态对象,但不能处理每个对象上的事件.

I recently started programming with corona sdk to make a simple game. I need to create dynamic objects and when I move some object out it removes itself. I can create dynamic objects but I can't handle the events on each one.

我想通过功能来做这一切.

I want to do it all by functions.

这是我的代码的一部分,在最后一个函数(myObject:touch)中,我想将其更改为一个新函数,该函数不仅可以处理myObject的所有对象,所以我需要将对象名称作为参数发送给该功能. 你能帮忙吗?

Here's a piece of my code and in last function (myObject:touch) I'd like to change it to a new function which will handle all objects not only myObject so I need to send object name as a parameter to that function. Would you please help?

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 )

推荐答案

我认为您在这里寻找的只是像这样更改moveOut中的过渡:

I think what you are looking for here is simply to change the transition in moveOut like this:

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

这篇关于如何在电晕SDK中创建,移动和删除动态对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆