1

嘿,我是 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

也有人能告诉我如何让它在屏幕上移动

请帮忙谢谢

这是媒体文件供您查看

4

1 回答 1

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)

在这里,我使用了 atimer来显示对象。您也可以将 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}) --]]

继续编码........ :)

于 2013-07-11T08:03:40.127 回答