keypressed() problems

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
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

keypressed() problems

Post 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
User avatar
Almost
Prole
Posts: 42
Joined: Fri Aug 21, 2009 8:04 pm
Location: Canada
Contact:

Re: keypressed() problems

Post 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)
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

Post 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.
Help us help you: attach a .love.
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Maximum velocity?

Post 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
Attachments
TriEvo.love
(1.92 KiB) Downloaded 241 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Maximum velocity?

Post 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.
Last edited by Robin on Sat Sep 05, 2009 2:44 pm, edited 1 time in total.
Help us help you: attach a .love.
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

Post by LagMasterSam »

I see, but then I get this error message

Could not load image "Bluetri01.png".
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

Post by Robin »

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

Could not load image "Bluetri01.png".
See my edits.
Help us help you: attach a .love.
LagMasterSam
Prole
Posts: 8
Joined: Sun Aug 30, 2009 2:24 pm

Re: keypressed() problems

Post by LagMasterSam »

Thanks for the help. :)

However, why did it work when I placed triBodyF=100 right before triBody1:setVelocity(triBodyF,0)?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: keypressed() problems

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 167 guests