Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Spawn bullet when an object gets in range of the shooter
Started by matt.e.pitcher Jan 11 2020 08:06 PM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

matt.e.pitcher

[GLOBAL: userInfoPane.html]
matt.e.pitcher
  • Observer

  • 2 posts
  • Corona SDK

Hey all,

 

I'm trying to figure out the best way to shoot a bullet from a static shooter object to a dynamic object that has moved into the range of the shooter. This is my first day using Corona and its been pretty great so far, can anyone help me solve this? Right now I am trying to add the bullet during an onCollision event, but that doesn't work because "physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event".

 

Any help is appreciated, thank you in advance!



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,613 posts
  • Corona SDK

If you want to use a collision listener, just spawn the bullet in the next frame

... in collision listener

-- wait 1 ms, because
-- all timers created in the current frame processing sweep, with a timer > 0 will occur
-- no sooner than the next frame
-- at that point this physics processing context will be over.

timer.performWithDelay( 1, 
function()
  -- add bullet creation code here
end )

  • matt.e.pitcher likes this

[TOPIC: post.html]
#3

matt.e.pitcher

[GLOBAL: userInfoPane.html]
matt.e.pitcher
  • Observer

  • 2 posts
  • Corona SDK

Do you think using a collision event on a radius object that represents the range of the shooter object to shoot the projectile would be more efficient than using enterFrame to loop over targets and shooters and calculating the distance between them to assign targets to the shooters?

 

I will probably just try it both ways and see what happens depending on number of shooters / targets. Any advice is appreciated though and thank you for the recommendation above!




[topic_controls]
[/topic_controls]