Making a player.

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
User avatar
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Making a player.

Post by Love2dNewbie »

I'm making my very first game with Löve 2D and I'm having a few problems.
How do I change the x position of the player by 1 when he presses the letter d?
I tried:

Code: Select all


function love.update(dt)
  if love.keyboard.isDown("d") then
   player.x = player.x + 1
   end 
end

I'm a newbie to all this programming, so some help would be greatly appriciated. :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Making a player.

Post by Robin »

Hi! You could try:

Code: Select all

function love.keypressed(key)
    if key == 'd' then
         player.x = player.x + 1
    end
end
If that doesn't work for you, please upload a .love of your game so we can help you.
Help us help you: attach a .love.
User avatar
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Re: Making a player.

Post by Love2dNewbie »

Error: main.lua:23: attempt to perform arithmetic on field 'x' (a nil value)
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 1:42 am, edited 2 times in total.
User avatar
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Re: Making a player.

Post by Love2dNewbie »

how can i define it?
User avatar
Chroteus
Citizen
Posts: 89
Joined: Wed Mar 20, 2013 7:30 pm

Re: Making a player.

Post by Chroteus »

Post .love file or the code.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Making a player.

Post by MadByte »

You can create a table for all variables belong to the player.

Code: Select all


-- main.lua

function love.load()
	player = {} -- creates a table
	player.x = 10 -- adds the variable "x" to the table.
	player.y = 10 -- adds the variable "y" to the table.
	player.vel = 100 -- ...
end


function love.update(dt)
	if love.keyboard.isDown("left") then
		player.x = player.x - player.vel * dt
	end
end
EDIT: This is meant for continiously applied velocity to the player coord's.
Robin posted a option for adding a value each time a key is pressed - but not hold down.
Last edited by MadByte on Sat Dec 14, 2013 7:49 pm, edited 1 time in total.
User avatar
Love2dNewbie
Prole
Posts: 7
Joined: Fri Dec 13, 2013 5:11 pm

Re: Making a player.

Post by Love2dNewbie »

Still doesnt work.
My code:

Code: Select all

function love.load()
  player = {} 
  player.x = 10 
  player.y = 276
  player.vel = 1
  print('Game Loaded')
  print('v 0.1')
  love.graphics.setBackgroundColor(0, 255, 255) 
  player = love.graphics.newImage("textures/char.png")
end


function love.draw()
   love.graphics.setColor(103, 164, 21, 255)
   love.graphics.rectangle("fill", 0, 300, 800, 300) 
   love.graphics.setColor(240, 255, 255, 255)
   love.graphics.draw(player, 10, 276, 0, 1)
end

function love.update(dt)
   if love.keyboard.isDown("left") then
      player.x = player.x - player.vel * dt
   end
end


function love.keypressed(key, unicode)
end

function love.keyreleased(key, unicode)
  
end
User avatar
Chroteus
Citizen
Posts: 89
Joined: Wed Mar 20, 2013 7:30 pm

Re: Making a player.

Post by Chroteus »

Code: Select all

player = love.graphics.newImage("textures/char.png")
Here's your problem. First you define player as a table, and then as an image.

Define image with player.image, not player variable.
And in draw function, draw player.image, instead of player.

So, your game should look like this:

Code: Select all

    function love.load()
      player = {}
      player.x = 10
      player.y = 276
      player.vel = 1
      player.image = love.graphics.newImage("textures/char.png")
      print('Game Loaded')
      print('v 0.1')
      love.graphics.setBackgroundColor(0, 255, 255)
    end


    function love.draw()
       love.graphics.setColor(103, 164, 21, 255)
       love.graphics.rectangle("fill", 0, 300, 800, 300)
       love.graphics.setColor(240, 255, 255, 255)
       love.graphics.draw(player.image, 10, 276, 0, 1)
    end

    function love.update(dt)
       if love.keyboard.isDown("left") then
          player.x = player.x - player.vel * dt
       end
    end


    function love.keypressed(key, unicode)
    end

    function love.keyreleased(key, unicode)
     
    end
Last edited by Chroteus on Sat Dec 14, 2013 7:53 pm, edited 1 time in total.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Making a player.

Post by MadByte »

Code: Select all

function love.draw()
   love.graphics.setColor(103, 164, 21, 255)
   love.graphics.rectangle("fill", 0, 300, 800, 300) 
   love.graphics.setColor(240, 255, 255, 255)
   love.graphics.draw(player, 10, 276, 0, 1) -- Input variables here
end
Also you didn't associated the variables of the player to the image in your love.graphics.draw function. It should look like this:

Code: Select all

love.graphics.draw(player.img, player.x, player.y, 0, 1)
also put your player image inside the table for better readability. I usually add a "img" variable into the table.
Good luck
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests