spawn and move

hey guys I’m trying to make a function that will spawn and move sprites

local function spawnBerry( event )  
 local berry = display.newImageRect("Berry.png", 50, 60)  
 berry.x, berry.y = math.random(50, 420), math.random(1, 300)  
 berry.x = berry.x - 1 -- Moves berry  
end  
timer.performWithDelay(500, spawnBerry, 99999999999999)  

There is my code and the line that says (Moves Berry) obviously isn’t doing its job. I’m a newbie to lua and to corona. So any help on how I could get the berry to move would be great

THANKS! [import]uid: 86506 topic_id: 14632 reply_id: 314632[/import]

@calebjj,
Have a read to understand the basic of movement here.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14632 reply_id: 54124[/import]

You’re probably better off using a transition than a timer :wink:

Try this code;

[lua]local berry = display.newImageRect(“Berry.png”, 50, 60)
berry.x, berry.y = math.random(50, 420), math.random(1, 300)
transition.to(berry, {time=2000, x=0})[/lua]

Have a play with transition.to() - it’s far nicer than a timer :wink:

Peach [import]uid: 52491 topic_id: 14632 reply_id: 54159[/import]

This works great but how do I change the rate of which they are created? [import]uid: 86506 topic_id: 14632 reply_id: 54190[/import]

Try this :

local function spawnBerry( event )  
 local berry = display.newImageRect("Berry.png", 50, 60)  
 berry.x, berry.y = math.random(50, 420), math.random(1, 300)  
 transition.to(berry, {time=2000, x=0})  
  
 return berry  
end  
timer.performWithDelay(math.random(500, 1000), spawnBerry, 0)  

Play around with the values in math.random to vary the speed they are created. Or you can just specific a number like you did before. [import]uid: 84637 topic_id: 14632 reply_id: 54210[/import]