1
\$\begingroup\$

I have an obj_roulette, which contains 4 subimages, with value 2-5 and image_number 0-3. The value result from roulette stored as var global.roulette.

Then, I make many obj_meteorite, which contains 4 subimages too, spawn from above with random x value and random image_number. Player can shoot them with left-mouse click.

This is what I want:

If image_number obj_roulette is 0, and player shoot obj_meteorite with image_number 0, score +10. If image_number obj_roulette is 0, and player shoot obj_meteorite with image_number 1, score -10.

I don't know how to check collision between mouse_x/mouse_y and object image_number, and how to match obj_roulette image_number and obj_meteorite image_number.

Is it using collision checking? If it yes, then maybe the example in this link can help: link 1

Please explain your answer. Thanks.

update

[Simple Diagram Image]

update2

Simple Diagram Image 2

\$\endgroup\$
  • \$\begingroup\$ Not sure exactly what your asking but if you want to check if a meteorite is clicked in the step event you can also add (&& subimage == 1). Or if you mean that you want to check if the mouse is hovering over the meteorite then you would need to have conditions checking if mouse_x > self.x && mouse_x < self.x + sprite_width and repeat for the y axis. \$\endgroup\$ – Daedric May 21 '16 at 6:41
  • \$\begingroup\$ @Daedric Where can I add (&& 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\$ – Grace Michelle May 21 '16 at 8:21
  • \$\begingroup\$ Then you need multiple if statements that do different things based on the value of the subimage \$\endgroup\$ – Daedric May 21 '16 at 10:17
  • \$\begingroup\$ @Daedric Can you give your code? Or if possible, can you give drag actions answer? Thanks \$\endgroup\$ – Grace Michelle May 21 '16 at 11:15
  • \$\begingroup\$ Can you add some diagrams because its really hard to solidify code when I don't know what your asking exactly. \$\endgroup\$ – Daedric May 22 '16 at 2:47
0
\$\begingroup\$

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_speeds are already set to 0, so that you can set the value of image_indexes without having them changing automatically over time.

The first script

Now, to check mouse clicks onto objects depending on current image_indexes, 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.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Sorry for late response. I try your optimized code, but the score never changes, no matter which meteorite I click. Note: I already put the obj_controller in the room. Then, I try your first code. The result is, the meteorite won't spawn. Is it because obj_tempMeteorite that I created to create instance of obj_meteorite? \$\endgroup\$ – Grace Michelle May 25 '16 at 4:19
  • \$\begingroup\$ Well, I don't know what obj_tempMeteorite is or does, you are mentioning it for the first time \$\endgroup\$ – liggiorgio May 25 '16 at 6:50
  • \$\begingroup\$ Well yeah, sorry, I thought it doesn't affect the problem. I updated my Simple Diagram Image, it contains obj_tempMeteorite \$\endgroup\$ – Grace Michelle May 25 '16 at 11:02
  • \$\begingroup\$ I can't see the point with your obj_tempMeteorite. Is it a controller? Does it deal with some sort of logic? It would be cleaner and clearer if the main logic was assigned to a single object (maybe the controller you mentioned) whilst coding other objects as far as their behaviors are concerned. An obj_Meteorite should be only responsible for its own movement, appearance, and maybe interaction between user and itself. \$\endgroup\$ – liggiorgio May 25 '16 at 14:29
  • \$\begingroup\$ I used obj_tempMeteorite to spawn obj_meteorite. I knew that from youtube tutorial, and it's modified in my program. It works fine, so I think I don't need to change the events in obj_tempMeteorite \$\endgroup\$ – Grace Michelle May 25 '16 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.