Yes, it is collision checking. Though, there's a little misunderstanding about variables that I'll first try to clarify before proceeding to a solution to your problem.
What does what
First of all, image_number
is a constant (YoYo Docs call it read-only variable) that gives you the number of sub-images of an object's current sprite (its sprite_index
). If you want information about the actual frame to be drawn in the current step, you need to check the instance variable image_index
.
That's the story: sprite_index
is the sprite assigned to an object to be drawn; image_index
is the current sub-image (or frame) of its animation to be drawn in the next Draw() call; finally, image_number
is the total number of sub-image of sprite_index
. When you change an object's sprite_index
its image_number
value will be automatically updated by the GM engine.
So, let's work with image_number
. This variable is usually set to -1, which means its value will be updated (so that the animation cycles through) depending on image_speed
value. I'm assuming your image_speed
s are already set to 0, so that you can set the value of image_index
es without having them changing automatically over time.
The first script
Now, to check mouse clicks onto objects depending on current image_index
es, just add to the Step Event of obj_meteorite
the following code:
// Check for an object being hit by the
// player: just a click, not continuously
if (mouse_check_button_pressed(mb_left))
{
// Is the mouse above me while clicking?
if (position_meeting(mouse_x,mouse_y,self))
{
// Was that the right object to hit?
if (image_index==global.roulette)
score += 10; // Yeah
else
score -= 10; // Nope
// You may want to destroy the object
instance_destroy();
}
}
The function collision_point()
belongs to the Advanced Collision Checking section in the YoYo Docs, as it's a more advanced way to check for instances whose collision masks include a particular point in the plane.
Optimization
This script is executed by every obj_meteorite
every time the left mouse button is pressed. That means, if there are currently 7 instances of obj_meteorite
then this script will be called and executed seven times. But we have one mouse cursor only, so we would just check what lies below it. We can achieve this by migrating such script into a new object called obj_controller
, which will execute a slightly different script in its Step Event:
// Check for an object being hit by the
// player: just a click, not continuously
var my_inst; // Local support variable
if (mouse_check_button_pressed(mb_left))
{
// Is there an instance below the mouse? Get its ID if yes
my_inst = instance_position(mouse_x,mouse_y,obj_meteorite);
// Is the mouse above the instance while clicking?
if (my_inst!=noone)
{
// Was that the right object to hit?
if (my_inst.image_index==global.roulette)
score += 10; // Yeah
else
score -= 10; // Nope
// You may want to destroy the object
with (my_inst) instance_destroy();
}
}
This way, the script is executed once only per mouse click. I guess your game isn't that complex and design optimization is no serious concern, but I suggest from time to time to check if your code can be designed and written differently to lower function calls and enhance your program performance, as it is good programming habit and practice.
(&& subimage == 1)
? Just put it in the code? What I want is, after the player clicks a meteorite, the program will check which sub-image that was clicked. Is it match with the roulette or not. \$\endgroup\$