Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
tell me about self — Gideros Forum

tell me about self

rancarirancari Member
edited February 2014 in General questions
I think it's dumb question. But, it is really important for my on going project. OK, start from script. Basically, it's just modification from "Drag Me" example.
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:setColorTransform(1,0.7,0.2,1)
		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
 
	--this is my problem
	if event.x>80 and event.x <120 then self:setX(80) end
end
 
local n = 5
local lett = {}
for i=1,n do 
lett[i] = Shape.new()
lett[i]:setFillStyle(Shape.SOLID,0x566765,0.5)
lett[i]:setLineStyle(1, 0x0066FF, 2)
lett[i]:beginPath()
lett[i]:moveTo(0, 0)
lett[i]:lineTo(0, 50)
lett[i]:lineTo(50, 50)
lett[i]:lineTo(50, 0)
lett[i]:lineTo(0, 0)
lett[i]:endPath()
lett[i].isFocus = false
stage:addChild(lett[i])
lett[i]:setPosition(i*60-40,26)
lett[i]:addEventListener(Event.MOUSE_DOWN, onMouseDown, lett[i])
lett[i]:addEventListener(Event.MOUSE_MOVE, onMouseMove, lett[i])
lett[i]:addEventListener(Event.MOUSE_UP, onMouseUp, lett[i])
end
The problem here is onMouseUp function. When I try the last shape, it's fine. But, if I move #3 shape (lett[3]), two following shape: lett[4] and lett[5] move like lett[3]. I want to make that only lett[3] move. So, what does the "self" mean?

Comments

  • Are you shapes overlapping each other?
  • edited February 2014 Accepted Answer
    @rancari You should put
    if event.x>80 and event.x <120 then self:setX(80) end
    before
    event:stopPropagation()<pre escaped='true' lang="lua">
    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:setColorTransform(1,0.7,0.2,1)
    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
    --this is my problem
    print(event.x)
    if event.x>80 and event.x <120 then self:setX(80) end
    event:stopPropagation()
    end

    end

    local n = 5
    local lett = {}
    for i=1,n do
    lett[i] = Shape.new()
    lett[i]:setFillStyle(Shape.SOLID,0x566765,0.5)
    lett[i]:setLineStyle(1, 0x0066FF, 2)
    lett[i]:beginPath()
    lett[i]:moveTo(0, 0)
    lett[i]:lineTo(0, 50)
    lett[i]:lineTo(50, 50)
    lett[i]:lineTo(50, 0)
    lett[i]:lineTo(0, 0)
    lett[i]:endPath()
    lett[i].isFocus = false
    stage:addChild(lett[i])
    lett[i]:setPosition(i*60-40,26)
    lett[i]:addEventListener(Event.MOUSE_DOWN, onMouseDown, lett[i])
    lett[i]:addEventListener(Event.MOUSE_MOVE, onMouseMove, lett[i])
    lett[i]:addEventListener(Event.MOUSE_UP, onMouseUp, lett[i])
    end
    Coming soon
  • rancarirancari Member
    edited February 2014
    @chang - yes, they're overlapping
    @vitalitymobile - thanks for your suggestion.solved
Sign In or Register to comment.