如何在电晕sdk中产生物体

时间:2013-07-10 01:23:20

标签: android object random corona spawning

嘿,我是Corona sdk世界的新手,我想学习如何产生一些物体,让它们在屏幕上移动我尝试了一切,它从来没有工作我在正确的方式产生论坛并尝试但仍然结束在我的代码中出错,这是我的代码

 local  mRandom = math.random 
 local   mAbs = math.abs 
 local   objects = {"rocket02" ,"rocket01","coin01"}

 local   function spawnObject() 
   local objIdx = mRandom(#objects)
   local objName = objects[objIdx]
   local object  = display.newImage("image/object_"..objName..".png")
   object.x = mRandom (screenLeft +30,screenRight-30)
   object.y = screenTop

   if objIdx < 4 then 
      object.type = "food"
   else 
      object.type = "other" 
   end 
 end

也可以告诉我如何让它在屏幕上移动

请帮助谢谢

这是您要查看的媒体文件

1 个答案:

答案 0 :(得分:0)

我会告诉你一个方法。为此,我按如下方式重写了您的代码:

local  mRandom = math.random
local   objects = {"rocket02" ,"rocket01","coin01"}
local objectTag = 0
local object = {}

local   function spawnObject()
    objectTag = objectTag + 1
    local objIdx = mRandom(#objects)
    local objName = objects[objIdx]
    object[objectTag]  = display.newImage(objName..".png")  -- see the difference here
    object[objectTag].x = 30+mRandom(320)
    object[objectTag].y = 200
    object[objectTag].name = objectTag
    print(objectTag)
end
timer.performWithDelay(1,spawnObject,3)

这里我使用timer来显示对象。您也可以将for循环用于相同的目的。 在这里,您可以将标记为object[objectTag]的任何对象调用。

有关您的信息:

display.newImage(objName..".png") 
    --[[ will display object named rocket02.png or rocket01.png or coin01.png
        placed in the same folder where your main.lua resides --]]

display.newImage("image/object_"..objName..".png")
    --[[ will display object named object_rocket02.png or object_rocket01.png 
         or object_coin01.png placed in a folder named 'image'. And the folder
         'image' should reside in the same folder where your main.lua is. --]]

要将对象从上到下移动,您可以使用:

<强>或者

 function moveDown()
     object[objectTag].y = object[objectTag].y + 10  
     --replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3)
 end
 timer.performWithDelay(100,moveDown,-1)

 transition.to(object[objectTag],{time=1000,y=480})
 --[[ replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3)
 eg: transition.to(object[1],{time=1000,y=480}) --]]

保持编码..............:)