sprite self. property values getting undefined in class function named :hit?

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

9/10 times when self values fail, the error is because you write .hit instead of :hit

reads title

You’re using a . instead of a :

I think you’ll find the problem is that your checking Ship:hit in your “IF” statement, it should be “self:hit” to represent the instance of the ship your checking.

@TechDojo, thanks. if Ship:hit → self.hit and all is good. I need new glasses… :wink: