Creating a Tile Map of Classes? [Fixed]

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.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Creating a Tile Map of Classes? [Fixed]

Post by xXOdysseusXx »

Hello I'm relatively new at programming and I was attempting to create a tiled game. What I did was create a Wall Tile Class and then I used some of the code in the Tiling tutorials in attempt to initialize, draw, and update the class in accordance to a Tile map... I utterly failed. I dont' get an error, but the titles refuse to show up. Can anybody help out here?

Code: Select all

require("lib/SECS")
require("Circle")
require("WallTile")
gamestate = require ("lib/hump/gamestate")
level1 = gamestate.new() -- I added a gamestate libary that should help
-- Tile Stuff
 tile = {}
	tile[1] = WallTile:new()
   map_w = 20 	
   map_h = 15
   map_x = 0
   map_y = 0
   map_offset_x = 30
   map_offset_y = 30
   tile_w = 48
   tile_h = 48
--Tile Stuff
function love.load(dt)
    gamestate.registerEvents() -- Calling the GameStates
    gamestate.switch(level1) -- Ofcourse this will eventually be the into or menu
end
function love.draw()
love.graphics.setColor(255,255,255)
love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end

function level1:init()
entity = Entity:new()
entity:init(0,0,40,40)
map={ -- My friends, we have a tile map for level1
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
   { 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}

initTiles() -- Initiating the tiles

end
function level1:enter() 
-- Runs every time it enters the game state.
end
function level1:update(dt)
entity:update(dt)
end
function level1:draw()
entity:draw()
drawTiles()
end

function initTiles()
 for y=1, map_h do
      for x=1, map_w do
			if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
            tile[map[y+map_y][x+map_x]]:init((x*tile_w)+map_offset_x,(y*tile_h)+map_offset_y ) -- What the fuck just happened?
			end
      end
   end
end

function drawTiles()
 for y=1, map_h do
      for x=1, map_w do
			if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
            tile[map[y+map_y][x+map_x]]:draw()
			end
      end
   end
end


If anybody needs some information on what I did or what I was trying to do just ask.
Last edited by xXOdysseusXx on Sat Jan 14, 2012 11:28 pm, edited 1 time in total.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Creating a Tile Map of Classes?

Post by MarekkPie »

If you want something to appear on the screen, it has to come from the love.draw() callback function.

Code: Select all

function love.draw()
love.graphics.setColor(255,255,255)
love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Right now, this is all you have in there. Try putting level1:draw() inside there.

I should clarify...not the definition of the method(function), but the actual call to it.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Re: Creating a Tile Map of Classes?

Post by xXOdysseusXx »

I think I should mention that I'm using a Game State library and that it does work the way I have it. The reason I only have the version in the main love.draw() function is because I want the version to display in all game states. I only have one right now though :). Anyways no cigar there.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Creating a Tile Map of Classes?

Post by MarekkPie »

You still need to call that game states draw in love.draw(), at least according to the my understanding of the hump documentation:
http://vrld.github.com/hump/#Gamestate-draw

See the example.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Re: Creating a Tile Map of Classes?

Post by xXOdysseusXx »

If you read above the example it will tell you that it's necessary only if not using registerevents(). So I should be good.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Creating a Tile Map of Classes?

Post by T-Bone »

If the library claims to be able to draw stuff without you telling it to, it has to achieve this by creating a function called love.draw() itself. So when you do

Code: Select all

function love.draw()
    love.graphics.setColor(255,255,255)
    love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Then you probably just overwrite the drawing function from the library. Try replacing with this and see if it helps

Code: Select all

oldDraw = love.draw
function love.draw()
    oldDraw()
    love.graphics.setColor(255,255,255)
    love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Remember, the function love.draw() is the ONLY function that can draw something. Ever.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Creating a Tile Map of Classes?

Post by thelinx »

Or call the register function in love.load, when your love.draw has already been created.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Creating a Tile Map of Classes?

Post by vrld »

Here is your first problem:

Code: Select all

if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
map[y+map_y][x+map_x] can be 0 or 1. But you wrote

Code: Select all

tile = {}
tile[1] = WallTile:new()
in the beginning, so tile[0] = nil and tile[1] = <instance of WallTile>, but it is never 1.

Here's your second problem:

Code: Select all

tile[map[y+map_y][x+map_x]]:init((x*tile_w)+map_offset_x,(y*tile_h)+map_offset_y ) -- What the fuck just happened?
You reinitialize one tile over and over again with different positions. Either create more than one tile instances or put the position into tile:draw() and remove initTiles().
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Re: Creating a Tile Map of Classes?

Post by xXOdysseusXx »

Thanks vrld. For the first problem does that mean I should rewrite it as:

Code: Select all

if tile[map[y+map_y][x+map_x]] == WallTile then -- if it's a wall tile
?

Also for the second problem, I want to add physic's oriented collisions to the tiles so that means the just drawing them wouldn't suffice. Thus why I made a WallTile class, so how could I make multiple instances of the class in the positions in the map? I will also then need to call their draw and update function... or is there a totally better way to do this? The game would be oriented like Super Mario is.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating a Tile Map of Classes?

Post by Robin »

xXOdysseusXx wrote:

Code: Select all

if tile[map[y+map_y][x+map_x]] == WallTile then -- if it's a wall tile
?
I don't think that would work, since it was an instance of WallTile rather than WallTile itself (unless you changed that).

This should work, though:

Code: Select all

if map[y+map_y][x+map_x] == 1 then -- if it's a wall tile
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 13 guests