Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Adding functions to onEnterFrame — Gideros Forum

Adding functions to onEnterFrame

KarrizKarriz Member
edited May 2013 in General questions
Hi,
I'm trying to create a code that applies a force to the player body when mouse (or finger) is held down. Here's what I 've got, but it doesn't work:

function self:onMouseDown(event)
local mousedown = true
print("mousedown")
end

function self:onMouseUp(event)
local mousedown = false
print("mouseup")
end

function scene:onEnterFrame()
if mousedown == true then
self.ball.body:applyLinearImpulse(forcex, forcey , self.ball:getX(), self.ball:getY())
print("linearimpulse")
end
end

I'm having problems with the code inside onEnterFrame. Mousedown and mouseup events show up in the print output, but "linearimpulse" does not, so it clearly doesn't do anything. I'm probably missing something obvious, but maybe someone can point me into the right direction?

If I remove the whole "if" thing, the force is being applied correctly in every frame. However, I only want to apply it when the mouse is being held down.

Comments

  • petecpetec Member
    edited May 2013 Accepted Answer
    You've made mousedown local to the onMouseDown and onMouseUp functions so it won't be recognised in the onEnterFrame function. Define local mousedown outside the functions and then just use mousedown=false and mousedown=true in the first two functions. I think that should do it.

    Likes: Karriz

    +1 -1 (+1 / -0 )Share on Facebook
  • KarrizKarriz Member
    Thanks, I thought "local" was referring to the current scene, but now things work fine after I removed it.
  • ar2rsawseenar2rsawseen Maintainer
    @Karriz local is referred to the smallest scope it is defined in.

    if you define it inside function, it is only for this function
    if you define it simply in file, it is only for this file.

    If you want something to be for the scene, then you must make it as a property of scene class, usually something like that:
    function scene:onMouseDown(event)
        self.mousedown = true
        print("mousedown")
    end
     
    function scene:onMouseUp(event) 
        self.mousedown = false
        print("mouseup")
    end
     
    function scene:onEnterFrame()
        if self.mousedown == true then
             self.ball.body:applyLinearImpulse(forcex, forcey , self.ball:getX(), self.ball:getY())
             print("linearimpulse")
        end
    end
Sign In or Register to comment.