I am trying to make an egg spawn every XX seconds from where the user clicks the screen. I want the player to be able to make multiple spawners. My problem is that when the player creates more than one, the eggs only spawn from the last place clicked, not from their original location. So think of the game Plants vs. Zombies and how the plants shoot a seed every XX seconds from their location. Thats basically what I'm trying to do. Here is part of my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function shootEgg(tX, tY, power) -- Spawn egg local egg = display.newCircle(tX, tY, 10) -- Add bullet physics physics.addBody( egg, { density= 10.0, friction= 0.3, bounce=0.00 } ) egg.isBullet = true egg:applyForce( power*20, 0, egg.x, egg.y ) return egg end function 123() timers[1][2] = timer.performWithDelay(2000, shootEgg(x_here, y_here, power_here), 0) end |
you need to give each place touched a unique name
tCount = 1
touch = {}
touch[tCount] = { event.x,event.y }
tCount = tCount + 1
then cycle though the touch table for each touch x & y
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function shootEgg(tX, tY, power) -- Spawn egg local egg = display.newCircle(tX, tY, 10) -- Add bullet physics physics.addBody( egg, { density= 10.0, friction= 0.3, bounce=0.00 } ) egg.isBullet = true egg:applyForce( power*20, 0, egg.x, egg.y ) return egg end local tempFunction = function() shootEgg(x_here, y_here, power_here) end function 123() timers[1][2] = timer.performWithDelay(2000, tempFunction, 0) end |
Here is how i handle passing vars with timers. Basically i keep all my timers attached the related objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function gunObject:shootEgg(event) -- Spawn egg local Bullet = display.newCircle(gunObject.x , gunObject.y, 10 ) -- Add bullet physics timer.performWithDelay(1,function() -- i normaly add a frame delay before spawning any type of physics object. physics.addBody( Bullet, { density= 10.0, friction= 0.3, bounce=0.00 } ) Bullet.isBullet = true Bullet:applyForce( gunObject.power*20, 0, gunObject.targetX, gunObject.targetY ) return Bullet end end function shootGun(EggObject) local gunObject = getGunObject() -- and/or create your gun object here gunObject.targetX = EggObject.x gunObject.targetY = EggObject.y gunObject.power = 100 gunObject.timer = timer.performWithDelay( 2000, gunObject, 0 ) end |
Thanks for your replies! The problem is I need to have multiple timers launching the same function but with different parameters. For example:
timer1: x = 50, y = 50, size = 10
timer2: x = 27, y = 56, size = 20
timer3: x = 38, y = 90, power = 10
1 2 3 4 5 | function 123() newCircle(x,y,size) --All the timers run this function but I need each of them to carry different --parameters. When I use the methods mentioned above, all the timers --use the values of the last called timer. end |
Something like this?
1 2 3 4 5 6 7 | timer1 = timer.performWithDelay(2000, function() shootEgg(50,50,10), 0) end,0) timer2 = timer.performWithDelay(2000, function() shootEgg(27,56,20), 0) end,0) timer3 = timer.performWithDelay(2000, function() shootEgg(38,90,10), 0) end,0) |
Thanks again for the reply! I would expect that to work but the same issue still exists, all the eggs spawn from the last clicked location. Instead of them spawning from the original location AND fro each subsequent location clicked, they ALL spawn from the last location clicked.
The left is what I'm trying to do. The right is my result.
As you can see, all the eggs shoot from the last clicked location instead of from it's correct location like on the left.
Ok so after rewriting all of my code I finally managed to get it working. Thanks for all the replies and help!
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Music/Song Database?
Catch The Bug is now available in the App Store
Network Reachability
Can you save the entire captured photo to a file?
Apple phasing out UDID - how does this affect Corona?
Default orientation
a password problem comes up when trying to build...
Interesting Headlines on iPhone Game News
Works on Windows and Mac simulators but not on the device.