onClick, create platform.

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
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

onClick, create platform.

Post by Raylin »

Why does this terminate LOVE and throw an exit code 134?

What this attempts to do is use the example and add the simple functionality of clicking somewhere on the screen and adding a platform.

Code: Select all

function fCon(a,b,coll)

end
function con(a,b,coll)
	player.onGround = true
end
function lCon(a,b,coll)

end
function result(a,b,coll)

end
function love.load()
	world = love.physics.newWorld(0,0,650,650)
	world:setCallbacks(fCon,con,lCon,result)
	world:setGravity(0,700)
	world:setMeter(64)
	bodies = {}
	shapes = {}
	bodies[0] = love.physics.newBody(world,650/2,625,0,0)
	shapes[0] = love.physics.newRectangleShape(bodies[0],0,0,650,50,0)
	bodies[1] = love.physics.newBody(world,650/2,650/2,15,0)
	shapes[1] = love.physics.newCircleShape(bodies[1],0,0,20)
	love.graphics.setBackgroundColor(104, 136, 248)
	love.graphics.setMode(650, 650, false, true, 0)
	player = {bodies[1],onGround=false}
end
function love.update(dt)
	world:update(dt)
	if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
		bodies[1]:applyForce(400, 0)
	elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
		bodies[1]:applyForce(-400, 0)
	end
	if love.keyboard.isDown("up") and player.onGround == true then
		bodies[1]:applyImpulse(0,-50,0,0)
		player.onGround = false
	end
	if love.mouse.isDown("l") then
		local x=#bodies+1
		bodies[x] = love.physics.newBody(world,love.mouse.getX(),love.mouse.getY(),0,0)
		shapes[x]= love.physics.newRectangleShape(bodies[x],0,0,3,1,0)
	end
end
function love.draw()
	local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox()
	local boxwidth = x3 - x2
	local boxheight = y2 - y1
	love.graphics.setColor(72, 160, 14)
	love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
	love.graphics.setColor(193, 47, 14)
	love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: onClick, create platform.

Post by tentus »

It's partly because you started your index at 0 for the bodies. Lua starts at 1.

It's mainly because the effective minimum size for a rectangle is determined by your meter. Try making a larger rectangle, like 6x6.

Also, the next step you'll want to loop through the bodies and draw them. As is, your physics rectangles will be invisible.
Last edited by tentus on Mon Jan 17, 2011 8:05 pm, edited 1 time in total.
Kurosuke needs beta testers
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: onClick, create platform.

Post by Taehl »

Well, for starters, your code would create a new platform every single frame that the mouse button is held down, which can't be good. But I don't know much about love.physics, so there may be a problem there, too.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: onClick, create platform.

Post by Raylin »

I've remedied this and I'm afraid that I'm screwing up somewhere.
Hmm.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: onClick, create platform.

Post by BlackBulletIV »

tentus wrote:It's partly because you started your index at 0 for the bodies. Lua starts at 1.
I can see the elements are initialised with keys (in this case 0 and 1), which would mean that there's no problem. If the keying is left to Lua, then the starting at 1 thing kicks in.

Unless of course you've already done this, I'd suggest moving the code to create a new platform into love.mousepressed and checking whether button == "l". This way you would only create platform on each mouse click.

Finally, aren't you meant to be drawing the bodies you create? In love.draw, you only draw the first 2 that you created. You'll probably need to loop through them, check what type a shape is using Shape:getType, and then react accordingly.
Post Reply

Who is online

Users browsing this forum: slime and 5 guests