Qcode wrote:Oh it just seemed like quite a slow value for it to be falling at, In my opinion.
I may adjust this later.
Anyway, I'm trying to make my code a bit more concised (is that the word ?) by using Middleclass (sorry Roland

) but I'm getting an error, about trying to index a field a nil value or whatever that could mean. I tried many things but didn't get it to work. You'll find my .love attached but I'm not sure you'd need it.
Here's what's in my main.lua :
Code: Select all
love.load()
require("middleclass")
require("dynamics")
circle = CircleDynamic(400, 100, 25)
box = RectDynamic(500, 300, 50, 50)
player = CircleDynamic(100, 100, 20, true, 'ball.png')
...
end
love.update(dt)
player:movement(dt)
circle:movement(dt)
box:movement(dt)
...
end
And here is my dynamics.lua :
Code: Select all
Dynamic = class('Dynamic')
function Dynamic:initialize(shape, x, y, image)
self.shape = shape
if image then self.image = love.graphics.newImage(image) end
self.x = x
self.y = y
self.velocity = {x=0, y=0, a=0}
self.mtv = {x=0, y=0}
self.onGround = false
self.onBox = false
self.boxVelocity = {x=0, y=0}
self.isJumping = false
self.typ = 'dynamic'
end
function Dynamic:movement(dt) -- Function to move dynamic and player objects
if self.form == 'circle' then
if self.onGround or self.onBox then -- If a circle is on ground or on another dynamic, makes it rotate
self.velocity.a = self.velocity.x * (0.5-dt)
end
end
if not self.onGround and not self.onBox then
self.velocity.x = self.velocity.x * (1-dt) -- In air friction
else
--self.velocity.x = self.velocity.x * (1-dt) -- Slippery ground friction
self.velocity.x = self.velocity.x * (1-dt*10) -- Normal friction
self.velocity.y = 0
end
self.velocity.y = self.velocity.y + (1000 * dt) -- Gravity
if self.velocity.y > 500 then -- Set max velocity down
self.velocity.y = 500
end
if self.velocity.x > 0 and self.velocity.x < 1 then -- Useless ?
self.velocity.x = 0
end
if self.velocity.x > -1 and self.velocity.x < 0 then -- Useless ?
self.velocity.x = 0
end
if self.typ == 'player' then -- Makes player move with keyboard
if love.keyboard.isDown('right') then -- Makes self move right
self.velocity.x = 200
end
if love.keyboard.isDown('left') then -- Makes self move left
self.velocity.x = -200
end
end
self.x, self.y = self:center() -- Get self current position
if self.y >= 600 then -- Makes self unable to go offscreen
self:moveTo(self.x, 0)
end
if self.x > 800 then -- Makes self unable to go offscreen
self:moveTo(0, self.y)
elseif self.x < 0 then -- Makes self unable to go offscreen
self:moveTo(800, self.y)
end
self:move((self.velocity.x + self.boxVelocity.x) * dt, self.velocity.y * dt) -- Moves
if self.form == 'circle' then
self:rotate((self.velocity.a/(20*20*math.pi))*dt*100) -- Rotates the self if it's a circle
end
end
CircleDynamic = class('CircleDynamic', Dynamic)
function CircleDynamic:initialize(x, y, r, player, image)
shape = collider:addCircle(x, y, r)
Dynamic.initialize(shape, x, y, image)
if player then self.typ = 'player' end
self.form = 'circle'
end
RectDynamic = class('RectDynamic', Dynamic)
function RectDynamic:initialize(x, y, sx, sy, player, image)
shape = collider:addRectangle(x, y, sx, sy)
Dynamic.initialize(shape, x, y, image)
if player then self.typ = 'player' end
self.form = 'rectangle'
end
Here is the error I get : "Attempt to index field 'velocity' (a nil value)"
Related to this line :
Code: Select all
self.velocity.x = self.velocity.x * (1-dt) -- In air friction
in Dynamic:movement(dt) function in dynamics.lua.
Thanks in advance for any help I'll get

I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.