首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lua错误:试图调用全局'Class‘(布尔值)

lua错误:试图调用全局'Class‘(布尔值)
EN

Stack Overflow用户
提问于 2020-04-18 19:45:09
回答 2查看 962关注 0票数 0

我刚接触lua语言,.I已经从github下载了一个class.lua文件。我写了一个用于游戏开发的代码,通过使用love2d框架在lua中创建类。我正在制作一个需要球拍和球的乒乓球游戏,但它给了我这样的错误:

错误

代码语言:javascript
复制
Ball.lua:2: attempt to call global 'Class' (a boolean value)


Traceback  
Ball.lua:2: in main chunk  
[C]: in function 'require'  
main.lua:6: in main chunk  
[C]: in function 'require'  
[C]: in function 'xpcall'  
[C]: in function 'xpcall'  

我花了这么多时间来解决这个问题,但是什么也没有发生。

main.lua

代码为:

代码语言:javascript
复制
push =require 'push'
Class=require 'class'

require  "Ball"
require  "Paddle"
WINDOW_WIDTH=1280
WINDOW_HEIGHT=720

VIRTUAL_WIDTH=432
VIRTUAL_HEIGHT=243

--speed at which we will move our paddles; multiplied by dt in update
PADDLE_SPEED=200

function love.load()
    
 
    love.graphics.setDefaultFilter('nearest','nearest')
    --more 'retro-looking' font object we can use for any text
    --os.time() give the time from 1 jan 1970
    math.randomseed(os.time())
    smallFont=love.graphics.newFont('font.ttf',8)
    --set love2d active font to the small font object
    scoreFont=love.graphics.newFont('font.ttf' , 32)
    --larger font for drawing the score on the screen
    love.graphics.setFont(smallFont)
    --initialize window with virtual resolution 
    push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT,WINDOW_WIDTH,WINDOW_HEIGHT,{
        fullscreen=false,
        resizable=false,
        vsync=true
    })
    --Paddle(x,y,width,height)
    player1=Paddle(10,30,5,20)
    player2=Paddle(VIRTUAL_WIDTH-10,VIRTUAL_HEIGHT-30,5,20)
    --place a  ball in the middle of the screen 
    ball=Ball(VIRTUAL_WIDTH/2-2,VIRTUAL_HEIGHT/2-2,4,4)

    gameState='start'
end 
function love.update(dt)
    --player 1 movement
    if love.keyboard.isDown('w') then
        --add negative paddle speed to current Y scaled by deltaTime
        player1.dy=-PADDLE_SPEED
        --player1Y= math.max ( 0, player1Y + -PADDLE_SPEED * dt)
    elseif love.keyboard.isDown('s') then
        --add positive paddle speed to current Y scaled by deltaTime    
        player1.dy=PADDLE_SPEED
    else
        player.dy=0        
        --player1Y = math.min ( VIRTUAL_HEIGHT -20 , player1Y + PADDLE_SPEED * dt)    
    end
    --player 2 movement
    if love.keyboard.isDown('up') then
        --add negative paddle speed to current Y scaled by deltaTime
        player2.dy=-PADDLE_SPEED
        --player2Y=math.max( 0, player2Y + -PADDLE_SPEED * dt)
    elseif love.keyboard.isDown('down') then
        player2.dy=PADDLE_SPEED
    else
        player2.dy=0
    end
    
    if gameState=='play' then 
        ball:update(dt)
    end
    player1:update(dt)
    player2:update(dt)
end                 
function love.keypressed(key)
   --key can be acessed by sting name
   if key=='escape' then
         --function love give us to terminate application
         love.event.quit()
   elseif key == 'enter' or key == 'return' then
       if gameState == 'start' then
           gameState = 'play'
       else
           gameState = 'start'

           ball:reset()
       end   
   end     
end
function love.draw()
   --begin rendering at virtual screen
   push:apply('start')
   --recent versions  of pong has grey color it sets grey color
   love.graphics.clear( 0 , 0 , 0 , 255)
   --note we are now using virtual width and height now for text placement
   --draw welcome text  on top of the screen 
   love.graphics.setFont(smallFont)
   if gameState == 'start' then
      love.graphics.printf('hello start state!', 0 , 20 , VIRTUAL_WIDTH , 'center' )
   else
      love.graphics.printf('hello play state!', 0 , 20 , VIRTUAL_WIDTH , 'center')
   end
   love.graphics.setFont(scoreFont)
   love.graphics.print(tostring(player1Score),VIRTUAL_WIDTH / 2 - 50 , VIRTUAL_HEIGHT / 3)
   love.graphics.print(tostring(player2Score),VIRTUAL_WIDTH / 2 + 30 , VIRTUAL_HEIGHT / 3)

   player1:render()
   player2:render()

   ball:render()
   --end rendering at virtual resolution 
   push:apply('end')
end             

Ball.lua

代码为:

代码语言:javascript
复制
--making a Ball class
Ball = Class{}
function Ball:init(x,y,width,height)
    self.x=x
    self.y=y
    self.width=width
    self.height=height
    self.dy=math.random(2) ==1 and -100 or 100
    self.dx=math.random(-50,50)
end

--[[places the ball in the middle of the screen with an
initial random velocity on both axes.]]

function Ball:reset()
    self.x=VIRTUAL_WIDTH/2-2
    self.y=VIRTUAL_HEIGHT/2-2
    self.dy=math.random(2) == 1 and -100 or 100
    self.dx=math.random(-50,50)
end
function Ball:update(dt)
    self.x=self.x+self.dx * dt
    self.y=self.y+self.dy * dt
end
function Ball:render()
    love.graphics.rectangle('fill',self.x,self.y,self.width,self.height)
end     
return Ball

请在这件事上帮助我。

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-10-29 09:43:50

我在同样的CS50课程上也遇到了同样的问题。我第一次有这样的经历:

代码语言:javascript
复制
Class = require 'class'
push = require 'push'

require 'Ball'
require 'Paddle'

在main.lua中:

代码语言:javascript
复制
Paddle = class{}

在Paddle.lua中。

我通过改变来解决这个问题:

代码语言:javascript
复制
Class = require 'class'

代码语言:javascript
复制
require 'class'

代码语言:javascript
复制
Paddle = class{}

代码语言:javascript
复制
Paddle = class()

我必须阅读class.lua库中的自述文件才能理解它。

票数 1
EN

Stack Overflow用户

发布于 2020-07-16 17:20:39

看起来你在cs50在线课程的轨道上,我也有同样的情况。

当我下载发行版代码时,我发现class.lua文件不是空的。当我用我文件夹中的文件更改class.lua文件时,问题似乎已经解决了。

下载分发代码here

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

https://stackoverflow.com/questions/61288544

复制
相关文章

相似问题

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