Help with Bow&Arrow

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
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Help with Bow&Arrow

Post by ixjackinthebox »

Well I am trying to think how I would make the Arrow move

I don't really know how to go about making it but I guess I would need to use

Code: Select all

table.insert
table.remove
to add in the new arrows
and I would also need to get the direction between the player and the mouse

So how can I go about making this ANY info is appreciated

Also I am using love.physics for pretty much everything

My love file
Attachments
Game1.love
(107.08 KiB) Downloaded 167 times
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help with Bow&Arrow

Post by Roland_Yonaba »

ixjackinthebox wrote: I don't really know how to go about making it but I guess I would need to use

Code: Select all

table.insert
table.remove
This might help.
ixjackinthebox wrote: and I would also need to get the direction between the player and the mouse
My love file
Let mx, my be the actual mouse position
let x,y be the player position.
This will return the (clockwise) angle between the player and the mouse, in radians

Code: Select all

local angle = math.atan2(my - y, mx - x)
Seen in this very helpful angle visualizer, from BlackBulletIV.
Also featured on the wiki.
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Help with Bow&Arrow

Post by ixjackinthebox »

Can someone tell me why this is wrong

Code: Select all

function love.mousepressed( MX, MY, button )
	--Arrow
	if button == "l" then
		Table_pos = Table_pos + 1
		table.insert(Objects.Arrow.body, Table_pos, love.physics.newBody( World, ArrowX, ArrowY, "dynamic"))
		table.insert(Objects.Arrow.shape, Table_pos, love.physics.newRectangleShape( 10, 3 ))
		table.insert(Objects.Arrow.fixture, Table_pos, love.physics.newFixture( Objects.Arrow.body , Objects.Arrow.shape, 1 ))
		--ArrowDraw = 1
			--Objects.Arrow.body:applyForce( MX, MY )
	
	end
end
I don't really know to set up the tables so it will create a new arrow and be able to show and hide the arrows individually

Edit:Can someone please tell me how to set the tables up properly.
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Help with Bow&Arrow

Post by ixjackinthebox »

Anyone...?
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Help with Bow&Arrow

Post by Ubermann »

You really no need a variable to save the number of elements in the table. You can use table.getn to obtain it.

I even recommend using this method because a variable could be modified wrong in your code and would lead to errors very hard to detect.

Other than that, I can't see anything wrong with your code, assuming that every variable is working as expected.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with Bow&Arrow

Post by bartbes »

Ubermann wrote:You can use table.getn to obtain it.
getn is deprecated and should not be used, I think it's even been removed for 5.2. Use the # operator instead.
User avatar
buhb11
Citizen
Posts: 81
Joined: Wed Dec 26, 2012 8:59 pm

Re: Help with Bow&Arrow

Post by buhb11 »

This will not help you solving the problem you got but you may want to look into your code cuz i get only 7fps on my leptop :(
I found Love very enjoyable and nice!
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Help with Bow&Arrow

Post by Ubermann »

bartbes wrote:
Ubermann wrote:You can use table.getn to obtain it.
getn is deprecated and should not be used, I think it's even been removed for 5.2. Use the # operator instead.
Oh, didn't know, Thanks for the tip.
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Help with Bow&Arrow

Post by ixjackinthebox »

What I want to try and do is on mouse press create a table like this one.

Code: Select all

        Objects.Arrow = {}
	Objects.Arrow.body = love.physics.newBody( World, ArrowX, ArrowY, "dynamic")
	Objects.Arrow.shape = love.physics.newRectangleShape( 10, 3 )
	Objects.Arrow.fixture = love.physics.newFixture( Objects.Arrow.body , Objects.Arrow.shape, 1 )
	Objects.Arrow.fixture:setUserData("Arrow")
But I would also need to be able to draw the arrow only after it has been shot and then remove the arrow when it goes out the games area.
But the thing I need help with how the tables should be done
This will not help you solving the problem you got but you may want to look into your code cuz i get only 7fps on my leptop :(
That might be because it uses love.physics for all of the controls
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Help with Bow&Arrow

Post by Santos »

Maybe something like this?

A table to store the arrows.

Code: Select all

arrows = {}
Creating a local arrow table, and then inserting it into the table of arrows.

Code: Select all

function create_arrow(x, y)
	local arrow = {}
	arrow.body = love.physics.newBody( World, x, y, "dynamic")
	arrow.shape = love.physics.newRectangleShape( 10, 3 )
	arrow.fixture = love.physics.newFixture( arrow.body , arrow.shape, 1 )
	arrow.fixture:setUserData("Arrow")

	table.insert(arrows, arrow)
end
And looping over all of the arrows to check if they're offscreen, and if they are, removing them from the table. (Note that this for loop uses pairs, not ipairs.)

Code: Select all

function remove_offscreen_arrows()
	for key, arrow in pairs(arrows) do
		if is_offscreen(arrow) then
			arrows[key] = nil
		end
	end
end
And calling them.

Code: Select all

function love.update(dt)
	remove_offscreen_arrows()
end

function love.mousepressed(x, y)
	create_arrow(x, y)
end
I hope this helps! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests