Code: Select all
function keypressed(key)
triBodyF = 100
end
function update(dt)
triBody1:setVelocity(triBodyF,0)
world:update(dt)
end
Code: Select all
function keypressed(key)
triBodyF = 100
end
function update(dt)
triBody1:setVelocity(triBodyF,0)
world:update(dt)
end
I did define triBodyF = 0 in the load() function. It doesn't matter whether the key has just been pressed or not. There should still be some effect, even if it's only temporary.Almost wrote:TribodyF is not defined until you press a key, yet the update function tries to set velocity based on it right away. If you want it to have a default value, put TribodyF = 0 or something in the load function.
Also, keypressed only happens when a key is first pressed. if you want something to happen WHILE a key is pressed, use
if love.keyboard.isDown(love.key_space) then
in the update function.
Oh, and typing "if variablename" will return false for undefined variables and true for defined variables (other than variables defined as false or nil)
Robin wrote:Could you post the whole .love-file here? It will be much easier to help you find the problem if we had that.
Code: Select all
function load()
windowWidth = 800 -- Must be multiple of 4
windowHeight = 600 -- Must be multiple of 4
worldPadding = 20 --[[ Must be multiple of 4
Used to give room for bounding boxes ]]
if love.graphics.checkMode(windowWidth, windowHeight, false ) then
love.graphics.setMode(windowWidth, windowHeight, false, true, 0)
end
-- Using negative values to match world and graphics coordinates while
-- leaving room in the world for bounding boxes.
world = love.physics.newWorld(-worldPadding,
-worldPadding,
windowWidth + worldPadding,
windowHeight + worldPadding,
0,
0,
true )
-- Code for the walls
leftWallBody = love.physics.newBody(world,
-worldPadding / 4,
windowHeight / 2,
0 )
leftWallShape = love.physics.newRectangleShape( leftWallBody,
worldPadding / 2,
windowHeight + worldPadding )
rightWallBody = love.physics.newBody( world,
windowWidth + worldPadding / 4,
windowHeight / 2,
0 )
rightWallShape = love.physics.newRectangleShape(rightWallBody,
worldPadding / 2,
windowHeight + worldPadding )
topWallBody = love.physics.newBody( world,
windowWidth / 2,
windowHeight + worldPadding / 4,
0 )
topWallShape = love.physics.newRectangleShape(topWallBody,
windowWidth,
worldPadding / 2)
bottomWallBody = love.physics.newBody(world,
windowWidth / 2,
-worldPadding / 4,
0 )
bottomWallShape = love.physics.newRectangleShape( bottomWallBody,
windowWidth,
worldPadding / 2)
-- Code for the triangle
triImage1 = love.graphics.newImage("Bluetri01.png")
triBody1 = love.physics.newBody(world,400,300)
triShape1 = love.physics.newPolygonShape(triBody1, -8,8, -8,-8, 8,0)
triBody1:setMassFromShapes()
triBodyF = 0
end
function keypressed(key)
triBodyF = 100 -- Doesn't work like this either.
------------------------------------------------
--[[
if key == love.key_left then
triBodyF = -100
elseif key == love.key_right then
triBodyF = 100
else
triBodyF = 0
end
--]]
end
function update(dt)
-- If I set triBodyF here, the velocity is updated as expected.
-- triBodyF = 100
triBody1:setVelocity(triBodyF,0)
world:update(dt)
end
function draw()
love.graphics.draw( triImage1,
triBody1:getX(),
triBody1:getY(),
triBody1:getAngle())
end
Common mistake: putting the files directly into the .love solves the problem:LagMasterSam wrote:I just realized you wanted the .love file. This leads me to yet another question. I already saw how to make a .love file. However, when I try to run the .love file, the game immediately exits. Is there a way to prevent that?
Code: Select all
TriEvo.love
TriEvo/
main.lua
<etc>
Code: Select all
TriEvo.love
main.lua
<etc>
Code: Select all
triBody1:setSleep(false)
See my edits.LagMasterSam wrote:I see, but then I get this error message
Could not load image "Bluetri01.png".
It's all about the sleeping: before the call to world:update(), the object wasn't asleep, but it's velocity was 0 -- that caused it to sleep, and once it's asleep (and if you don't call setSleep(false)), setting a velocity doesn't work, so unless you can press a key before update() is called (or set triBodyF to anything other than 0 in another way), it falls asleep.LagMasterSam wrote:However, why did it work when I placed triBodyF=100 right before triBody1:setVelocity(triBodyF,0)?
Users browsing this forum: Bing [Bot] and 3 guests