Some coding help!

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
Alecal
Prole
Posts: 5
Joined: Mon Nov 15, 2010 9:51 pm

Some coding help!

Post by Alecal »

Hey guys!

I'm new to the forums and new to coding in LUA with löve ^^
I'm making a game and I need some help with the coding :megagrin:
The code I've made is
enx = enx + (x - enx) * (dt - 0.01)
eny = eny + (y - eny) * (dt - 0.01)
Where enx & eny is the enemys position and x & y is the players position.
This code is placed in function love.update(dt).
I've coded so that when the player's position is past a certain x value the variable mo becomes smaller and the background moves back, creating the illusion that you are walking forward (when really he is just standing still at the x position given)
This also work the other way around, when he is walking left the mo variable gets bigger.

Now, I want to make so that the enemy's position will be eny + mo but I dont know any good way to make this happen :death:
If I simply write enx + mo the enemy will "fly" further and further away from the player
I can post the .love program if it helps

Thanks
regards, alecal


ps. awesome forum :)
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Some coding help!

Post by ishkabible »

Code: Select all

enx = enx + mo
maybe this is what you want?
User avatar
Alecal
Prole
Posts: 5
Joined: Mon Nov 15, 2010 9:51 pm

Re: Some coding help!

Post by Alecal »

ishkabible wrote:

Code: Select all

enx = enx + mo
maybe this is what you want?
I actually went to write enx + mo
Yeah, no... It doesn't work :(
The thing is, since it does it in the update thing, when the variable grows bigger the enemy flies away faster and further the bigger the variable gets
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Some coding help!

Post by schme16 »

post the .love and I'll have a quick check over it, see if I can help at all :P
My Development Diary - http://shanegadsby.info
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Some coding help!

Post by ivan »

Hey Alecal.
I think you need to clarify what behavior you are trying to program for the enemies.
If you want the enemy to move towards the player (at a constant speed) then the code should look something like:
local dx = (x - enx)
local dy = (y - eny)
local dist = math.sqrt ( dx * dx + dy * dy )
-- dist is the distance between the player and the enemy

enx = enx + dx / dist * (dt * espeed)
eny = eny + dy / dist * (dt * espeed)
-- where espeed is the enemy speed

Secondly, if you want to move the background you don't need to change the actual enemy position (enx and eny).
Simply store an offset variable (such as mo) and transform the enemy position to screen coordinates ONLY for rendering purposes:
enxScreen = enx - mox
enyScreen = enx - moy
-- draw the enemy at enxScreen, enyScreen
User avatar
Alecal
Prole
Posts: 5
Joined: Mon Nov 15, 2010 9:51 pm

Re: Some coding help!

Post by Alecal »

ivan wrote:Hey Alecal.
I think you need to clarify what behavior you are trying to program for the enemies.
If you want the enemy to move towards the player (at a constant speed) then the code should look something like:
local dx = (x - enx)
local dy = (y - eny)
local dist = math.sqrt ( dx * dx + dy * dy )
-- dist is the distance between the player and the enemy

enx = enx + dx / dist * (dt * espeed)
eny = eny + dy / dist * (dt * espeed)
-- where espeed is the enemy speed

Secondly, if you want to move the background you don't need to change the actual enemy position (enx and eny).
Simply store an offset variable (such as mo) and transform the enemy position to screen coordinates ONLY for rendering purposes:
enxScreen = enx - mox
enyScreen = enx - moy
-- draw the enemy at enxScreen, enyScreen
Thank you! Now, I've already tried something similar to this.
Something strange happens after a while, the enemy just walks away
I will put the .love file in this post so you guys can have a look
I'm pretty far from done atm
Attachments
coal.love
(704.13 KiB) Downloaded 114 times
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Some coding help!

Post by schme16 »

I've made a lot of changes, I really only meant to do a quick fix, but the more I thought about it the more I felt you could benefit from using the camera lib,
and creating a table of enemies, this helps scale the project later on, I also tried to straighten out some of the code blocks, getting the spacking and tabbing a little neater. This can help later on when the project goes from a 100 or 200 lines to 1000's of lines of code.
Anyway, hope the code makes sense, just ask if you need the code explained better :P
Attachments
coal[schme16 rev].love
(706.64 KiB) Downloaded 110 times
My Development Diary - http://shanegadsby.info
User avatar
Alecal
Prole
Posts: 5
Joined: Mon Nov 15, 2010 9:51 pm

Re: Some coding help!

Post by Alecal »

schme16 wrote:I've made a lot of changes, I really only meant to do a quick fix, but the more I thought about it the more I felt you could benefit from using the camera lib,
and creating a table of enemies, this helps scale the project later on, I also tried to straighten out some of the code blocks, getting the spacking and tabbing a little neater. This can help later on when the project goes from a 100 or 200 lines to 1000's of lines of code.
Anyway, hope the code makes sense, just ask if you need the code explained better :P
Oh, thanks dude!
Thats really awesome, the other problem we had was how to create multiple zombies, looks like you fixed that!
I'll be sure too ask you next time I need help! :)
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Some coding help!

Post by schme16 »

Heh,no probs! Some of the stuff you guys had was really clever, like using -1 for facing backwards. Id never thought of that before :P
My Development Diary - http://shanegadsby.info
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests