keypressed() problems

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

keypressed() problems

This function was working for me earlier. I'm not sure what the problem is now. It's not setting my triBodyF variable any longer.

Code: Select all

function keypressed(key)
triBodyF = 100
end

function update(dt)
triBody1:setVelocity(triBodyF,0)
world:update(dt)
end


Almost
Prole
Posts: 42
Joined: Fri Aug 21, 2009 8:04 pm
Contact:

Re: keypressed() problems

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)

LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

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)
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.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

Could you post the whole .love-file here? It will be much easier to help you find the problem if we had that.

LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Maximum velocity?

EDIT

Actually, I also have a question about setVelocity(). There seems to be some kind of upper limit on velocity. There seems to be no difference between 100 and 10000 when I use setVelocity.

EDIT 2

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?
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.
0,
0,
true )

-- Code for the walls
leftWallBody = love.physics.newBody(world,
windowHeight / 2,
0 )

leftWallShape = love.physics.newRectangleShape( leftWallBody,

rightWallBody = love.physics.newBody( world,
windowHeight / 2,
0 )

rightWallShape = love.physics.newRectangleShape(rightWallBody,

topWallBody = love.physics.newBody( world,
windowWidth / 2,
0 )

topWallShape = love.physics.newRectangleShape(topWallBody,
windowWidth,

bottomWallBody = love.physics.newBody(world,
windowWidth / 2,
0 )

bottomWallShape = love.physics.newRectangleShape( bottomWallBody,
windowWidth,

-- 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

Attachments
TriEvo.love

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Maximum velocity?

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?
Common mistake: putting the files directly into the .love solves the problem:
Not:

Code: Select all

TriEvo.love
TriEvo/
main.lua
<etc>
But:

Code: Select all

TriEvo.love
main.lua
<etc>
EDIT: I also had to replace "Bluetri01.png" by "BlueTri01.png" somewhere in your code to make it run.

And I solved it: the problem is that you're starting out with TriBodyF = 0... this causes the body to sleep, and it must be kept awake, using:

Code: Select all

triBody1:setSleep(false)
before you set the velocity -- there is a setAllowSleep function, but that doesn't work correctly.
Last edited by Robin on Sat Sep 05, 2009 2:44 pm, edited 1 time in total.

LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

I see, but then I get this error message

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

LagMasterSam wrote:I see, but then I get this error message

See my edits.

LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

Thanks for the help.

However, why did it work when I placed triBodyF=100 right before triBody1:setVelocity(triBodyF,0)?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

LagMasterSam wrote:However, why did it work when I placed triBodyF=100 right before triBody1:setVelocity(triBodyF,0)?
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.