I'm new to this forum & also new to this language & game engine, i'm learning a course on udemy, In that course where I have to avoid multiple jumps(i.e the player should not jump when he is in air or not in contact with other objects, I got a error which Is shown in the image, I don't know whether It is a silly error, so please see it. And also could you please explain the logic in this jumping section, The tutor created a last 2 functions for this in main.lua & I still can't understand It & he is a good tutor by the way.
Main.lua
Code: Select all
function love.load(arg)
--creating physics--
myWorld=love.physics.newWorld(0, 500, sleep)
myWorld:setCallBacks(beginContact, endContact, preSolve, postSolve)
--importing sprites--
sprites= {}
sprites.coin_sheet= love.graphics.newImage('sprites/coin_sheet.png')
sprites.player_jump=love.graphics.newImage('sprites/player_jump.png')
sprites.player_stand=love.graphics.newImage('sprites/player_stand.png')
require('player')
--creating function for platforms
platforms = {}
spawnPlatform(50,400, 300, 30)
end
function love.update(dt)
--calling the physics(note the colon not equal between myworld & update)--
myWorld:update(dt)
playerUpdate(dt)
end
function love.draw()
love.graphics.draw(sprites.player_stand, player.body:getX(), player.body:getY(), nil, nil, nil, sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2)
for i,p in ipairs(platforms) do
love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height)
end
end
function love.keypressed(key, scancode, isrepeat)
if key=="w" or key=="up" and player.grounded==true then
player.body:applyLinearImpulse(0, -2500)--for making player jump
end
end
function spawnPlatform(x, y , width, height)
local platform = {}
platform.body= love.physics.newBody(myWorld, x, y, "static")
platform.shape= love.physics.newRectangleShape(width/2, height/2, width, height)-- width/2, height/2 moves the offset to upper left hand corner of this shape thereby matching with platform
platform.fixture= love.physics.newFixture(platform.body, platform.shape)
platform.width=width
platform.height=height
table.insert(platforms, platform)
end
function beginContact(a,b, coll)
player.grounded=true
end
function endContact(a, b, coll)
player.grounded=false
end
Code: Select all
player={}
--creating player physics
player.body=love.physics.newBody(myWorld, 100, 100, "dynamic")
player.shape=love.physics.newRectangleShape(66, 92)
--A fixture acts as a connection between body & shape--
player.fixture=love.physics.newFixture(player.body, player.shape, density)
player.speed=100
player.grounded=false
function playerUpdate(dt)
if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
player.body:setX(player.body:getX() - player.speed * dt)
end
if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
player.body:setX(player.body:getX() + player.speed * dt)
end
end