I can't apply gravity on my character.

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
Fab1can
Prole
Posts: 7
Joined: Sat Nov 07, 2015 5:58 pm
Location: Italy

I can't apply gravity on my character.

Post by Fab1can »

I've write this code:

Code: Select all

function love.load(arg)
	player = { x = 200, y = 210, speed = 150, img = nil }
	player.img=love.graphics.newImage ("char.png")
	love.physics.setMeter (32)
	world = love.physics.newWorld(0 , 32*9.81)
	player.body = love.physics.newBody (world, player.x, player.y, dynamic)
	player.shape = love.physics.newRectangleShape(32, 64)
	player.fixture = love.physics.newFixture (player.body, player.shape, 5)
end

function love.update(dt)
	if love.keyboard.isDown ("a") then
		player.x=player.x-player.speed*dt
	end
	if love.keyboard.isDown ("d") then
		player.x=player.x+player.speed*dt
	end
end

function love.draw(dt)
	love.graphics.draw(player.img, player.x, player.y)
end
I can move right and left my character but he doesn't fall down. Why?
(Scuse me if i've done some grammar error, i can't speak english very well)
User avatar
vladgalay
Citizen
Posts: 60
Joined: Sun Jan 29, 2012 4:54 pm
Location: The Motherland

Re: I can't apply gravity on my character.

Post by vladgalay »

This and this articles should help you
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: I can't apply gravity on my character.

Post by Beelz »

From a glance it looks like you forgot to include this in the update code:

Code: Select all

world:update(dt)
------------

Also, load and draw don't require arguments.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Fab1can
Prole
Posts: 7
Joined: Sat Nov 07, 2015 5:58 pm
Location: Italy

Re: I can't apply gravity on my character.

Post by Fab1can »

I've added world:update (dt) in the update code but it continues to have no gravity.
Now the code is:

Code: Select all

function love.load()
	player = { x = 200, y = 210, speed = 150, img = nil }
	player.img=love.graphics.newImage ("char.png")
	love.physics.setMeter (32)
	world = love.physics.newWorld(0 , 32*9.81)
	player.body = love.physics.newBody (world, player.x, player.y, dynamic)
	player.shape = love.physics.newRectangleShape(32, 64)
	player.fixture = love.physics.newFixture (player.body, player.shape, 5)
end

function love.update(dt)
	world:update (dt)
	if love.keyboard.isDown ("a") then
		player.x=player.x-player.speed*dt
	end
	if love.keyboard.isDown ("d") then
		player.x=player.x+player.speed*dt
	end
end

function love.draw()
	love.graphics.draw(player.img, player.x, player.y)
end
User avatar
Alexar
Party member
Posts: 174
Joined: Thu Feb 05, 2015 1:57 am
Location: Chengdu,China

Re: I can't apply gravity on my character.

Post by Alexar »

Fab1can wrote:I've write this code:

Code: Select all

	player.body = love.physics.newBody (world, player.x, player.y, dynamic)
[/quote]
it's "dynamic" not a nil value
Fab1can
Prole
Posts: 7
Joined: Sat Nov 07, 2015 5:58 pm
Location: Italy

Re: I can't apply gravity on my character.

Post by Fab1can »

Alexar wrote:
Fab1can wrote:I've write this code:

Code: Select all

	player.body = love.physics.newBody (world, player.x, player.y, dynamic)
[/quote]
it's "dynamic" not a nil value[/quote]
What do you mean?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: I can't apply gravity on my character.

Post by zorg »

dynamic is a variable, probably not set so it's nil by default,
you wanted to write 'dynamic', a string.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 8 guests