Looping Objects on screen

Ok so im having trouble with a function I created

I have two “ImageRects”, one that creates a bubble from an image and one that creates multiple images from a table. My goal is to have the image inside the bubble throughout a performWithDelay loop. The problem im having is when the images from the table get looped through, they for some odd reason, move away ffrom the bubble. I dont know why this is happening, can anyone help? I posted code and video below.

local function createBubble() local bubble = display.newImageRect(mainGroup, "assets/images/bubble.png", 90,90) bubble.anchorX = 0 bubble.anchorY = 0.5 physics.addBody(bubble, 'dynamic', {radius=43, bounce = 0.0}) bubble.name = "bubble" bubble.x = math.random(\_W-bubble.width) bubble.y = \_H-150 bubble:setLinearVelocity(0,-100) bubble:applyTorque(math.random(0.4,0.9)) bubble:addEventListener('touch', bubbleTap) local animal = display.newImageRect(mainGroup, "assets/images/animals/"..animals[math.random(#animals)]..".png",60,60) animal.anchorX = 0 animal.anchorY = 0.5 physics.addBody(animal, 'dynamic', {radius=43, bounce = 0.0}) animal.x = bubble.x + 15 animal.y = \_H-150 animal:setLinearVelocity(0,-100) animal:toBack() end function scene:create( event ) local sceneGroup = self.view physics.start() physics.setGravity(0,0) physics.pause() local background = display.newImageRect(backGroup, "assets/images/background.png", \_W, \_H ) background.x = contentX background.y = contentY end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() timer.performWithDelay(math.random(1800,2000), createBubble,0) end end

heres the video link:

https://youtu.be/9_-o2z7GUqo

I think its because they are pushing off of each other.  Since both are physics bodies with force being applied they are fighting with each other. Look at Physics Joints. A Weld joint might be what you need:  https://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Rob

Thanks Rob, i’ll give it a try.  :slight_smile:

I think its because they are pushing off of each other.  Since both are physics bodies with force being applied they are fighting with each other. Look at Physics Joints. A Weld joint might be what you need:  https://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Rob

Thanks Rob, i’ll give it a try.  :slight_smile: