Need help with a start stop button..

Hiya,

Im working on a button that when pressed will cause a disk to spin. I have that all said and done but the disk only spins when the button is released and still only spins about one rotation. I need the disk to spin non-stop when the button is released and stop spinning when the stop button is hit? What do…

Thank you and a million internets to whoever helps me :slight_smile: [import]uid: 10355 topic_id: 10187 reply_id: 310187[/import]

Hi Evna.

I think we would need to more about your code to answer this. In general terms, you will have a button, which is probably a ui.newButton object (requires ui.lua). That button will call an event handler function when its tapped.

That function would do something like:

local isSpinning = false  
local function onTap(event)  
 if isSpinning then  
 isSpinning = false  
 else  
 isSpinning = true  
 end  
end  
  
local function spinDisc(disc, speed)  
 disc:rotate(speed)  
end   
  
local function gameLoop(event)  
 if isSpinning then  
 spinDisc(disc1, speed)  
 spinDisc(disc2, speed\*2) -- etc.  
 end  
end  
  
-- insert your disc creation code here  
-- figure out what speed should be  
  
local playButton = ui.newButton{default="play\_btn.png", over="play\_btn\_down.png", onRelease=onTap, x=display.contentWidth/2, y=display.contentHeight-20}  
  
Runtime:addEventListener("enterFrame", gameLoop)  

Alternatively instead of setting an isSpinning flag, you could Add/remove the event listener for the “enterFrame” event. Basically ever frame (30 times per section or 60 times per second depending on settings) the gameLoop gets called to process your game movement.

Rob [import]uid: 19626 topic_id: 10187 reply_id: 37194[/import]

Hey thank you for the reply! I really appreciate it. I ive taken a look at your code and am trying to get it to work/ understand it ( forgive my ignorance)… is that the only way you would go about doing this?

And here is my original code that was doing what I described in the first post…

  
 local function startSpin (event)  
 wiseman.rotation = wiseman.rotation + 103  
 end  
  
  
 local startButton = nil  
 startButton = ui.newButton{  
 defaultSrc = "images/under2.png",  
 defaultX = 73,  
 defaultY = 39,   
 overSrc = "images/over2.png",  
 overX = 73,  
 overY = 39,   
 onEvent = startSpin,  
 id = "oneButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 emboss = false  
 }  
 startButton.x = 67  
 startButton.y = 393  
 startButton.isActive = true  
 choice1:insert(startButton)  
  

When I hit the start button the disc rotates 103 degrees… If I add an event listener to the Runtime the disc spins constantly at a speed of 103 degrees/s … Argg I need this to happen when the start button is hit… :confused: [import]uid: 10355 topic_id: 10187 reply_id: 37540[/import]

The Runtime “enterFrame” event is the way your program knows to do things like move things, rotate, fade, etc. if you want it to be continuous. A lot of things you can use transition.to to move around and such, but those are generally things that have a fixed start and end point. Since you want it to spin, it would be a pain to make transition.to work for you.

So when you do: Runtime:addEventHandler(“enterFrame”, myfunction)

You are asking Corona to call myFunction 30 times per second (or 60 depending on settings). In “myfunction” you do your math to calculate your new rotation and you let it continue.

What you want your button to do is to pause/start the rotation and you can do that one of two ways:

  1. In “myfunction” you can put in a variable that determines the “state” of the rotation. Then your button’s onRelease function can toggle that “isRotating” state variable. This is the way I would do it. But I’m an old school programmer.

  2. The other way is to use your button’s onRelease function to either add the event handler or remove it depending on the current state. Any way, you’re going to need a variable that keeps track of if your spinning or not.

BTW: Your button call variables don’t look right to me. The parameters to ui.button are specific and case sensitive. Maybe you have a different ui.lua file than I’m using. [import]uid: 19626 topic_id: 10187 reply_id: 37542[/import]

Here’s how I’m doing it in my Roly-Polies HD game:

BlockRedSwirl = display.newImage("Block-RedSwirl.png")  
BlockRedSwirl.x = 200  
BlockRedSwirl.y = 200  
local function spinSwirl()  
 BlockRedSwirl:rotate( -10 ) -- go counter-clockwide 10 degrees  
end  
BlockRedSwirl.tmr = timer.performWithDelay ( 100, spinSwirl, -1 ) -- move every 1/10th second  

The line that calls timer.performWithDelay() can be put inside your touch listener so it only starts when you touch the button.

When you want to make it stop you can just do something like:

timer.cancel(BlockRedSwirl.tmr)  

…to kill the timer that’s making it rotate.

You can mess around with the degrees of rotation and the timer delay to make it spin as fast or slow as you want.

Jay
[import]uid: 9440 topic_id: 10187 reply_id: 37551[/import]

Rob, thank you for the explanation… I finally got it to work! :smiley:

And Jay, I tried your method too and it works as well, although when you attached the delay to a button and hit the button multiple times the speed of the object seems to multiply till it eventually stops moving at all. Would there be a way to use the button were it only calls the delay once? Sorry if this is confusing… im trying to explain the best I can. And thank you for your help as well! [import]uid: 10355 topic_id: 10187 reply_id: 37709[/import]