Page 1 of 1

keypressed() problems

Posted: Fri Sep 04, 2009 9:27 pm
by LagMasterSam
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

Re: keypressed() problems

Posted: Fri Sep 04, 2009 10:44 pm
by Almost
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)

Re: keypressed() problems

Posted: Sat Sep 05, 2009 3:36 am
by LagMasterSam
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.

Re: keypressed() problems

Posted: Sat Sep 05, 2009 10:19 am
by Robin
Could you post the whole .love-file here? It will be much easier to help you find the problem if we had that.

Maximum velocity?

Posted: Sat Sep 05, 2009 1:47 pm
by LagMasterSam
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.
  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

Re: Maximum velocity?

Posted: Sat Sep 05, 2009 2:25 pm
by Robin
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.

Re: keypressed() problems

Posted: Sat Sep 05, 2009 2:27 pm
by LagMasterSam
I see, but then I get this error message

Could not load image "Bluetri01.png".

Re: keypressed() problems

Posted: Sat Sep 05, 2009 2:45 pm
by Robin
LagMasterSam wrote:I see, but then I get this error message

Could not load image "Bluetri01.png".
See my edits.

Re: keypressed() problems

Posted: Sat Sep 05, 2009 2:55 pm
by LagMasterSam
Thanks for the help. :)

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

Re: keypressed() problems

Posted: Sat Sep 05, 2009 3:09 pm
by Robin
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.