We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to have the panda (which the user moves with left and right arrow keys) collide with a candy image which is falling from the top left of the screen. When the 2 images collide, it would say "YOU TOUCHED THE CANDY" on the screen and the console. But when the panda and candy touch, nothing happens.
Here is the link to the code (I couldn't figure out how to indent the code on here): https://paste.ofcode.org/sBSTST866aqUv5xkzWJ84g
I tried doing if (pandaX==xPosCandy && pandaY==yPosCandy ) But it didn't work. I also tried doing if (pandaX>xPosCandy && pandaY<yPosCandy ) but it says YOU TOUCHED THE CANDY too soon.
Any help would be appreciated, and let me know if you need more code. Thanks!
Answers
http://JeffreyThompson.org/collision-detection/rect-rect.php
@GoToLoop Thanks, I changed some variables for height and width to make it easier, and I tried checking if they were overlapping like so:
But it still doesn't work. I'm not sure if my math is wrong calculating the top and bottoms of the images. Any suggestions?
@brianna0811 please edit your post with the gear icon and format your code correctly: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
This will enable others to read and test your code.
@jeremydouglass @GoToLoop
Here's the code
I also tried
But it didn't work
@brianna0811
I have to ask, is this all your code? If it is...
Please visit the reference in the Processing's website and check the following keywords: setup, size and draw.
It is also a good idea to visit the Examples section and explore few of them there. They are very simple and explain the basic mechanism of operation of Processing. In your code:
You need to call the size() function as your first line of setup.
You need to call draw() as this is the function that is continuously executed at 60 fps. This is where you add your code that changes over time.
Another note. When you format code in the forum, ensure there is an empty line above and below your code. This will ensure your code block gets formatted properly.
Kf
In order to encapsulate the collision detection algorithm + PImage + coordinates x & y, I've made a class called Sprite. Then created 2 subclasses extending it called Panda & Candy.
Just check it out online at this link: https://OpenProcessing.org/sketch/433511
And below the full code for it: :bz
@GoToLoop Thank you so much! I actually went back in my code and found that when I was calling the method touchCandy() it wasn't calling the parameters in the right order. It works now, thanks a lot for your help!