Coin keeps spazzing out when collision code is added

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
l0ki77
Prole
Posts: 6
Joined: Tue Apr 18, 2023 7:55 pm

Coin keeps spazzing out when collision code is added

Post 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
Attachments
FInal Project.zip
(1.68 MiB) Downloaded 45 times
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Coin keeps spazzing out when collision code is added

Post 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()
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Coin keeps spazzing out when collision code is added

Post 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]
l0ki77
Prole
Posts: 6
Joined: Tue Apr 18, 2023 7:55 pm

Re: Coin keeps spazzing out when collision code is added

Post 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
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Coin keeps spazzing out when collision code is added

Post by knorke »

Ah yes. You need to do the same fix for the player's dimensions as well.
In function Player:new()
l0ki77
Prole
Posts: 6
Joined: Tue Apr 18, 2023 7:55 pm

Re: Coin keeps spazzing out when collision code is added

Post by l0ki77 »

Thanks for your help
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests