Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
prevent from dragging a shape over other shape — Gideros Forum

prevent from dragging a shape over other shape

newwork01newwork01 Member
edited April 2014 in Code snippets
Hi Everyone,
Im new to gideros . In Drag Me Example, I want to prevent from dragging a shape over other shape ( means it will stop when hit other shape ) . How can i implement the code ?

Comments

  • You can try TntCollision Engine or

    http://giderosmobile.com/forum/discussion/121/simple-collision-detection/p1

    to check for the collision if they collide stop moving them else allow

    :)
  • ar2rsawseenar2rsawseen Maintainer
    @hgvyas123 unfortunately thats not that easy, if you determine that they collide and stop move them, then you can't also move the figure back from the collided figure, because you will be ignoring all movements.

    You also need to take an account the side of collision, the vector, etc.

    more and more I started thinking that it might be easier to implement through dragging box2d bodies, unless I'm missing some obvious solution :D

    http://appcodingeasy.com/Gideros-Mobile/Dragging-Box2d-object-in-Gideros-Mobile

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • hmmm right little tweak needed here is the solution
    --[[
    Drag the shapes around with your mouse or fingers
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile
    ]]
     
    local myShapes = {}
     
    function Sprite:collidesWith(sprite2)
    	local x,y,w,h = self:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
     
    	return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
    end
     
    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
        self.lastX,self.lastY = self:getPosition()
    	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
    	for i=1,#myShapes do
    		if myShapes[i] ~= self then
    			if self:collidesWith(myShapes[i]) then
    				self:setPosition(self.lastX,self.lastY)
    			end
     
    		end
    	end
     
        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
      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
     
      shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape)
      shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape)
      shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape)
     
      stage:addChild(shape)
      table.insert(myShapes,shape)
    end
     
    local info = TextField.new(nil, "drag the shapes around with your mouse or fingers")
    info:setPosition(23, 50)
    stage:addChild(info)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • basically i store position of shape before moving it and later i check whether it collides or not and if they collide i reset shape's position to last stored value

    :)
  • ar2rsawseenar2rsawseen Maintainer
    @hgvyas123 hmm, yes that is a nice workaround, kudos! ;)

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.