I have this code and the :hit function is being called but it seems the self.x, self.y, self.with, self.height property values are getting nuked as I get error string ship = class() 40 attempt to perform arithmetic on field ‘width’ ( a nil value)
Ship = class()
function Ship:init(x, y, player, icon, shields, engines, angle)
self.x = x
self.y = y
self.player = player
self.icon = icon
self.image = readImage("Dropbox:"..self.icon)
self.width = 64
self.height = 64
self.shields = shields
self.engines = engines
self.angle = angle
self.weapon1 = "phasers"
self.weapon2 = "photontorpoes"
self.shoot=0
end
function Ship:draw()
pushMatrix()
translate(self.x, self.y)
rotate(self.angle)
sprite(self.image, 0, 0)
popMatrix()
--sprite(self.image, self.x, self.y)
end
function Ship:touched(touch)
if touch.state==BEGAN then
if Ship:hit(vec2(touch.x, touch.y)) then
print("ship touched")
print(self.icon)
end
end
end
function Ship:hit(point)
print(self.x)
if point.x > (self.x - self.width/2) and
point.x < (self.x + self.width/2) and
point.y > (self.y - self.height/2) and
point.y < (self.y + self.height/2) then
return true
end
return false
end
I use similar code in my other project and don’t understand how the self property values are nil. They are the values I expect because the draw function works just fine and it refers to self.property value in it.
Thanks in advance,
Gib