How can I make an object come out of another

I have a player/image that goes up and down on the screen. 

I have a function that spawns an image every time you touch the screen

— right here is the function that spawns the image —

tapTothrow = function (event)

    if event.phase == “began” then

        moneyBag = display.newImageRect(gameGroup,“play.png”,80,80)

        moneyBag.x = W1 / 2 + 100

        moneyBag.y = H1 / 2 + 120

        physics.addBody(moneyBag)

        moneyBag:setLinearVelocity(100, 0)

    end

end

— right here is the function that spawns the image —

as you can see I set the image to spawn at a set place on the screen but I want it to spawn where the player/image is. thanks!!!

also download super pooper its for both app store and google play. googleplay version has the updated version. It’s also my first app! (]: thanks for the support!

Hey Max.

First and foremost I see some problems with the way you are creating those images upon touch. The main ones being that: 

  1. You appear to be creating them as global variables (basing this on the code you have posted).

  2. You loose the reference to the previously spawned object every time you spawn one, and thus are creating memory leaks. (unless you are tracking them in a table in the code you didn’t post)

In either case, please see the following guide, as I believe it will serve you well: http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

To answer your question about positioning it where the player is:

moneyBag.x = player.x moneyBag.y = player.y 

That assumes that your money bag and player both share an anchor point of 0.5 (which is the default) and that your “player” is actually named player of course :slight_smile:

Happy coding!

thanks a ton! Also I just looked at that link for spawning images correctly right before you answered my question! Ha I was hoping you would tell me to go to that tutorial! thanks!

how can I make the object go where I tap instead of the same place every time? here is my code.

tapTothrow = function (event)

    if event.phase == “began” then

        moneyBag = display.newImageRect(gameGroup,“play.png”,80,80)

        moneyBag.x = player.x + 50

        moneyBag.y = player.y 

        physics.addBody(moneyBag,{radius=35})

        moneyBag:setLinearVelocity(400, 0)

    end

end

Runtime:addEventListener(“touch”,tapTothrow)

Change moneyBag x/y to event.x and event.y respectively.

Have you gone through the beginner tutorials on the Corona university site? If not i think they would be extremely helpful to you.

Happy coding!

it works thanks! and no Ill check them out. Thanks for the suggestion!

Hey Max.

First and foremost I see some problems with the way you are creating those images upon touch. The main ones being that: 

  1. You appear to be creating them as global variables (basing this on the code you have posted).

  2. You loose the reference to the previously spawned object every time you spawn one, and thus are creating memory leaks. (unless you are tracking them in a table in the code you didn’t post)

In either case, please see the following guide, as I believe it will serve you well: http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

To answer your question about positioning it where the player is:

moneyBag.x = player.x moneyBag.y = player.y 

That assumes that your money bag and player both share an anchor point of 0.5 (which is the default) and that your “player” is actually named player of course :slight_smile:

Happy coding!

thanks a ton! Also I just looked at that link for spawning images correctly right before you answered my question! Ha I was hoping you would tell me to go to that tutorial! thanks!

how can I make the object go where I tap instead of the same place every time? here is my code.

tapTothrow = function (event)

    if event.phase == “began” then

        moneyBag = display.newImageRect(gameGroup,“play.png”,80,80)

        moneyBag.x = player.x + 50

        moneyBag.y = player.y 

        physics.addBody(moneyBag,{radius=35})

        moneyBag:setLinearVelocity(400, 0)

    end

end

Runtime:addEventListener(“touch”,tapTothrow)

Change moneyBag x/y to event.x and event.y respectively.

Have you gone through the beginner tutorials on the Corona university site? If not i think they would be extremely helpful to you.

Happy coding!

it works thanks! and no Ill check them out. Thanks for the suggestion!