Page 1 of 1

Code wont detecting values

Posted: Thu Dec 15, 2022 10:13 am
by SuperCrips
code:
Player = {}

function Player:load()
self.x = 100
self.y = 0
self.width = 20
self.height = 60
self.xVel = 0
self.yVel = 0
self.maxSpeed = 200
self.acceleration = 4000
self.friction = 3500

self.physics = {}
self.physics.body = love.physics.newBody(World, self.x, self.y, "dynamic")
self.physics.body:setFixedRotation(true)
self.physics.shape = love.physics.newRectangleShape(self.width, self.height)
self.physics.fixture = love.physics.newFixture(self.physics.body, self.physics.shape)
end

function Player:update(dt)
self.syncPhysics()
end

function Player:syncPhysics()
self.x, self.y = self.physics.body:getPosition()
self.physics.body:setLinearVelocity(self.xVel, self.yVel)
end

function Player:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

Issue:
In function Player:draw()
the "love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)" line when compiling code cant access the values which added in function Player:load like self.x, self.y, etc.

Error

player.lua:31: bad argument #2 to 'rectangle' (number expected, got nil)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'rectangle'
player.lua:31: in function 'draw'
main.lua:23: in function 'draw'
[love "callbacks.lua"]:168: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

(Using STI)

Re: Code wont detecting values

Posted: Thu Dec 15, 2022 7:29 pm
by darkfrei

Code: Select all

function Player:update(dt)
  self:syncPhysics() -- was function, not method
end
You can disable (comment) update to be sure that it has no destructive elements.

Otherwise attach the .love file, not just (probably) the part without errors.