Page 1 of 1
Coin keeps spazzing out when collision code is added
Posted: Sat May 06, 2023 9:37 am
by l0ki77
Hello I've been trying to make a game for school and every time I add collision detection code to my project my object which is a coin keeps spazzing out without the player even touching out. The coin just goes all over the screen randomly with the player having any interactions with. I've tried removing the random spawn of the coin but the score keeps updating even without the player being near the coin
I have attached a file and I was wondering how I would use code tags, sorry I'm new to posting on forums
Re: Coin keeps spazzing out when collision code is added
Posted: Sat May 06, 2023 10:14 am
by knorke
Hi
Your coin is too big and so it collides every frame.
love.graphics.getWidth() / Height() get the size of the window.
I guess you will want to use
https://love2d.org/wiki/Texture:getWidth but for testing I just put fixed numbers:
Code: Select all
function Coin:new()
self.image = love.graphics.newImage('sprites/coin.png')
self.x = 300
self.y = 300
self.speed = 0
self.w = 50--love.graphics.getWidth()
self.h = 50--love.graphics.getHeight()
Re: Coin keeps spazzing out when collision code is added
Posted: Sat May 06, 2023 1:47 pm
by pgimeno
l0ki77 wrote: ↑Sat May 06, 2023 9:37 am
I have attached a file and I was wondering how I would use code tags, sorry I'm new to posting on forums
To use code tags, you write:
[
code]
the code here
[
/code]
Re: Coin keeps spazzing out when collision code is added
Posted: Sat May 06, 2023 7:42 pm
by l0ki77
knorke wrote: ↑Sat May 06, 2023 10:14 am
Hi
Your coin is too big and so it collides every frame.
love.graphics.getWidth() / Height() get the size of the window.
I guess you will want to use
https://love2d.org/wiki/Texture:getWidth but for testing I just put fixed numbers:
Code: Select all
function Coin:new()
self.image = love.graphics.newImage('sprites/coin.png')
self.x = 300
self.y = 300
self.speed = 0
self.w = 50--love.graphics.getWidth()
self.h = 50--love.graphics.getHeight()
thanks i used
Code: Select all
self.image:getWidth() and self.image:getHeight()
and got it to work but the score will increase in random increments once Collison is detected a couple of times
Re: Coin keeps spazzing out when collision code is added
Posted: Sat May 06, 2023 10:11 pm
by knorke
Ah yes. You need to do the same fix for the player's dimensions as well.
In function Player:new()
Re: Coin keeps spazzing out when collision code is added
Posted: Sun May 07, 2023 12:07 am
by l0ki77
Thanks for your help