Questions of a noob

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.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

after a a crazy bunch of errors, i am stuck with this error. I did use the require field, and it seemed to work, sorta. At least now im getting a different error for a different line.
error i am getting now

main.lua:20: attempt to index global 'player' (a nil value)
traceback
main.lua:20: in function 'update'
[C]: in function 'xpcall'

here is my new code :
main.lua

Code: Select all

equire("map.lua")
require("player.lua")

function love.load()
    map = Map.load()
    player = Player.load()
end


   function love.keypressed(key)
	if state.keypressed then state.keypressed(key) end
   end

    function love.keyreleased(key)
	if state.keyreleased then state.keyreleased(key) end
    end

    function love.update(dt)
	map:update(dt)
	player:update(dt)
    end

  function love.draw()	
	map:draw()
	player:draw()
   end
map.lua

Code: Select all


Map = {}
Map.__index = Map

function Map.load()

local temp = {}
setmetatable(temp, Map)

temp.TileW, temp.TileH = 32, 32
temp.Tileset = love.graphics.newImage('tiles.png')
  
  temp.tilesetW, temp.tilesetH = temp.Tileset:getWidth(), temp.Tileset:getHeight()
  
  Quads = {
    love.graphics.newQuad(0, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 1 = brown
    love.graphics.newQuad(32, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 2 = grass
    love.graphics.newQuad(0, 32, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 3 = halfbridge
    love.graphics.newQuad(32, 32, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH) -- 4 = black
  }
  
    TileTable = {
  
     { 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
     { 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
     { 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
     { 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 } 
  }

  return temp
end

function Map.update(dt)
end

function Map.draw()
  for rowIndex=1, #TileTable do
    local row = TileTable[rowIndex]
    for columnIndex=1, #row do
      local number = row[columnIndex]
      local x = (columnIndex-1)*temp.TileW
      local y = (rowIndex-1)*temp.TileH
      love.graphics.drawq(temp.Tileset, Quads[number], x, y)
    end
  end
end

player.lua

Code: Select all

Player = {}
Player.__index = Player


function Player.load()
    --First we initialize all the state images that we need
    images = {
        up = love.graphics.newImage("tankup.png"),
		down = love.graphics.newImage("tankdown.png"),
        left = love.graphics.newImage("tankleft.png"),
		idle = love.graphics.newImage("tankidle.png"),
        right = love.graphics.newImage("tankright.png")
    }

    --And here's our player object, complete with a state variable and a variable that holds the current image.
    tank = {
        state = "idle",
        image = images["tankidle.png"],
        x = 500,
        y = 300,
        speed = 100,
		hp = 100
    }
end

function Player.update(dt)
    --Now what we do is we check for a button to be pressed, and if it is we set the player state and move the player.
    if love.keyboard.isDown("left") then
        tank.state = "left"
        tank.x = tank.x - tank.speed*dt
    elseif love.keyboard.isDown("right") then
        tank.state = "right"
        tank.x = tank.x + tank.speed*dt
    elseif love.keyboard.isDown("up") then
        tank.state = "up"
        tank.x = tank.x - tank.speed*dt
    elseif love.keyboard.isDown("down") then
        tank.state = "down"
        tank.x = tank.x + tank.speed*dt
	else
	if love.keyboard.isDown(" ") then
	tank.state ="idle"
    end
end
    --Now we update the player image with whatever image corresponds to the current state.
    tank.image = images[Player.state]
end

function Player.draw()
    --And finally we draw the player.
    love.graphics.draw(tank.image, tank.x, tank.y)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Questions of a noob

Post by Robin »

Could you bundle up your code in a .love? It is a bit hard to find out where the problem is exactly without a .love.

Also, please use consistent indentation. It makes reading source code a lot easier.
Help us help you: attach a .love.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

here is my .love

Note it does not work for some reason, it just shows the default love ball thing and a black screen. But you can always just unzip it and stuff i guess.
Attachments
test.love
(48.77 KiB) Downloaded 103 times
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

i am sorry robin. It seems i forgot you would like it done with winzip instead. so here it is.
Attachments
test.love
(54.46 KiB) Downloaded 117 times
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Questions of a noob

Post by BarnD »

woo, got it working.. about to upload
test.love
the tank game
(55.51 KiB) Downloaded 235 times
Why do my attachments always look bad..
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Questions of a noob

Post by BarnD »

DOUBLE POST ALERT
I fixed the few other errors, and got the tank movable :) (Probably, im getting 1 fps with it cause my computer is really crap) :/
test.love
(55.65 KiB) Downloaded 228 times
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

i was able to do this as well. now i just have to get my tank to be able to move up and down, not just left and right. It seems that all the right images are there and such, so this shall be the next task. I think maybe its something to do with the map maybe?
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Questions of a noob

Post by BarnD »

mrpoptart wrote:i was able to do this as well. now i just have to get my tank to be able to move up and down, not just left and right. It seems that all the right images are there and such, so this shall be the next task. I think maybe its something to do with the map maybe?
The one i posted worked every way, well anyways good luck fixing it all. :)
(its my bed time *looks at clock* 2am)
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

ok i solved the issue. Was even more of a noobie mistake then i can even believe. lmao. tank now moves in all directions, on the map, with all the correct pictures going with the correct directions. thank you so much for your help everyone.

I had the tank moving at the tank.x position on the map for every keyboard input. just changed down and up to tank.y and now it works great.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Questions of a noob

Post by Robin »

Good to see you worked it out between the two of you. :)
mrpoptart wrote:i am sorry robin. It seems i forgot you would like it done with winzip instead. so here it is.
It's not just me. Many people can't open RARs. What's worse, LÖVE can't run RAR archives, at all.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest