Jump Isn't WorkingRESOLVED

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.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Jump Isn't WorkingRESOLVED

Post by WolfNinja2 »

It no longer crashes the game, (Thanks to this forum :D ) but now it just cant jump, I'm unsure how to fix this :p

It looks like it's TRYING to jump, gravity is just to heavy? Maybe turning off gravity until the jump is ended will fix this, I will play around with it while I wait for someone to post on here.

-Wolf

EDIT: Fixed Thanks Puzzlem00n!
Attachments
JUMP_SQUARE_JUMP.love
(1019 Bytes) Downloaded 102 times
Last edited by WolfNinja2 on Thu Nov 08, 2012 10:49 am, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Jump Isn't Working

Post by micha »

What you do right now is, when the player presses the jump button, the square is moved up by one block. From there it falls back down.

What you want instead, is having a squares that keeps moving up to a certain point and then fall down again. To get this you need to store the velocity of the player (not only its position) and each frame you do the following:

Code: Select all

player.x = player.x + player.vx *dt
player.y = player.y + player.vy*dt
That means the squares moves in the direction indicated by the vector (vx,vy), the velocity vector.

To get gravity then you need to update the player.vy by

Code: Select all

player.vy = player.vy + gravity*dt
(Assign some value to gravity first)

Another comment: You square moves completely gridbased. It should not do that, if you want to get a fluid platformer.
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Jump Isn't Working

Post by Puzzlem00n »

Clearly, you're basing this off of the Gridlocked Player tut, which is really more for turn based RPGs, if anything. Before you start with micha's advice, you may have to start from the beginning. (Although some platformers have gone gridbased, even they have fluid animation over the top.)

It appears your idea of gravity is this:

Code: Select all

if testMap(0, 1) then
     player.grid_y = player.grid_y + 32
end
The reason you can't fix that is because it's fundamentally wrong. All you're doing there is telling it to go right back down once it goes up: You see, testMap(0,1) is always true when you're in the air. I feel like there used to be a jumping tutorial on the tuts page, but it appears to be gone now, so just do what micha says. If you need any more ideas, check out the love file I uploaded. It's as basic an implementation as you can get that works well. Of course, it's uncommented, so if you'd like any help with it, feel free to ask!
Attachments
Fun with Platformers.love
THE TRIUMPHANT RETURN OF FUN WITH PLATFORMERS!
(948 Bytes) Downloaded 112 times
I LÖVE, therefore I am.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

So, how would I get collision to work then?

Post by WolfNinja2 »

Puzzlem00n wrote:Clearly, you're basing this off of the Gridlocked Player tut, which is really more for turn based RPGs, if anything. Before you start with micha's advice, you may have to start from the beginning. (Although some platformers have gone gridbased, even they have fluid animation over the top.)

It appears your idea of gravity is this:

Code: Select all

if testMap(0, 1) then
     player.grid_y = player.grid_y + 32
end
The reason you can't fix that is because it's fundamentally wrong. All you're doing there is telling it to go right back down once it goes up: You see, testMap(0,1) is always true when you're in the air. I feel like there used to be a jumping tutorial on the tuts page, but it appears to be gone now, so just do what micha says. If you need any more ideas, check out the love file I uploaded. It's as basic an implementation as you can get that works well. Of course, it's uncommented, so if you'd like any help with it, feel free to ask!
I'm looking at the game you submitted, i don't really understand the gridlocked player tutorial on map collision as is. Could you explain it a bit for me?

Thanks Either Way!

-Wolf
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Jump Isn't Working

Post by Puzzlem00n »

Here you go, man! I've directed you to a more basic form that has some flaws, but it helps to understand the core concepts. I hope the comments are adequate.

Code: Select all

function love.load()
	player = {x, y, ySpeed = 0, grav = 1350, h = 40, w = 40} --set up some variables

	map = { --we'll work how to use this map data later: X is player spawn, 0's are empty, 1's are solid, and 2's are lava
		{1, 1, 1, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1},
		{1, X, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
		{1, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 1, 1, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1}
		}
	
	--go through the map, find the X, and put the player there
	for y = 1, #map do
		for x = 1, #map[y] do
			if map[y][x] == X then
				player.x = x * player.w - player.w
				player.y = y * player.h - player.h
			end
		end
	end
	
	onGround = false --we set this up for later
end

function mapCollide(x, y)
	--Here's how we find out if we collided.
	--Any "array[][]" statement will return the value of
	--the two coordinates you supply in the table.
	--So, we floor (round down) the arguments supplied in
	--the function divided by the player's width/height,
	--plus one for positioning reasons.
	checkMap = map[math.floor(y/player.h) + 1][math.floor(x/player.w) + 1]
	if checkMap == 2 then
		--remember, 2 is lava, so if we hit it, reload game
		love.load()
	end
	return checkMap --send the number of the tile back to us
end

function love.update(dt)
   player.ySpeed = player.ySpeed + player.grav * dt --Gravity constantly adds to downward velocity, or ySpeed.
   player.y = player.y + player.ySpeed * dt --Add the velocity to the actual position.
   if love.keyboard.isDown("left") then
      player.x = player.x - 240 * dt --Move 'em left!
   end
   if love.keyboard.isDown("right") then
      player.x = player.x + 240 * dt --Move 'em right!
   end
   if mapCollide(player.x + 1, player.y + player.h) == 1 or mapCollide(player.x + player.w - 1, player.y + player.h) == 1 then
      --if we the bottom of our player hits the ground:
      player.y = player.y -player.ySpeed * dt --undo the moving of velocity
      player.ySpeed = 0 --displace the velocity and make it 0 again
      onGround = true --tell our game he's on the ground
   end
   if mapCollide(player.x + 1, player.y) == 1 or mapCollide(player.x + player.w - 1, player.y) == 1 then
      --if the top of our player hits the ceiling:
      player.ySpeed = 0 --displace the velocity to let him fall
   end
   if mapCollide(player.x, player.y + 1) == 1 or mapCollide(player.x, player.y + player.h - 1) == 1 then
      --if the left of our player hits, undo left movement
      player.x = player.x + 240 * dt
   end
   if mapCollide(player.x + player.w, player.y + 1) == 1 or mapCollide(player.x + player.w, player.y + player.h - 1) == 1 then
      --if the right of our player hits, undo right movement
      player.x = player.x - 240 * dt
   end
end

function love.keypressed(key)
	if key == "up" and onGround then
		--if we press jump on the ground:
		player.ySpeed = -600 --make our velocity negative
		--remember that negative vert velocity is going up, so jump!
	end
end

function love.draw()
		--loop through the array we made before
		for y = 1, #map do
			for x = 1, #map[y] do --go through every row in every column
				if map[y][x] == 1 then
					love.graphics.setColor(255, 255, 255)
					love.graphics.rectangle("fill", x * player.w - player.w, y * player.h - player.h, player.w, player.h)
					--so, let's take the row (x) and column (y), multiply that
					--by the width and height we know we'll need for x and y
				end
				if map[y][x] == 2 then
					love.graphics.setColor(255, 0, 0)
					love.graphics.rectangle("fill", x * player.w - player.w, y * player.h - player.h, player.w, player.h)
					--same
				end
			end
		end
		--draw the player
		love.graphics.setColor(0, 255, 255)
		love.graphics.rectangle("fill", player.x, player.y, player.w, player.h)
end
I LÖVE, therefore I am.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Jump Isn't Working

Post by micha »

In case you just want to read more about doing a platformer, I can recommand these two articles:
http://www.explodingrabbit.com/forum/en ... al-01.669/
and
http://higherorderfun.com/blog/2012/05/ ... atformers/
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Jump Isn't Working

Post by Puzzlem00n »

micha wrote:In case you just want to read more about doing a platformer, I can recommand these two articles:
http://www.explodingrabbit.com/forum/en ... al-01.669/
and
http://higherorderfun.com/blog/2012/05/ ... atformers/
I love that first link! It's perfect for beginners, can't believe I haven't found it before. Note for Wolf though: When he says to write "love.event.push("q")," you really want "love.event.quit()," otherwise it won't work.

The second link is a good article, but it's sort of advanced. It can all kind of go over your head if you aren't grounded in the basics.
I LÖVE, therefore I am.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Jump Isn't Working

Post by WolfNinja2 »

Thanks Puzzlem00n, I made my first level, get to the highest brown block to win. It's basic, but it's a start. THANKS PUZZLEM00N!

EDIT: ADDED LEGIT GRAPHICS :o
Was really simple :D
ReUploaded File!

-Wolf
Attachments
Thanks_Puzzlem00n_GRAPHICS_ADDED.love
(2.64 KiB) Downloaded 107 times
Thanks_Puzzlem00n.love
(2.64 KiB) Downloaded 94 times
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Jump Isn't Working

Post by Puzzlem00n »

Awesome! Next I'll have to help you to counteract some of those flaws I mentioned, though. Remember how the original upload I made was a bit more complicated, and I downgraded you to a less complicated version for "learning purposes?" Some of those flaws will become noticeable as you play it more. (I think the way it doesn't always seem to want to jump is one of them.)
I LÖVE, therefore I am.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Jump Isn't Working

Post by WolfNinja2 »

Puzzlem00n wrote:Awesome! Next I'll have to help you to counteract some of those flaws I mentioned, though. Remember how the original upload I made was a bit more complicated, and I downgraded you to a less complicated version for "learning purposes?" Some of those flaws will become noticeable as you play it more. (I think the way it doesn't always seem to want to jump is one of them.)
Yes, I noticed sometimes if you do that little wall glitch it has a freak out, trust me, it was worse till I toned down dt a bit.
And I would really enjoy that, I was happy with this, but I wasn't proud till I added the graphics :D
So, how might I change it up to stop those glitches? Once gameplay isn't flawed I can get on different levels and a menu :)
Post Reply

Who is online

Users browsing this forum: No registered users and 143 guests