首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“尝试索引本地'self‘(一个零值)”不能对我的类的变量进行索引

“尝试索引本地'self‘(一个零值)”不能对我的类的变量进行索引
EN

Stack Overflow用户
提问于 2017-10-02 19:08:58
回答 1查看 1.2K关注 0票数 0

我知道这个问题在这个网站上已经存在过几次了,但是我的问题有点不同!所以..。看到我的问题是,我试图在我为我的玩家类写的一个绘图函数中得到一个值,但是当我试图用自己得到任何东西时,它表示试图索引本地的'self‘(一个零值),这些函数对我的问题并不重要(我希望)

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-08-27 22:04:03

我也遇到了同样的问题,@EgorSkriptunoff的回答已经过时;我打算在这里发帖,作为回答,而不是对后人的评论。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46532119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档