Shooting a projectile from a player

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
lobes
Prole
Posts: 2
Joined: Fri Jun 29, 2012 1:47 am
Location: Dirty South

Shooting a projectile from a player

Post by lobes »

Hey all first off I'm new here and wanted to start off by saying that...hi I'm new. Never programmed a game before and Love2D seemed like one of my best options to get my feet wet. I do have programming experience (Basic Java course and C# in school) and am a Web Developer/Designer (so coding somewhat comes easy to me)

With that being said I'm new to Lua (which I'm liking a lot) and obviously Love. Lets jump into it:

I want to start basic and just make that typical flying game where you fly around and shoot shit at people. I followed a few tutorials and was able to come up with a start screen, player with controls, and a few other things. Now I need help creating the projectile and having the player shoot it when a key is pressed.

I remember from using Unity that to shoot a projectile we had to make it appear using the players height and width. So I created a new file for my projectile where I'm trying to set the basic properties. I know I would need to check if the "space key" is pressed for when to shoot the projectile - but how would I exactly finish this up? I'm kind of lost on how to determine EXACTLY where to shoot the project from based on my player, how to call it and implement it (IE: I have the projectile created, but how do I USE it) - Obviously it will have to be create WHEN the space key is pressed and executed (right?) So my function to draw the projectile will need to execute IF the space key is pressed?

If any of that made sense, I need some help! I included my LOVE files if anyone can help!
Attachments
LoveGame.zip
All the LOVE files and stuff if anyone can look at mah codez
(74.87 KiB) Downloaded 259 times
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Shooting a projectile from a player

Post by rokit boy »

to shoot to mouse you can do something like this:

Code: Select all

function love.update(dt)
    dir = math.atan2(YOFbullet-YOFMOUSE,XOFbullet-XOFMOUSE)
    speed = 5
    ax = speed * dt * math.cos(dir)
    by = speed * dt * math.sin(dir)
    x = x + ax
    y = y + ay
end
if I remember correctly
u wot m8
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Shooting a projectile from a player

Post by Santos »

Welcome! :)

Here is what I would do:
- Use projectile_shoot() to create the projectile, and call it from love.keypressed
- Create projectile_update(dt) to move the projectile, and call it from love.update

love.keyboard.isDown is a function which will return true if a key is held down, which is useful for cases such as in your player.lua file. When player_move gets called, it asks "Is this key being held down? If so, move in that direction", which is what you want.

However, for the projectile, you will probably want to use love.keypressed, which is similar in nature to the love.mousepressed callback function you have already used. When a key is pressed, this function will run.

You can use love.keypressed like this:

Code: Select all

function love.keypressed(key)
	if key == " " then
		projectile_shoot()
	end
end
Now for the question of where to create the projectile. player.pic:getHeight() will return the height of the image player.pic, which will always be 150 in this case. To find where the player is, you need to keep track of it, which you are doing with player.x and player.y.

To put the middle of the projectile at the position of the middle of the player, add half the width and height of the player to the player's x and y positions (which will put the top left corner of the projectile in the middle of the player), then subtract half the projectile's width and height. If this is hard to visualize, try drawing it out on paper.

Code: Select all

projectile.x = player.x + (player.pic:getWidth() / 2) - (projectile.pic:getWidth() / 2) 
projectile.y = player.y + (player.pic:getWidth() / 2) - (projectile.pic:getHeight() / 2)
For everything else, check out part 1 and part 2 of Headchant's tutorial.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests