Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Attempt to index a nil value — Gideros Forum

Attempt to index a nil value

sLKzsLKz Member
edited September 2014 in Bugs and issues
I'm getting the following error:
classes/stack.lua:38: attempt to index field 'element' (a nil value)
stack traceback:
classes/stack.lua:38: in function 'removeSprite'
classes/stack.lua:77: in function

Here is the code:
stack = Core.class(Sprite)
 
function stack:init(_elementsSize, _cubeSize, _positionX, _positionY)
	elementsSize = _elementsSize
	cubeSize = _cubeSize
 
	positionX = _positionX
	positionY = application:getContentHeight() - _positionY
 
	self.element = {}
 
	index = 1 
end
 
function stack:addSprite(sprite)
	if index <= elementsSize then
		self.element[index] = sprite
 
		if index >= 2 then
			self.element[index - 1]:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self.element[index - 1])
			self.element[index - 1]:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self.element[index - 1])
			self.element[index - 1]:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self.element[index - 1])
		end
 
		self.element[index]:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self.element[index])
		self.element[index]:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self.element[index])
		self.element[index]:addEventListener(Event.MOUSE_UP, self.onMouseUp, self.element[index])
 
		self.element[index]:setPosition(positionX, positionY - index * 55)
 
		self:addChild(self.element[index])
 
		index = index + 1
	end
end
 
function stack:removeSprite()
	self: removeChild(self.element[index])
 
	index = index - 1
 
	self.element[index]:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self.element[index])
	self.element[index]:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self.element[index])
	self.element[index]:addEventListener(Event.MOUSE_UP, self.onMouseUp, self.element[index])
end
 
function stack:onMouseDown(event)
	if self:hitTestPoint(event.x, event.y) then
		self.isFocus = true
 
		self.x0 = event.x
		self.y0 = event.y
 
		event:stopPropagation()
	end
end
 
function stack:onMouseMove(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
 
function stack:onMouseUp(event)
	if self.isFocus then
		if (event.x > 150) or (event.x < 100) then
			print("yes")
			self:removeSprite()
		end
 
		self.isFocus = false
 
		event:stopPropagation()
	end
end

Comments

  • @sLKz in your code you don't add event listeners to your stack class, but only to stack.elements.
    self.elements would onyl be reachable if you added mouse events like this:
    function stack:addSprite(sprite)
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
Sign In or Register to comment.