Code wont detecting values

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
SuperCrips
Prole
Posts: 1
Joined: Thu Dec 15, 2022 10:09 am

Code wont detecting values

Post 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)
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Code wont detecting values

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests