"尝试索引本地' self' (零值)"我的类的索引变量

时间:2017-10-02 19:08:58

标签: lua

我知道这个问题在这个网站上已经存在了几次,但我的问题有点不同!所以...看到我的问题是我试图在我为我的玩家类写的绘图函数中获得一个值,但是当我试图通过self.such' n这样做时它会说尝试索引本地'自' (零值)函数对我的问题不重要(我希望)

require"functions"
Acc = 2000
Decel = 2500
Maxspd = 900

Player = {}
Player.__index = Player

function Player:new(x,y)
    local play = {}
    setmetatable(play, Player)
    play.inx = 0
    play.iny = 0
    play.dirx = 0
    play.diry = 0
    play.x = x
    play.y = y
    play.speedx = 0
    play.speedy = 0
    play.velx = 0
    play.vely = 0
    play.clr = "red"
    play.hp = 100
    play.rot = 0
    play.size = 40
    play.hoehe = math.sqrt(3) * (40 / 2)
    return play
end

function love.load()
    Main = Player:new(100,100)
end

function Player:update(d)
    if love.keyboard.isDown("w") then self.iny = -1
    elseif love.keyboard.isDown("s") then self.iny = 1 else self.iny = 0 end
    if love.keyboard.isDown("d") then self.inx = 1 
    elseif love.keyboard.isDown("a") then self.inx = -1 else self.inx = 0 end
    if self.inx == -self.dirx then self.speedx = self.speedx / 5 end
    if self.iny == -self.diry then self.speedy = self.speedy / 5 end
    self.dirx = self.inx
    self.diry = self.iny
    if self.inx ~= 0 then self.speedx = self.speedx + Acc * d
        else self.speedx = self.speedx - Decel * d end
    if self.iny ~= 0 then self.speedy = self.speedy + Acc * d
        else self.speedy = self.speedy - Decel * d end
    self.speedx = clamp(self.speedx,0,Maxspd)
    self.speedy = clamp(self.speedy,0,Maxspd)
    self.velx = self.speedx * d * self.dirx
    self.vely = self.speedy * d * self.diry
    self.x = self.x * d
    self.y = self.y * d
end

function Player:getX() return self.x end
function Player:getY() return self.y end

function Player:switch()
    if self.clr == "red" then self.clr = "blue" end
    if self.clr == "blue" then self.clr = "red" end
end

function Player:draw()
    local x1,x2,x3,y1,y2,y3 = 0,0,0,0,0,0
    x1 = self.x - self.size / 2
    y1 = self.y + self.hoehe / 2
    x2 = self.x + self.size/2
    y2 = self.y + self.hoehe / 2
    x3 = self.x
    y3 = self.y - self.hoehe / 2
    x1, y1 = rotate_around_point(x1, y1, self.x, self.y, self.rot)
    x2, y2 = rotate_around_point(x2, y2, self.x, self.y, self.rot)
    x3, y3 = rotate_around_point(x3, y3, self.x, self.y, self.rot)
    local verts = {x1,y1,x2,y2,x3,y3}
    if self.clr == "blue" then love.graphics.setColor(0,255,0,1) end
    if self.clr == "red" then love.graphics.setColor(255,0,0,1) end
    love.graphics.polygon("fill",verts)
end

function love.keypressed(key,scancode,isrepeat)
    if key == "space" then Main:switch() end
end

function love.update(d)
    Main:update(d)
end

function love.draw()
    Main.draw()
end

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,@ EgorSkriptunoff的答案仍然无效;将在此处发布答案而不是后代评论。

  

可能您正在以x.f()而不是x:f()的形式调用函数