jumping tutorial?

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
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

It is only 1 file so I will just post the code. I am almost certain it has to do with the size of the map and the view.

Code: Select all


-- Office Man Stan --



function love.load()

	love.graphics.setMode(320, 320)
	love.graphics.setCaption( "Office Man Stan" )

	stan_right = love.graphics.newImage( "P.PNG" )
	ground = love.graphics.newImage( "X.png" )
	stan_left = love.graphics.newImage( "P_left.png" )
	-- game variables
	
	player_X = 0
	player_Y = 0
	player_direction = "r"
	-- background color
	love.graphics.setBackgroundColor(104, 136, 248)

	-- map stuff
	
	-- map variables 
	map_w = 20
	map_h = 20
	map_x = 0
	map_y = 0
	map_offset_x = 30
	map_offset_y = 30
	map_display_w = 14
	map_display_h = 10
	tile_w = 32
	tile_h = 32
	
	map={ 
	 
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    
}
	
	love.graphics.setFont(12)
	
	--tiles
	tile = {}
	for i=0,1 do
		tile[i] = love.graphics.newImage( "tile_image"..i..".png" )
	end
end

function draw_map()

	for y=1, map_display_h do
		for x=1, map_display_w do
			love.graphics.draw(
				tile[map[y+map_y] [x+map_x]],
				(x*tile_w)+map_offset_x,
				(y*tile_h)+map_offset_y )
		end
	end
end

function love.update(dt)
	
if love.keyboard.isDown( "left" ) then
	player_X = player_X - 1
	player_direction = "l"
	map_y = map_y-1
	map_x = math.max(map_x-1, 0)
	end
	
if love.keyboard.isDown( "right" ) then
	player_X = player_X + 1
	player_direction = "r"
	map_x = math.min(map_x+1, map_w-map_display_w)
	end

	
if love.keyboard.isDown( "down" ) then
	player_Y = player_Y + 1
	player_direction = "d"
	if map_y > map_h-map_display_h then map_y = map_h-map_display_h; end
	end
	
if love.keyboard.isDown( "up" ) then
	player_Y = player_Y - 1 
	player_direction = "u"
	if map_y < 0 then map_y = 0; end
	end
end

function love.draw()

if player_direction == "r" then
	love.graphics.draw( stan_right, player_X, player_Y )
	end

if player_direction == "l" then
	love.graphics.draw( stan_left, player_X, player_Y )
	end

if player_direction == "u" then
	love.graphics.draw( stan_right, player_X, player_Y )
	end
	
if player_direction == "d" then
	love.graphics.draw( stan_right, player_X, player_Y )
	end
	
	--love.graphics.draw( ground, 0, 260 )
	
	draw_map()
	
end

Update: OK, the real problem is when ever the player touches the tiles it gives me an error:

Ok i think i will display the player position to see if the map size is wrong which it might.

Before:
[img]
http://img823.imageshack.us/img823/1276 ... before.png
[/img]

After:
[img]
http://img705.imageshack.us/img705/3250/officeafter.png
[/img]
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: jumping tutorial?

Post by Robin »

It was pretty hard to track down: the error gave us the hint to look at line 89, unfortunately because you didn't supply your original files, it is impossible to determine with certainty where line 89 was. Having said that, I'm pretty sure the error is in this line:

Code: Select all

			love.graphics.draw(
				tile[map[y+map_y] [x+map_x]],
				(x*tile_w)+map_offset_x,
				(y*tile_h)+map_offset_y )
You see, if you go to the left, so that map_x is -1, I'tll try to do this on the first iteration:

Code: Select all

love.graphics.draw(tile[map[1] [1+-1]], ...)
Which is equivalent to:

Code: Select all

love.graphics.draw(tile[map[1] [0]], ...)
Which is equivalent to:

Code: Select all

love.graphics.draw(nil, ...)
And you can't draw nil.

A solution would be to safeguard your drawing code so that I'll never try to do that. There are several ways of doing that, be creative! :)
Help us help you: attach a .love.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

So it turns out whenever i hit a tile it causes an error.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

I kinda understand what you are saying, but I am not 100% clear on what to do. And I did copy the code right from the file
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: jumping tutorial?

Post by kikito »

Mmm..

It is my fault. I didn't realize that the tutorial was about "map tiling with scrolling".
It teaches 2 things at the same time. Map loading & displaying (more or less ok) and scrolling (not very nicely).

It will be easier if you get rid of the scrolling part.

In order words: Remove all references to the following variables (declarations, and whenever they are used in a expression):

map_x, map_y, map_offset_x, map_offset_y.

That should simplify your code and allow you to continue. If you later need scrolling, there are better ways than the ones depicted in that code.
When I write def I mean function.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

Code: Select all

function draw_map()

	for y=1, map_display_h do
		for x=1, map_display_w do
			love.graphics.draw(
				tile[map[y+map_y] [x+map_x]],
				(x*tile_w)+map_offset_x,
				(y*tile_h)+map_offset_y )
		end
	end
end
what should i do with this snippet that draws the map?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: jumping tutorial?

Post by Robin »

Like kikito said:

Code: Select all

function draw_map()

	for y=1, map_display_h do
		for x=1, map_display_w do
			love.graphics.draw(tile[map[y] [x]], x*tile_w, y*tile_h)
		end
	end
end
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: jumping tutorial?

Post by kikito »

Literally remove all references to those variables. As if they never had existed.

So if you have

Code: Select all

foo + map_offset_x
,you should leave just this:

Code: Select all

foo
EDIT: Ninja'd by Robin. That ninja was carrying a fish.
When I write def I mean function.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

Alright, thanks guys I had this weird error, but i fixed it by defining map_x and map_y
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: jumping tutorial?

Post by toaster468 »

So collisions next?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest