When I try and make a button that rotates something it only rotates while im moving the mouse. This will help you understand:
http://youtu.be/p8cBuvScY_Q
what do i do to fix this?
PS: the code you see in the video doesnt have “local” at the start of some, thats because I followed instructions given to me and declared the variables at the top. [import]uid: 123066 topic_id: 22767 reply_id: 322767[/import]
Could you post some code of the function, variable, and event listener of that image? [import]uid: 122076 topic_id: 22767 reply_id: 90846[/import]
All of the code except the pictures and variables were in the video but here it is:
Diplay Objects and Variables:[lua]local bear
local rotateLeft
local rotateRight
local leftButton
local rightButton
local back
local pressBack
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
local background = display.newImage( “invasionBackground.png” )
background.x = display.contentWidth - 240
background.y = display.contentHeight - 160
background:scale(1,1)
bear = display.newImage( “polarbear.png”, 130, 60 )
bear:scale(0.30,0.30)
bear.rotation = rotation
leftButton = display.newImage( “rotateLeft.png”, -185, 195 )
leftButton:scale(0.25,0.2)
rightButton = display.newImage( “rotateRight.png”, -80, 195 )
rightButton:scale(0.25,0.2)
back = display.newImage( “back.png”, -140, -100 )
back:scale(0.15,0.15)
group:insert(background)
group:insert(bear)
group:insert(leftButton)
group:insert(rightButton)
group:insert(back)
end[/lua]
The Functions:[lua]function scene:enterScene( event )
local group = self.view
rotateLeft = function( event )
bear.rotation = bear.rotation - 6
end
rotateRight = function( event )
bear.rotation = bear.rotation + 6
end
pressBack = function( event )
storyboard.gotoScene( “Menu”, “zoomOutInFade”, 240 )
end
back:addEventListener( “tap”, pressBack )
leftButton:addEventListener( “touch”, rotateLeft )
rightButton:addEventListener( “touch”, rotateRight )
end[/lua] [import]uid: 123066 topic_id: 22767 reply_id: 90854[/import]
It’s doing it on pressed, moved and ended event phases.
What you’d want to do is use a timer to do the rotate every X seconds, then cancel the timer when the button is released
[import]uid: 52491 topic_id: 22767 reply_id: 90877[/import]
How do you do that? The only “timer” I know of is the Runtime event listener, can you please explain (I am still learning) [import]uid: 123066 topic_id: 22767 reply_id: 90890[/import]
Try this code on its own, should give you something to work from;
[lua]local rect = display.newRect( 150, 100, 20, 100 )
local button = display.newRect( 110, 400, 100, 30 )
local function rotateRect()
rect.rotation = rect.rotation+6
end
local function pressButton(event)
if event.phase == “began” then
rotateTimer = timer.performWithDelay(100, rotateRect, 0)
elseif event.phase == “ended” then
timer.cancel(rotateTimer)
end
end
button:addEventListener(“touch”, pressButton)[/lua]
Timers you can learn about in the API section or, alternatively, check out this little tutorial I wrote about them; http://techority.com/2011/11/13/understanding-timers/
Peach
[import]uid: 52491 topic_id: 22767 reply_id: 90920[/import]
Thank you so much! ? [import]uid: 123066 topic_id: 22767 reply_id: 90925[/import]
Ok theres a problem with that. When I hold down on the right button(example) It spins right, when I let go it stops which is all good. When I drag my mouse off of the right button and let go it continously spins! this is a problem because I need to be able to drag my finger on to the button next to it so it can spin the other way. Is this fixable? [import]uid: 123066 topic_id: 22767 reply_id: 90928[/import]
Yes, there are different ways of handling that - normal button issue.
You could and a;
or event.phase == “moved”
in the part where we check “ended”, that’s one option.
Another is setting focus.
The the “moved” phase first and let me know.
Peach
[import]uid: 52491 topic_id: 22767 reply_id: 91050[/import]
When I do it with the “moved” event thing then It stops rotating when I move my mouse. I need it to stp when I move my mouse off of the object! [import]uid: 123066 topic_id: 22767 reply_id: 91053[/import]
Check coords; http://developer.anscamobile.com/forum/2011/04/16/detecting-when-finger-slides-display-object
[import]uid: 52491 topic_id: 22767 reply_id: 91110[/import]