Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Make a sprite disappear in a specific region using drag a drop — Gideros Forum

Make a sprite disappear in a specific region using drag a drop

a00zaa00za Member
edited March 2013 in General questions
i re-modified the drop function when the Mouse is Up so it will check the position of the dropped sprite if it is in the area of the region the sprite will be disappear if not the nothing happen.
here is the code:

function CLL:DragonMouseUpN(event)
if self.isFocus then
if( (self:getY()>(800))
and (self:getY()<(1000))
and (self:getX()<(900))
and (self:getX()<(1000))) then
self.nood:setVisible(false)
self.isFocus = false --unmark
event:stopPropagation()
else
self.isFocus = false --unmark
event:stopPropagation()
end
end
end

Comments

  • O.o

    The x and y coordinate seem too large, what is your device settings?
  • (self:getX()<(900)) and (self:getX()<(1000)) I think there is a problem here. Are you mean (self:getX()>(900)) and (self:getX()<(1000)) ??
  • a00zaa00za Member
    @mertocan thnx for the info i must have get confused.
    But I sill get the same error"attempt to index field 'nood' (a nil value)"
  • We need to see the code where you're setting the nood property. If nood sometimes doesn't exist you can add "if self.nood" check before self.nood:setVisible though.
  • So problem is your nood variable.I am not sure but you need to change it with self.nood or you didnt defined it in init function. define it in the init function like self.nood = ....

    Sorry for bad english :)
  • a00zaa00za Member
    i have defined it in the int()
    self.noodles=Bitmap.new(Texture.new("pics/nood.png"))
  • So change self.nood:setVisible(false) to self.noodles:setVisible(false)
  • a00zaa00za Member
    Sorry for the error in writing
    self.nood=Bitmap.new(Texture.new("pics/nood.png"))
  • Can you send the project? I think you are using nood variable instead of self.nood
  • a00zaa00za Member
    here is the file
  • a00zaa00za Member
    function Balaleetp3:init()
    application:setOrientation(Application.LANDSCAPE_LEFT)
    ------ADD images-------------------------------------------------
    --bg
    local bg = Bitmap.new(Texture.new("pics/bg.png"))
    self:addChild(bg)
    localScore = 0
    counter=1
    --pot
    pot=Bitmap.new(Texture.new("pics/pot.png"))
    pot:setAnchorPoint(0.5,0.5)
    pot:setPosition(980,900)
    print(pot:getHeight())
    self:addChild(pot)
    --fire
    fire = FireB.new()
    fire:setPosition(600,1100)
    self:addChild(fire)
    --steam
    steam=SteamB.new()
    steam:setPosition(660,350)
    self:addChild(steam)
    --[[splash
    splash=Bitmap.new(Texture.new("pics/splash.png"))
    splash:setAnchorPoint(0.5,0.5)
    splash:setPosition(0.64,0.28)
    self:addChild(splash)]]--
    self.oil=Bitmap.new(Texture.new("pics/oil_bottle.png"))
    self.oil:setAnchorPoint(0.5,0.5)
    self.oil:setPosition(1600,400)
    self:addChild(self.oil)
    --noodles
    self.noodles=Bitmap.new(Texture.new("pics/noodles.png"))
    self.noodles:setAnchorPoint(0.5,0.5)
    self.noodles:setRotation(120)
    self.noodles:setPosition(200,400)
    self:addChild(self.noodles)
    --------------------------Images end----------------------
    --------------------------Fade Ingredent------------------
    self:FadeIng()
    -------------------------Enf Fade Ingredient--------------
    --------------------------Event Listener------------------

    --noodles
    self.noodles:addEventListener(Event.MOUSE_DOWN, self.DragonMouseDown, self.noodles)
    self.noodles:addEventListener(Event.MOUSE_MOVE, self.DragonMouseMove, self.noodles)
    self.noodles:addEventListener(Event.MOUSE_UP, self.DragonMouseUpN, self.noodles)
    --[[oil
    self.oil:addEventListener(Event.MOUSE_DOWN, self.DragonMouseDown, self.oil)
    self.oil:addEventListener(Event.MOUSE_MOVE, self.DragonMouseMove, self.oil)
    self.oil:addEventListener(Event.MOUSE_UP, self.DragonMouseUp, self.oil)
    --]]
    --Fire
    --self:addEventListener(Event.TIMER, self.onTimer,self)


    -------------------------End Event Listener---------------

    end


    -------------------------Function----------------------------

    ------Drag functions
    function Balaleetp3:DragonMouseDown(event)
    if self:hitTestPoint(event.x, event.y) then
    self.isFocus = true -- mark to chk whether it is selected or not
    self.x0 = event.x--store initial position of touch
    self.y0 = event.y
    event:stopPropagation() --stop all the touch events or mouse event
    end
    end

    function Balaleetp3:DragonMouseMove(event)
    if self.isFocus then
    local dx = event.x - self.x0--get the diff of initial pos and current touch pos
    local dy = event.y - self.y0

    self:setX(self:getX() + dx) -- add this diff to img's x and y pos
    self:setY(self:getY() + dy)

    self.x0 = event.x -- change the initial pos with new one
    self.y0 = event.y

    event:stopPropagation()
    end
    end

    function Balaleetp3:DragonMouseUpN(event)
    if self.isFocus then
    if( (self:getY()>(610))
    and (self:getY()<(1190))
    and (self:getX()>(306))
    and (self:getX()<(1654))) then
    self.noodles:setVisible(false)
    self.isFocus = false --unmark
    event:stopPropagation()
    else
    self.isFocus = false --unmark
    event:stopPropagation()
    end
    end
    end
    -------------Fade Function
    function Balaleetp3:onTimer(timer)
    fade = Fade.new(self)
    stage:addChild(fade)
    end
    function Balaleetp3:FadeIng()
    --counter = counter + 1
    local IngNo= math.random(1,2)
    --noodles

    if (IngNo == 1) then
    local Nodmovie = MovieClip.new{
    {1,30, self.noodles, {alpha=0}},
    {31, 60, self.noodles, {alpha = {0, 1, "easeOut"}}
    }
    }
    Nodindex = self:getChildIndex(self.noodles)
    self:addChildAt(Nodmovie,Nodindex)
    Nodmovie:play()
    IngID=1
    elseif (IngNo == 2) then
    --oil

    Oilmovie = MovieClip.new{
    {1,30, self.oil, {alpha=0}},
    {31, 60, self.oil, {alpha = {0, 1, "easeOut"}}}
    }
    Oilindex = self:getChildIndex(self.oil)
    self:addChildAt(Oilmovie,Oilindex)
    Oilmovie:play()
    IngID=2
    end
    end
  • self.noodles:addEventListener(Event.MOUSE_DOWN, self.DragonMouseDown, self.noodles)
    self.noodles:addEventListener(Event.MOUSE_MOVE, self.DragonMouseMove, self.noodles)
    self.noodles:addEventListener(Event.MOUSE_UP, self.DragonMouseUpN, self.noodles)

    it should be

    self:addEventListener(Event.MOUSE_DOWN, self.DragonMouseDown, self)
    self:addEventListener(Event.MOUSE_MOVE, self.DragonMouseMove, self)
    self:addEventListener(Event.MOUSE_UP, self.DragonMouseUpN, self)

    because you are adding to self.noodles sprite and then trying to reach self.noodles.noodles
  • a00zaa00za Member
    edited March 2013
    i did what you said and the whole stage moved
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @a00za the difference is, if you use
    self.noodles:addEventListener(Event.MOUSE_UP, self.DragonMouseUpN, self.noodles)
    then inside DragonMouseUpN self references to self.noodles, so you need to do:
    function CLL:DragonMouseUpN(event)
        if self.isFocus then
            if your long if statement then self:setVisible(false)
        --etc
    end
    but if you do
    self.noodles:addEventListener(Event.MOUSE_UP, self.DragonMouseUpN, self)
    then self inside DragonMouseUpN is referenced to self as in all other methods (to CLL instance).
    Thus you have to do:
    function CLL:DragonMouseUpN(event)
        if self.isFocus then
            if( (self.noodles:getY()>(800)) --etc
    end
Sign In or Register to comment.