Help with system.getTimer

Hi everyone. I bought the videos from Jay, mastering Corona, they are good, I was hoping to get some answers to make my second app, but I still have the same 5 questions. this is one of them.

– Timer

– I have an image that is there on the screen.

– When I press a button, it moves from right to left. That’s good!

What function do I need to make that image stop?

This is the complete code

-- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- -- How to make an app -- By Victor M. Barba -- Copyright 2013 -- All Rights Reserved -- -- Version 1.0 -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[STORYBOARD REQUIRE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[VARIABLES]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local buttonHome local buttonBack local funny1 local funny2 ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[FUNCTIONS]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local function buttonHomeHandler() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener( "enterFrame", move );&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return true end local tPrevious = system.getTimer() local function move(event) &nbsp;&nbsp; &nbsp;local tDelta = event.time - tPrevious &nbsp;&nbsp; &nbsp;tPrevious = event.time &nbsp;&nbsp; &nbsp;local xOffset = ( 0.6 \* tDelta ) &nbsp;&nbsp; &nbsp;funny1.x = funny1.x - xOffset &nbsp;&nbsp; &nbsp;funny2.x = funny2.x - xOffset &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if (funny1.x + funny1.contentWidth) \< 0 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;funny1:translate( 600 \* 2, 0) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;if (funny2.x + funny2.contentWidth) \< 0 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;funny2:translate( 600 \* 2, 0) &nbsp;&nbsp; &nbsp;end end -------------------------------------------------------------------------------------------------------- Start everything moving local function buttonBackHandler() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:addEventListener( "enterFrame", move );&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return true end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) &nbsp;&nbsp; &nbsp;local group = self.view&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local background = display.newImage( "backgroundFunny.png" ) &nbsp;&nbsp; &nbsp;group:insert ( background ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;buttonHome = widget.newButton{ &nbsp;&nbsp; &nbsp;defaultFile="buttonHome.png", &nbsp;&nbsp; &nbsp;onRelease = buttonHomeHandler &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;group:insert ( buttonHome ) &nbsp;&nbsp; &nbsp;buttonHome.x = 910 &nbsp;&nbsp; &nbsp;buttonHome.y = 710 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;buttonBack = widget.newButton{ &nbsp;&nbsp; &nbsp;defaultFile="buttonBack.png", &nbsp;&nbsp; &nbsp;onRelease = buttonBackHandler &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;group:insert ( buttonBack ) &nbsp;&nbsp; &nbsp;buttonBack.x = 100 &nbsp;&nbsp; &nbsp;buttonBack.y = 710 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;funny1 = display.newImage( "funnyG1.png" ) &nbsp;&nbsp; &nbsp;group:insert ( funny1 ) &nbsp;&nbsp; &nbsp;funny1:setReferencePoint( display.CenterLeftReferencePoint ) &nbsp;&nbsp; &nbsp;funny1.x = 100; funny1.y = 200 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;funny2 = display.newImage( "carTireFront.png" ) &nbsp;&nbsp; &nbsp;group:insert ( funny2 ) &nbsp;&nbsp; &nbsp;funny2:setReferencePoint( display.CenterLeftReferencePoint ) &nbsp;&nbsp; &nbsp;funny2.x = 250; funny2.y = 200 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) &nbsp;&nbsp; &nbsp; end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() &nbsp;&nbsp; &nbsp; end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) &nbsp;&nbsp; &nbsp;local group = self.view&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if buttonHome then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonHome:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonHome = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if buttonBack then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonBack:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonBack = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end ------------------------------------------------------------------------------------------ scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thanks

Victor

I know your just playing with the code above so here is a quick example of another button press event.

to stop event listener you need to remove it.

[lua]

local function buttonStopHandler()

        Runtime:removeEventListener( “enterFrame”, move );

    return true

end

[/lua]

Now if you wanted to get fancy you could always have the runtime event listener running like in your example above but change your code to set a switch based on you clicking the buttons when to set a flag like isMoving to true or false and then wrap the code that updates the ( .x ) of the object only if isMoving = true.

Example:

[lua]

local isMoving = false;

local function buttonHomeHandler()
    isMoving = true;
    return true;
end

local function buttonBackHandler()
    isMoving = true;
    return true;
end

local function buttonStopHandler()

        isMoving = false;

    return true

end

local tPrevious = system.getTimer()
local function move(event)
    local tDelta = event.time - tPrevious
    tPrevious = event.time

    local xOffset = ( 0.6 * tDelta )
    
    if isMoving = true then

        funny1.x = funny1.x - xOffset
        funny2.x = funny2.x - xOffset
    
        if (funny1.x + funny1.contentWidth) < 0 then
            funny1:translate( 600 * 2, 0)
        end
        if (funny2.x + funny2.contentWidth) < 0 then
            funny2:translate( 600 * 2, 0)
        end
    end

end
-------------------------------------------------------------------------------------------------------- Start everything moving
Runtime:addEventListener( “enterFrame”, move );

[/lua]

 

Hi doubleslashdesign:

the code with the if isMoving, I didn’t really get it.

but the problem I had to make it stop I got that by changing the position of the function.

I just put the function that removes the event listener below the add event listener, and it works!

Thanks you for that I own you two…

Would it be too much to ask for a third favor?

I have a question about removing a sprite image

http://forums.coronalabs.com/topic/36659-help-on-sprites/

thank you for taking the time to help me out. You are an angel.

Victor

sure ill take a look at it this evening when i get back.

But real quick the isMoving is acting like a Toggle.

turrning it on and off ( true and false ) to start and stop the item from moving by preventing it from going through the code that updates the (X) position.

Larry

I know your just playing with the code above so here is a quick example of another button press event.

to stop event listener you need to remove it.

[lua]

local function buttonStopHandler()

        Runtime:removeEventListener( “enterFrame”, move );

    return true

end

[/lua]

Now if you wanted to get fancy you could always have the runtime event listener running like in your example above but change your code to set a switch based on you clicking the buttons when to set a flag like isMoving to true or false and then wrap the code that updates the ( .x ) of the object only if isMoving = true.

Example:

[lua]

local isMoving = false;

local function buttonHomeHandler()
    isMoving = true;
    return true;
end

local function buttonBackHandler()
    isMoving = true;
    return true;
end

local function buttonStopHandler()

        isMoving = false;

    return true

end

local tPrevious = system.getTimer()
local function move(event)
    local tDelta = event.time - tPrevious
    tPrevious = event.time

    local xOffset = ( 0.6 * tDelta )
    
    if isMoving = true then

        funny1.x = funny1.x - xOffset
        funny2.x = funny2.x - xOffset
    
        if (funny1.x + funny1.contentWidth) < 0 then
            funny1:translate( 600 * 2, 0)
        end
        if (funny2.x + funny2.contentWidth) < 0 then
            funny2:translate( 600 * 2, 0)
        end
    end

end
-------------------------------------------------------------------------------------------------------- Start everything moving
Runtime:addEventListener( “enterFrame”, move );

[/lua]

 

Hi doubleslashdesign:

the code with the if isMoving, I didn’t really get it.

but the problem I had to make it stop I got that by changing the position of the function.

I just put the function that removes the event listener below the add event listener, and it works!

Thanks you for that I own you two…

Would it be too much to ask for a third favor?

I have a question about removing a sprite image

http://forums.coronalabs.com/topic/36659-help-on-sprites/

thank you for taking the time to help me out. You are an angel.

Victor

sure ill take a look at it this evening when i get back.

But real quick the isMoving is acting like a Toggle.

turrning it on and off ( true and false ) to start and stop the item from moving by preventing it from going through the code that updates the (X) position.

Larry