lua尝试索引“全局”;“静态体”;(布尔值)
我尝试在lua中对对象进行一些实验,因此我尝试创建一个StaticBody、一个碰撞和一个Hitbox对象,但当我在创建StaticBody的行中启动main时,它抛出了一个错误:“main.lua:4尝试索引全局“StaticBody”(布尔值) 这是密码lua尝试索引“全局”;“静态体”;(布尔值),lua,love2d,Lua,Love2d,我尝试在lua中对对象进行一些实验,因此我尝试创建一个StaticBody、一个碰撞和一个Hitbox对象,但当我在创建StaticBody的行中启动main时,它抛出了一个错误:“main.lua:4尝试索引全局“StaticBody”(布尔值) 这是密码 --main.lua StaticBody = require"StaticBody" collision = require"collision" player = StaticBody.StaticBody:new(300,
--main.lua
StaticBody = require"StaticBody"
collision = require"collision"
player = StaticBody.StaticBody:new(300, 200, 40, 30, "RECT", 2000, 5000, 8.9, 900, 900, 5)
function love.load()
playerimg = love.graphics.newImage("player.png")
player:setSprite(playerimg, 1, 1, 0, 0)
end
function love.draw()
player:draw()
end
function love.update(d)
if love.keyboard.isDown("a") then
input = -1
end
if love.keyboard.isDown("d") then
input = 1
end
if not love.keyboard.isDown("a") and not love.keyboard.isDown("d") then
input = 0
end
player:update(d,input)
end
function love.keypressed( key, scancode, isrepeat)
if key == "space" then
player:jump(800)
end
end
-----------------------------------------------------------------------------------------------------------------
--StaticBody.lua
-----------------------------------------------------------------------------------------------------------------
StaticBody = {x = 0, y = 0, w = 1, h = 1, speedx = 0, speedy = 0, maxxspd = 1, maxyspd = 1, collosionmode = "RECT", sprite = nil, scalex = 1,
scaley = 1, offsetx = 0, offsety = 0, acc = 1, decel = 1, grav = 1, velx = 0, colbox = nil, hitbox = nil, turningfriction = 2}
collision = require"collision"
function clamp(variable,vmin,vmax)
if variable < vmin then
variable = vmin
end
if variable > vmax then
variable = vmax
end
end
function StaticBody:new(x, y, w, h, colmode, acc, decel, grav, maxxspd, maxyspd, turningfriction)
setmetatable({},StaticBody)
self.x = x
self.y = y
self.w = w
self.h = h
self.hitbox = collision.hitbox:new(x,y,w,h,false)
self.colbox = collision.ColObjekt:new(colmode,self.hitbox)
self.acc = acc
self.decel = decel
self.grav = grav
self.maxxspd = maxxspd
self.maxyspd = maxyspd
self.speedx = 0
self.speedy = 0
self.turningfriction = turningfriction
self.velx = 0
return self
end
function StaticBody:setSprite(sprite, scalex, scaley, offsetx, offsety)
self.sprite = sprite
self.scalex = scalex
self.scaley = scaley
self.offsetx = offsetx
self.offsety = offsety
end
function StaticBody:draw()
love.graphics.draw(self.sprite,self.x,self,y,0,self.scalex,self.scaley,self.offsetx,self.offsety,0,0)
end
function StaticBody:update(d,input)
local dir = 0
if input == -dir then
self.speedx = self.speedx / self.turningfriction
end
if input then
dir = input
end
if not self.colbox:getOnGround() then
self.speedy = self.speedy + self.grav * d
end
if input ~= 0 then
self.speedx = self.speedx + self.acc * d
else
self.speedx = self.speedx - self.decel * d
end
clamp(self.speedx,0,self.maxxspd)
clamp(self.speedy,0,self.maxyspd)
self.velx = self.speedx * d * dir
self.y = self.y + self.speedy * d
self.x = self.x + self.speedx * d * dir
end
function StaticBody:jump(jumpforce)
self.speedy = -jumpforce
end
------------------------------------------------------------------------------------------------
--collision.lua
------------------------------------------------------------------------------------------------
ColObjekts = {}
ColObjekt = {mode, x, y, w, h, collisionObjekts = ColObjekts}
--Constructer
function ColObjekt:new(mode,x,y,w,h,collisionObjekts)
setmetatable({},ColObjekt)
self.mode = mode
self.collisionObjekts = collisionObjekts
self.x = x
self.y = y
self.w = w
self.h = h
ColObjekts.push(self)
return self
end
--Constructer with Hitbbox
function ColObjekt:new(mode,Hb,collisionObjekts)
setmetatable({},ColObjekt)
self.mode = mode
self.collisionObjekts = collisionObjekts
self.x = Hb.x
self.y = Hb.y
self.w = Hb.w
self.h = Hb.h
end
function ColObjekt:setcolObjekts(colobs)
self.collisionObjekts = colobs
end
function ColObjekt:getColliding()
for key,i in pairs(self.collisionObjekts) do
if getCollidingWith(self,i) then
return true
end
return false
end
end
function ColObjekt:getCollidingWhich()
for key,i in pairs(self.collisionObjekts) do
if getCollidingWith(self,i) then
return true, i
end
return false
end
end
function ColObjekt:getCollidingDir(Colob1,Colob2)
if not getCollidingWith(Colob1,Colob2) then
return false
elseif Colob1.y > Colob2.y then return "ONTOP"
elseif Colob1.x < Colob2.x and Colob1.y > Colob2.y+Colob2.h then return "LEFT"
elseif Colob1.x > Colob2.x and Colob1.y > Colob2.y+Colob2.h then return "RIGHT"
else return "UNDER"
end
end
function ColObjekt:getOnGround()
if self:getCollidingDir(self,self:getCollidingWhich()) == "ONTOP" then
return true
else
return false
end
function getCollidingWith(Colob1,Colob2)
--if Colob1 == Rectangle
if Colob1.mode == "RECT" then
if Colob2.mode == "RECT" then
ColRect_Rect(Colob1,Colob2)
end
if Colob2.mode == "CIRCLE" then
ColRect_Circle(Colob2,Colob1)
end
end
--if Colob1 == Circle
if Colob1.mode == "CIRCLE" then
if Colob2.mode == "CIRCLE" then
ColCircle_Circle(Colob1,Colob2)
end
if Colob2.mode == "RECT" then
ColRect_Circle(Colob1,Colob2)
end
end
end
function ColRect_Edges(c,r)
--OBEN
if c.x > r.x and c.x < r.x + r.w
and c.y > r.y - c.h and c.y < r.y then return true
--UNTEN
elseif c.x > r.x and c.x < r.x + r.w
and c.y > r.y + r.h - c.h and c.y < r.y + r.h then return true
--LINKS
elseif c.y > r.y and c.y < r.y + r.h
and c.x > r.x - c.w and c.x < r.x then return true
--RECHTS
elseif c.y > r.y and c.y < r.y + r.h
and c.x > r.x + r.w - c.w and c.x < r.x + r.w then return true
--WENN NICHTS ZUTRIFFT
else return false end
end
function ColRect_Corners(c,r)
if math.sqrt((c.x - r.x * c.x - r.x) + (c.y - r.y * c.y - r.y)) < c.w/2 then
return true
elseif math.sqrt((c.x - (r.x + r.w) * c.x - (r.x + r.w)) + (c.y - r.y * c.y - r.y)) < c.w/2 then
return true
elseif math.sqrt((c.x - (r.x + r.w) * c.x - (r.x + r.w)) + (c.y - (r.y + r.h) * c.y - (r.y + r.h))) < c.w/2 then
return true
elseif math.sqrt((c.x - r.x * c.x - r.x) + (c.y - (r.y + r.h) * c.y - (r.y + r.h))) then
return true
else
return false
end
end
function ColRect_Rect(r1,r2)
if r1.x < r2.x + r2.w and r1.x > r2.x - r1.x and r1.y > r2.y - r1.h and r1.y < r2.y + r2.h then
return true
end
return false
end
function ColCircle_Circle(c1,c2)
local scndx = c1.x - c2.x
local scndy = c1.y - c2.y
if math.sqrt((scndx * scndx) + (scndy * scndy)) < c1.w/2 + c2.w/2 then
return true
end
return false
end
function ColRect_Circle(c,r)
if c.x > r.x and c.x < r.x + r.w
and c.y > r.y and c.y < r.y + r.h then
return true
end
if ColRect_Corners(c,r) then
return true
end
if ColRect_Edges(c,r) then
return true
end
return false
end
Hitbox = {x,y,w,h,show = false}
function Hitbox:new(x,y,w,h,show)
setmetatable({},Hitbox)
self.x = x
self.y = y
self.w = w
self.h = h
self.show = show
return self
end
function Hitbox:update()
if self.show then
love.graphics.Rectangle("fill",self.x,self.y,self.w,self.h)
end
end
function Hitbox:setVisible(b)
self.show = b
end
end
-main.lua
StaticBody=需要“StaticBody”
碰撞=需要“碰撞”
player=StaticBody.StaticBody:new(300、200、40、30,“RECT”、2000、5000、8.9900、900、5)
函数love.load()
playerimg=love.graphics.newImage(“player.png”)
球员:赛斯普林特(球员,1,1,0,0)
结束
函数love.draw()
球员:平局()
结束
函数love.update(d)
如果love.keyboard.isDown(“a”)那么
输入=-1
结束
如果爱.keyboard.isDown(“d”),那么
输入=1
结束
如果不是love.keyboard.isDown(“a”)而不是love.keyboard.isDown(“d”),那么
输入=0
结束
播放器:更新(d,输入)
结束
功能爱。按键按下(键,扫描码,isrepeat)
如果键==“空格”,则
运动员:跳跃(800)
结束
结束
-----------------------------------------------------------------------------------------------------------------
--静态体
-----------------------------------------------------------------------------------------------------------------
StaticBody={x=0,y=0,w=1,h=1,speedx=0,speedy=0,maxxspd=1,maxyspd=1,collosionmode=“RECT”,sprite=nil,scalex=1,
scaley=1,offsetx=0,offsety=0,acc=1,decel=1,grav=1,velx=0,colbox=nil,hitbox=nil,turning摩擦力=2}
碰撞=需要“碰撞”
功能钳位(变量、vmin、vmax)
如果变量vmax,则
变量=vmax
结束
结束
功能静态体:新(x、y、w、h、colmode、acc、decel、grav、maxxspd、maxyspd、旋转摩擦)
setmetatable({},StaticBody)
self.x=x
self.y=y
self.w=w
self.h=h
self.hitbox=冲突。hitbox:new(x,y,w,h,false)
self.colbox=collision.ColObjekt:new(colmode,self.hitbox)
self.acc=acc
self.decel=减速
self.grav=grav
self.maxxspd=maxxspd
self.maxyspd=maxyspd
self.speedx=0
self.speeded=0
自转摩擦=自转摩擦
self.velx=0
回归自我
结束
函数StaticBody:setSprite(sprite、scalex、scaley、offsetx、offsety)
self.sprite=精灵
self.scalex=scalex
self.scaley=scaley
self.offsetx=offsetx
self.offsety=offsety
结束
函数StaticBody:draw()
love.graphics.draw(self.sprite,self.x,self,y,0,self.scalex,self.scaley,self.offsetx,self.offsety,0,0)
结束
函数StaticBody:更新(d,输入)
本地目录=0
如果输入==-dir,则
self.speedx=self.speedx/self.turning摩擦力
结束
如果输入,则
dir=输入
结束
如果不是self.colbox:getOnGround()那么
self.speedy=self.speedy+self.grav*d
结束
如果输入~=0,则
self.speedx=self.speedx+self.acc*d
否则
self.speedx=self.speedx-self.decel*d
结束
钳位(self.speedx,0,self.maxxspd)
钳位(自速,0,自maxyspd)
self.velx=self.speedx*d*dir
self.y=self.y+self.d
self.x=self.x+self.speedx*d*dir
结束
功能静态体:跳跃(跳跃力)
self.speedy=-jumpforce
结束
------------------------------------------------------------------------------------------------
--卢阿
------------------------------------------------------------------------------------------------
ColObjekts={}
ColObjekt={mode,x,y,w,h,collisionObjekts=ColObjekts}
--建设者
函数ColObjekt:新(模式、x、y、w、h、碰撞对象)
setmetatable({},ColObjekt)
self.mode=mode
self.collisionObjekts=collisionObjekts
self.x=x
self.y=y
self.w=w
self.h=h
ColObjekts.push(自)
回归自我
结束
--带Hitbbox的构造函数
功能ColObjekt:新(模式、Hb、碰撞对象)
setmetatable({},ColObjekt)
self.mode=mode
self.collisionObjekts=collisionObjekts
self.x=Hb.x
self.y=Hb.y
self.w=Hb.w
self.h=Hb.h
结束
函数ColObjekt:setcolObjekts(colobs)
self.collisionObjekts=冒号
结束
函数ColObjekt:getCollinding()
对于键,我成对(self.collisionObjekts)执行
如果与(自我,我)发生冲突,那么
返回真值
结束
返回错误
结束
结束
函数ColObjekt:getCollidingWhich()
对于键,我成对(self.collisionObjekts)执行
如果与(自我,我)发生冲突,那么
我相信这是真的
结束
返回错误
结束
结束
函数ColObjekt:getCollidingDir(Colob1、Colob2)
如果未与(Colob1、Colob2)发生冲突,则
返回错误
elseif Colob1.y>Colob2.y然后返回“ONTOP”
elseif Colob1.xColob2.y+Colob2.h,然后返回“LEFT”
elseif Colob1.x>Colob2.x和Colob1.y>Colob2.y+Colob2.h,然后返回“RIGHT”
否则返回“下”
结束
结束
函数ColObjekt:getOnGround()
如果self:getCollidingDir(self,self:getCollidingWhich())=“ONTOP”,那么
返回真值
否则
返回错误
结束
函数getCollidingWith(Colob1、Colob2)
--如果Colob1==矩形
如果Colob1.mode==“RECT”,则
如果Colob2.mode==“RECT”,则
ColRect_Rect(Colob1,Colob2)
结束
如果Colob2.mode==“圆圈”,则
ColRect_圆(Colob2,Colob1)
结束
结束
--如果Colob1==圆
如果Colob1.mode==“圆圈”,则
如果Colob2.mode==“圆圈”,则
package.loaded.StaticBody = StaticBody