I tried using the approach in this thread but it doesn't seem to work. Here's the code I wrote for the function that checks whether 2 actors collide with each other. The function is designed so that it can accept the actor names dynamically. I removed the if statement safeguard against zero collisions as my background ensures that at least 1 actor always collide with the object.
- Code: Select all
int checkCollision(char actor_1[30], char actor_2[30]) {
int n; // Used to store the number of actors colliding with event actor
int i;
Actor *actors; // Declares an Actor* pointer (used to point to an array of actors event actor is colliding with)
actors=getAllActorsInCollision(actor_1, &n); // The &n passes the address of the variable n, so the function can change the value
// Actors now points to an array of n actors, where n is equal to the number of actors colliding with the actor
// if no actors are colliding, actors is set to null
for(i=0;i<n;i++) {
if(strcmp(actors[i].name, actor_2)==0) // Check whether actor_1 collides with actor_2
return 1;
}
return 0;
}
Here is how I call this function when trying to check whether two objects collide with each other:
- Code: Select all
if (checkCollision("Hover_Table_Right", "Key")) {
// Some code here
}
else {
// Additional code here
}
The code in the else statement always gets executed instead of that in the if statement, regardless of whether or not the two objects collide. Could someone please let me know whether I've missed something and have gone wrong somewhere?