Stopping function after delay

Hello.

I have function, that allows an object to rotate around its axis.

The object reaches the “ground” for a certain period of time (7 seconds, for example)

I would like, that in 7 seconds, the object would stop spinning and remain immobile

Further, I see two ways:

  1. Set the timer so that in 7 seconds the rotation function is canceled, stopped

  2. Do so that the rotation function stops when determining the collision of the falling object and the ground

Which way will be easier? And could you give some examples of the code, if possible

Further, I see two ways:

I’m not sure I understand this post.   Are you saying:

  1. You have an object that is spinning.

  2. It falls from the top of the screen and ‘lands’ on something below over a period of ~7 seconds.

  3. You want the object to stop spinning and come to rest as soon as it ‘lands’.

If that is the case, and assuming you’re using physics, the easiest way to do this is with a collision listener.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/fallStop.zip

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

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ===================================================== -- ===================================================== -- == Uncomment following lines if you need physics local physics = require "physics" physics.start() physics.setGravity(0,10) --physics.setDrawMode("hybrid") -- local back = display.newImageRect( "protoBackX.png", 720, 1386 ) back.x = display.contentCenterX back.y = display.contentCenterY if( display.contentWidth \> display.contentHeight ) then back.rotation = 90 end -- ===================================================== -- ===================================================== local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local mRand = math.random -- local ground = display.newRect( cx, cy + 200, fullw, 40) physics.addBody( ground, "static" ) local function onCollision( self, event ) if( event.phase == "began" ) then timer.performWithDelay( 1, function() physics.removeBody( self ) end ) transition.to( self, { alpha = 0, delay = 500, time = 0, onComplete = display.remove } ) end return true end local function ballDrop() local ball = display.newImageRect( "ball.png", 50, 50 ) ball:setFillColor( mRand(50,100)/100, mRand(50,100)/100, mRand(50,100)/100 ) ball.x = cx + mRand( -fullw/2, fullw/2 ) ball.y = cy - fullh/2 + 40 physics.addBody( ball, { bounce = 0, radius = 23 } ) ball.angularVelocity = (mRand(1,2)==1) and -180 or 180 ball.linearDamping = 2 ball.collision = onCollision ball:addEventListener( "collision" ) end timer.performWithDelay( 1000, ballDrop, -1 ) -- ===================================================== -- =====================================================

This exact approach can also be applied to a running timer or enterFrame listener.

If I totally misunderstood what you mean, please clarify.

Thanks, i will try apply what you advise me, @roaminggamer

Ohhhhhh, it is really work, thank you again :) 

I’m not sure I understand this post.   Are you saying:

  1. You have an object that is spinning.

  2. It falls from the top of the screen and ‘lands’ on something below over a period of ~7 seconds.

  3. You want the object to stop spinning and come to rest as soon as it ‘lands’.

If that is the case, and assuming you’re using physics, the easiest way to do this is with a collision listener.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/fallStop.zip

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

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ===================================================== -- ===================================================== -- == Uncomment following lines if you need physics local physics = require "physics" physics.start() physics.setGravity(0,10) --physics.setDrawMode("hybrid") -- local back = display.newImageRect( "protoBackX.png", 720, 1386 ) back.x = display.contentCenterX back.y = display.contentCenterY if( display.contentWidth \> display.contentHeight ) then back.rotation = 90 end -- ===================================================== -- ===================================================== local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local mRand = math.random -- local ground = display.newRect( cx, cy + 200, fullw, 40) physics.addBody( ground, "static" ) local function onCollision( self, event ) if( event.phase == "began" ) then timer.performWithDelay( 1, function() physics.removeBody( self ) end ) transition.to( self, { alpha = 0, delay = 500, time = 0, onComplete = display.remove } ) end return true end local function ballDrop() local ball = display.newImageRect( "ball.png", 50, 50 ) ball:setFillColor( mRand(50,100)/100, mRand(50,100)/100, mRand(50,100)/100 ) ball.x = cx + mRand( -fullw/2, fullw/2 ) ball.y = cy - fullh/2 + 40 physics.addBody( ball, { bounce = 0, radius = 23 } ) ball.angularVelocity = (mRand(1,2)==1) and -180 or 180 ball.linearDamping = 2 ball.collision = onCollision ball:addEventListener( "collision" ) end timer.performWithDelay( 1000, ballDrop, -1 ) -- ===================================================== -- =====================================================

This exact approach can also be applied to a running timer or enterFrame listener.

If I totally misunderstood what you mean, please clarify.

Thanks, i will try apply what you advise me, @roaminggamer

Ohhhhhh, it is really work, thank you again :)