Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to pass variables through Event listeners - The prequel — Gideros Forum

How to pass variables through Event listeners - The prequel

saeyssaeys Member
edited June 2015 in General questions
Hi boys and girls,
I know there is a discussion about this, but I would like to make a fresh start (among other things because that discussion went over my head a bit and my problem relates closely to the Dragme example project, see below.

I would like to have the "i" variable (from the for loop) returned when one of the functions are executed. So the code keeps track of which square is pressed on, moved and released. I don't seem to be able to pass the variable through the event listener.

One solution (I think, haven't tried it) is maybe to embed the function lines in the addEventListener methods but I don't want to do that because I would like to remove the event listeners at a certain point later.

Any suggestions?
local function onMouseDown(self, event)
	if self:hitTestPoint(event.x, event.y) then
		self.isFocus = true
		self.x0 = event.x
		self.y0 = event.y
		event:stopPropagation()
	end
end
 
local function onMouseMove(self, event)
	if self.isFocus then
		local dx = event.x - self.x0
		local dy = event.y - self.y0
		self:setX(self:getX() + dx)
		self:setY(self:getY() + dy)
		self.x0 = event.x
		self.y0 = event.y
		event:stopPropagation()
	end
end
 
local function onMouseUp(self, event)
	if self.isFocus then
		self.isFocus = false
		event:stopPropagation()
	end
end
 
for i=1,5 do
	shape = Shape.new()
 	shape:setLineStyle(3, 0x000000)
	shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
	shape:beginPath()
	shape:moveTo(0, 0)
	shape:lineTo(100, 0)
	shape:lineTo(100, 50)
	shape:lineTo(0, 50)
	shape:closePath()
	shape:endPath()
	shape:setX(math.random(0, 320 - 100))
	shape:setY(math.random(0, 480 - 50))
	shape.isFocus = false
	stage:addChild(shape)
	shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape)
	shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape)
	shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape)
end

Comments

  • edited June 2015
    Firstly, you should take care about Global variable, in this code, you are making shape as global variable, so may be you should fix this, as comment in code bellow.
    For "i" variable, because shape is table, so you can assign i to shape, and get it later.
     
    local function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then
    		-- We can get "i" like this
    		print("onMouseDown",self.id)
    		self.isFocus = true
    		self.x0 = event.x
    		self.y0 = event.y
    		event:stopPropagation()
    	end
    end
     
    local function onMouseMove(self, event)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
    		self:setX(self:getX() + dx)
    		self:setY(self:getY() + dy)
    		self.x0 = event.x
    		self.y0 = event.y
    		event:stopPropagation()
    	end
    end
     
    local function onMouseUp(self, event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    	end
    end
     
     
    local shapes = {}
    for i=1,5 do
     	--1 Should have local here
     	local shape = Shape.new()
     
     	shape:setLineStyle(3, 0x000000)
    	shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
    	shape:beginPath()
    	shape:moveTo(0, 0)
    	shape:lineTo(100, 0)
    	shape:lineTo(100, 50)
    	shape:lineTo(0, 50)
    	shape:closePath()
    	shape:endPath()
    	shape:setX(math.random(0, 320 - 100))
    	shape:setY(math.random(0, 480 - 50))
    	shape.isFocus = false
    	stage:addChild(shape)
     
    	-- save id here for later use
    	shape.id = i;
    	shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape)
    	shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape)
    	shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape)
    	-- if you want control shape later 
    	--(remove listener, destroy or do something else) , add shape into shapes table
    	shapes[i] = shape;
    	-- or shapes[#shapes + 1] = shape
    end
    Coming soon
  • I recommend using an object-oriented approach.
     
    GameLogic = Core.class()
     
    function GameLogic:init()
        -- init logic
    end
     
    function GameLogic:run()
        local shapes = {}
        for i=1,5 do
            self.CurrentIteration = i
            ....
        end
    end
     
    function GameLogic:_onMouseDown(self, event)
    	-- use self.CurrentIteration
    end
     
    function GameLogic:_onMouseMove(self, event)
    	-- use self.CurrentIteration
    end
     
    function GameLogic:_onMouseUp(self, event)
    	-- use self.CurrentIteration
    end

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • saeyssaeys Member
    @vitalitymobile @asakharov
    Thank you so much, worked well in my own implementation! I have still to learn about variable names, what is what on either side of a . (dot) etc
Sign In or Register to comment.