Page 1 of 1

Some coding help!

Posted: Thu Mar 24, 2011 9:07 pm
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 :)

Re: Some coding help!

Posted: Thu Mar 24, 2011 10:30 pm
by ishkabible

Code: Select all

enx = enx + mo
maybe this is what you want?

Re: Some coding help!

Posted: Fri Mar 25, 2011 7:57 am
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

Re: Some coding help!

Posted: Fri Mar 25, 2011 11:24 am
by schme16
post the .love and I'll have a quick check over it, see if I can help at all :P

Re: Some coding help!

Posted: Sat Mar 26, 2011 6:46 am
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

Re: Some coding help!

Posted: Sat Mar 26, 2011 1:59 pm
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

Re: Some coding help!

Posted: Sat Mar 26, 2011 3:33 pm
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

Re: Some coding help!

Posted: Sat Mar 26, 2011 5:22 pm
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! :)

Re: Some coding help!

Posted: Sat Mar 26, 2011 5:30 pm
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