Hello, this is my first post from snooping around the forums and decided I try it out. I'm still a little new to Love2d, but this is more of a coding problem than a Loved2d problem. So I'm making a little game where enemies are generated and the player goes around and shoots them. I'm having problems figuring out so that when a enemy intersects the player, the enemy stops moving yet keeping it's original x and y. I've tried using this code for the left of the player intersecting with the right of the enemy:
for i,v in ipairs(enemy) do
local startX = v.x
local startXvel = v.xvel
if player.x + player.width > v.x then
v.x = player.x - v.width
v.xvel = player.xvel
end
v.x = startX
v.xvel = startXvel
end
Yet the enemy always moves through the player. If I take a the last two lines in the for loop, the enemies x equals the players. Thanks for the help.
The enemy keeps moving through the player because you just the set enemy.xvel to whatever the player's is. I assume next you change the player's velocity. Try changing that to v.xvel = 0. Not to mention, right after that, you change it back to its original velocity (v.xvel = startXvel). You need to remove the last two lines and replace the sixth with v.xvel = 0.
Without more code that's all I can do.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
The enemy keeps moving through the player because you just the set enemy.xvel to whatever the player's is. I assume next you change the player's velocity. Try changing that to v.xvel = 0. Not to mention, right after that, you change it back to its original velocity (v.xvel = startXvel). You need to remove the last two lines and replace the sixth with v.xvel = 0.
Without more code that's all I can do.
Thanks for the advice but again I still incur some problems. Here's all of the code and the code that I previously posted was inside player.physics https://www.mediafire.com/?i3f8uxamamdg6d8. Thanks again for the help because this can become very frustrating.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude wrote:Hey, I fixed your code. Check out the changes I made.
Thanks davis, I grateful for the advice and the help. But is there any way to make so that the player cannot pass through the enemies or is that inevitable? I am happy to know that the LOVE2D forums is very kind and helpful.
Yeah, there is. Just look at the enemy.physics code, and use the same logic for the player. I wanted to leave something for you to do- you learn more from doing.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude wrote:Yeah, there is. Just look at the enemy.physics code, and use the same logic for the player. I wanted to leave something for you to do- you learn more from doing.