1

hey i am new to the Corona sdk world i want to learn how to spawn some objects and make them move across the screen i try everything and it never work i read the forum on spawning the right way and try it but still end up with a error in my code help this is my code

 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

Also can some one tell me how to make it move across the screen

Please help Thanks

here's the media file for you to take a look at

SeanDp32
  • 199
  • 4
  • 17

1 Answers1

0

I'll show you a method. For that, I have rewritten your code as follows:

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)

Here I've used a timer for displaying the object. You can also use for loop for the same purpose. Here you can call any object with tag as object[objectTag].

For your information:

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 --]]

And

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. --]]

And for moving your object from top to bottom, you can use:

either

 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)

or

 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}) --]]

Keep coding.............. :)

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
  • hey i get this new error message table index is nil on line object[i] = display.newImage("image/object_"..objName..".png") thank for your help – SeanDp32 Jul 12 '13 at 23:55
  • Sorry, my mistake. Replace 'i' with 'objectTag'. I've corrected the above code... – Krishna Raj Salim Jul 13 '13 at 10:12
  • it okay i am so glad that you are helping e with this an no mean to bug but get new error when i run then code. the error say "attempt to index field" it on line object[objectTag].x = 30+mRandom(320) i am not share what could be casing this. i am hoping that you can help me solve this so i could get my game going thanks – SeanDp32 Jul 14 '13 at 19:06
  • @user2566626: Hi, I've checked it. But the code is ok and I haven't getting any error. Check all needed images(object_rocket02.png,object_rocket01.png and object_coin01.png) are in the correct path(inside folder 'image'). And, are you using any other code instead of the above lines..? If you still getting an error, then please post the full error and if extra code, then that also.... :) – Krishna Raj Salim Jul 15 '13 at 04:13
  • yeah i don't another code running in my project but i keep getting this error "Attempt to index field “?” (nil value)" on this line object[objectTag].x = 30+mRandom(320).but i was wondering if i have to refer to a folder when i said "image/object_" should this be a folder name or what but if it shouldn't be the code that you gave me i am running it by it self and i got all the images in the folder that i am referring to. to help you solve this problem i could zip it up and send it to you. thanks a lot bro you a real life saver. – SeanDp32 Jul 15 '13 at 23:51
  • http://www.mediafire.com/download/u2p7atj9si95s9f/how_to_spawn_stuff.zip this is the media fire file you can use to help me solve this i hope it's not over board but thanks a lot for your help if this works a lot of new users to corona won't have this problem ps the images in the file isn't for commercial usage thanks – SeanDp32 Jul 16 '13 at 00:11
  • Wow... I saw the project. As per the code I gave to you, you need to put those 3 images inside a folder named 'image'. And every image name should start with 'object_'. Like: object_coin01.png,object_rocket01.png,etc. But by seeing the project, I've replaced the code for your convenience. Just replace the old code with the new one. And I am adding more information to clarify your doubt.... :) – Krishna Raj Salim Jul 16 '13 at 04:30
  • hey bro it's working thanks a million. this will help to all beginner there thanks a lot bro if i have any other question i be happy to post keep doing what your doing helping programmers make grate applications thanks. ...:) – SeanDp32 Jul 16 '13 at 22:46
  • @user2566626: Good to hear that your problem is solved. If the answer is satisfyable, then accept the answer by clicking the tick mark. Otherwise it will be shown as unanswered in the Stackoverflow community... :) – Krishna Raj Salim Jul 17 '13 at 04:03
  • i mark as a correct code it thanks again hope this help other people – SeanDp32 Jul 17 '13 at 11:51