How to implement a conveyor belt

Hi everyone,

I am new to Corona SDK and try to realise a game using this SDK but I got stuck implementing a conveyor belt.

What I want to program?

A conveyor belt which transports multiple objects from left to right. If an item is picked up and dragged of the conveyor belt this item should not be influenced anymore from the conveyor belt movement. All other items should move normally from left to right.

It should also be possible to adjust the speed.

What did I try?

So far I tried two possible solutions:

  • Detect a collision from an item with the conveyor belt and then start a timer which continously sets a linear velocity to this item.

          [lua]object:setLinearVelocity(40, 0)[/lua]

  • Detect a collision from an item with the conveyor belt and then start a transition.

           [lua]transition.to( collidedObject, { time = 7000, x = 700, y = 0} )[/lua]

Both solutions seemed to me as a not perfect solution. I always have to save the timer or transition for each item and stop only those which was dragged of the conveyor belt.

What do I need from you?

Do you know any better way to implement a conveyor belt? Or do I have to use the above mentioned approach? 

Currently it feels very “hacky” to me and I would love to have a more stable implementation. 

Thank you very much in advance for your help!

Regards,

Michael

https://www.youtube.com/watch?v=5tXN7C_4Ieo&feature=youtu.be

local physics = require "physics" physics.start() physics.setGravity(0,10) local beltSpeed = 100 local container = display.newContainer( 400, 200 ) container.x = display.contentCenterX container.y = display.contentCenterY local frame = display.newRect( container.x, container.y, 400, 200 ) frame:setFillColor(0,0,0,0) frame:setStrokeColor(0,0,1,1) frame.strokeWidth = 4 local trigger = display.newRect( container, 0, 0, 400, 200 ) trigger:setFillColor(0,1,0,0.2) physics.addBody(trigger, "static") trigger.isSensor = true local curX = -240 local links = {} for i = 1, 13 do local link = display.newRect( container, curX, 0, 40, 40 ) links[i] = link link:setFillColor(0.5,0.5,0.5) link:setStrokeColor(1,0,0) link.strokeWidth = 2 physics.addBody(link, "kinematic" , { friction = 1, bounce = 0 }) link.label = display.newText( container, i, link.x, link.y ) function link.enterFrame( self ) self.label.x = self.x self.label.y = self.y if( self.x \> 240 ) then self.x = self.x - 480 end end; Runtime:addEventListener( "enterFrame", link ) link:setLinearVelocity( beltSpeed, 0 ) curX = curX + 40 end local box = display.newRect( container, -150, -50, 40, 40 ) physics.addBody(box, "dynamic" , { friction = 1, bounce = 0 }) box:setFillColor(0,1,1) function box.enterFrame( self ) if( self.x \> 240 ) then self.x = self.x - 480 end end; Runtime:addEventListener( "enterFrame", box )

Thanks, this is very helpful.

Yesterday I solved it my own way. My solution:

I use a kind of OOP approach and have a item class for the items which are transported on the conveyor belt. So if the collision occurs I pass the timer to the item class and register a callback. The callback method than triggers the setLinearVelocity on the spawned object in my game.lua file. If the item is dragged of the belt the timer is stopped in the item class.

https://www.youtube.com/watch?v=5tXN7C_4Ieo&feature=youtu.be

local physics = require "physics" physics.start() physics.setGravity(0,10) local beltSpeed = 100 local container = display.newContainer( 400, 200 ) container.x = display.contentCenterX container.y = display.contentCenterY local frame = display.newRect( container.x, container.y, 400, 200 ) frame:setFillColor(0,0,0,0) frame:setStrokeColor(0,0,1,1) frame.strokeWidth = 4 local trigger = display.newRect( container, 0, 0, 400, 200 ) trigger:setFillColor(0,1,0,0.2) physics.addBody(trigger, "static") trigger.isSensor = true local curX = -240 local links = {} for i = 1, 13 do local link = display.newRect( container, curX, 0, 40, 40 ) links[i] = link link:setFillColor(0.5,0.5,0.5) link:setStrokeColor(1,0,0) link.strokeWidth = 2 physics.addBody(link, "kinematic" , { friction = 1, bounce = 0 }) link.label = display.newText( container, i, link.x, link.y ) function link.enterFrame( self ) self.label.x = self.x self.label.y = self.y if( self.x \> 240 ) then self.x = self.x - 480 end end; Runtime:addEventListener( "enterFrame", link ) link:setLinearVelocity( beltSpeed, 0 ) curX = curX + 40 end local box = display.newRect( container, -150, -50, 40, 40 ) physics.addBody(box, "dynamic" , { friction = 1, bounce = 0 }) box:setFillColor(0,1,1) function box.enterFrame( self ) if( self.x \> 240 ) then self.x = self.x - 480 end end; Runtime:addEventListener( "enterFrame", box )

Thanks, this is very helpful.

Yesterday I solved it my own way. My solution:

I use a kind of OOP approach and have a item class for the items which are transported on the conveyor belt. So if the collision occurs I pass the timer to the item class and register a callback. The callback method than triggers the setLinearVelocity on the spawned object in my game.lua file. If the item is dragged of the belt the timer is stopped in the item class.