Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Removing objects with events — Gideros Forum

Removing objects with events

ThuwizThuwiz Member
edited June 2016 in General questions
I'm trying to remove an object who is dragged inside a box but I'm having trouble understanding how to use these objects through my code. I'm getting used with Gideros and Lua. In Java, I could call my objects methods and do the verification and remove from the scene.

They way I'm trying to do is setting a global variable and when the onMouseUp is call in the box object I set the variable, but in my object Match, the event onMouseUp is no executing inside the box object. The hierarchy of the object could be the problem?

main.Lua
auxVar = false
 
function setValue(value)
	auxVar = value
end
 
function getValue()
	return auxVar
end
.
.
.
local teste = {1,1,1}
local teste2 = {2,2,2}
 
createMatchObj("box1", teste)
createMatchObj("box2", teste2)
 
createBox(25,350, "0xffbb15", "1")  --box1
createBox(175,350, "0xa2bb15","2") -- box2
Match.lua
.
.
.
function Match:onMouseUp(event)
    if self.isFocus then
		self.isFocus = false
		if getValue() then
			print("Object released inside the box")
			self:removeChild(self.downState)
		end
 
        event:stopPropagation()
    end
end
Box.lua
.
.
.
function Box:onMouseUp(event)
    if self:hitTestPoint(event.x, event.y) then
		--if self.isFocus then
			--self.isFocus = false
			if getValue == 0 then
				setValue(true)
			end
		--end
		--event:stopPropagation()
	end
end
English isn’t my first language, so please excuse any mistakes.

Comments

  • antixantix Member
    edited June 2016 Accepted Answer
    Just looking at your code quickly it seems that there is no checking to see which match got dropped into which box. I remade your code as an example of how this might work..

    All Boxes and Matches are now subclasses of Shape and only Matches capture any mouse events. When creating a Match, it gets passed a table containing all of all boxes.

    Whenever a Match receives a MouseUp event, it checks each box in the table and if it was released inside a box (and the box has the same id), then it removes its self from the stage (also removing the label, shape data, and event listeners).

    I hope you can follow the example and find it of some use :)
    local boxes = {}
     
    table.insert(boxes, Box.new(25, 350, 0xffbb15, 1))
    table.insert(boxes, Box.new(175, 350, 0xa2bb15, 2))
     
    for i = 1, 3 do
    	Match.new(1, boxes)
    end
     
    for i = 1, 3 do
    	Match.new(2, boxes)
    end
    Box = Core.class(Shape)
     
    function Box:init(x, y, color, id)
    	self.id = id
     
    	self:setLineStyle(3, 0x000000)
    	self:setFillStyle(Shape.SOLID, color, 0.5)
    	self:beginPath()
    	self:moveTo(0, 0)
    	self:lineTo(120, 0)
    	self:lineTo(120, 50)
    	self:lineTo(0, 50)
    	self:closePath()
    	self:endPath()
    	self:setPosition(x, y)
     
    	-- Create the label and attach it to the shape
        local label = TextField.new(nil, id)
        label:setPosition(self:getWidth()/2 - label:getWidth()/2, self:getHeight()/2 + label:getHeight()/2)
        self:addChild(label)
     
    	stage:addChild(self) -- add everything to the stage
     
    	return self
    end
    Match = Core.class(Shape)
     
    function Match:init(id, boxes)
    	self.id = id
    	self.boxes = boxes
     
    	self:setLineStyle(3, 0x000000)
    	self:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
    	self:beginPath()
    	self:moveTo(0, 0)
    	self:lineTo(100, 0)
    	self:lineTo(100, 25)
    	self:lineTo(0, 25)
    	self:closePath()
    	self:endPath()
    	self:setPosition(math.random(0, 240 - 50), math.random(0, 340 - 50))
     
    	-- Create the label and attach it to the shape
        local label = TextField.new(nil, id)
        label:setPosition(self:getWidth()/2 - label:getWidth()/2, self:getHeight()/2 + label:getHeight()/2)
        self:addChild(label)
     
    	stage:addChild(self) -- add everything to the stage
     
    	self.isFocus = false
    	self.enabled = true
     
        self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
        self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
        self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
     
    	return self
    end
     
    function Match:onMouseDown(event)
    	if not self.enabled then return end
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true		
    		event:stopPropagation()
     
    		self.x0 = event.x
            self.y0 = event.y
    	end
    end
     
    function Match:onMouseMove(event)
    	if not self.enabled then return end
        if self.isFocus then
            event:stopPropagation()
     
            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
        end
    end
     
    function Match:onMouseUp(event)
    	if not self.enabled then return end
     
    	-- Return true if point is inside rect
    	local function pointinrect(x1, y1, x2, y2, w, h)
    		return x1 >= x2 and y1 >= y2 and x1 <= x2 + w and y1 <= y2 + h
    	end
     
        if self.isFocus then
    		self.isFocus = false
            event:stopPropagation()
     
    		local boxes = self.boxes -- Check each box against this match
    		for i = 1, #boxes do
    			local box = boxes[i]
    			local x, y, w, h = box:getBounds(stage)
    			if pointinrect(self.x0, self.y0, x, y, w, h) then
    				if box.id == self.id then
     
    					print("Match Dropped in box with same id")
     
    					self.enabled = false
    					self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) -- remove event listeners
    					self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    					self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    					self:removeChildAt(1) -- remove label
    					self:clear() -- get rid of all shape stuff
    					self:removeFromParent() -- remove from parent (stage)
    				else
     
    					print("Match dropped into wrong box")
     
    				end
    			end
    		end
        end
    end
    zip
    zip
    Match 2.zip
    3K

    Likes: Thuwiz

    +1 -1 (+1 / -0 )Share on Facebook
  • ThuwizThuwiz Member
    @antix, thank you so much for the detailed explanation! Sorry for the newbie questions it's not been too long since I get my head around with game programming

    Thanks again.
    English isn’t my first language, so please excuse any mistakes.
Sign In or Register to comment.