lua错误:尝试调用全局“类”(布尔值)
我不熟悉lua语言。我从github下载了class.lua文件。我用love2d框架在lua中创建类,为游戏开发编写了一个代码。我正在制作一个需要球拍和球的乒乓球游戏,但它给了我这样的错误: 错误 我花了很多时间来解决这个问题,但什么也没发生 梅因·卢阿 代码是:lua错误:尝试调用全局“类”(布尔值),lua,love2d,Lua,Love2d,我不熟悉lua语言。我从github下载了class.lua文件。我用love2d框架在lua中创建类,为游戏开发编写了一个代码。我正在制作一个需要球拍和球的乒乓球游戏,但它给了我这样的错误: 错误 我花了很多时间来解决这个问题,但什么也没发生 梅因·卢阿 代码是: push =require 'push' Class=require 'class' require "Ball" require "Paddle" WINDOW_WIDTH=1280
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
--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
巴尔·卢阿
代码是:
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
--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
请在这件事上帮助我。
提前谢谢。看来你正在学习cs50在线课程,我也遇到了同样的情况 当我下载分发代码时,我看到class.lua文件不是空的。当我用文件夹中的文件更改class.lua文件时,问题似乎得到了解决
下载分发代码我在同一个CS50课程中遇到同样的问题。我首先有:
Class = require 'class'
push = require 'push'
require 'Ball'
require 'Paddle'
在main.lua和:
Paddle = class{}
在桨上
我通过改变来解决这个问题:
Class = require 'class'
到
及
到
我必须阅读class.lua库中的自述文件才能找到答案。可能您忘记了将return class语句附加到module class.lua中
Paddle = class{}
Paddle = class()