1

I have this sprite: spr_meteoriteLv3, which has two sub-images with index image_index 0 and 1 respectively.

I have these objects: obj_meteoriteLv3, obj_tempMeteoriteLv3, and obj_score. Object obj_meteoriteLv3 spawns from above with random position, random amount, and random sub-image. Object obj_tempMeteoriteLv3 makes obj_meteoriteLv3s spawn. When player clicks on a meteorite, the program checks the value of image_index for that object.

obj_meteoriteLv3 has these events:

Create Event: change sprite_index into spr_meteoriteLv3, and start moving downwards.

Left-pressed Mouse Event: destroy the self instance, and check image_index: if image_index == 0 then score += 5; else score -= 5).

obj_tempMeteoriteLv3 has these events:

Create Event: set Alarm 0 to 3600, set variable exist to 1, and set variable add to 1.

Alarm 0: set variable add to 0, and destroy the obj_meteoriteLv3 instance.

Alarm 1: set variable exist to 1.

Step Event: if (exist == 1) then, if (add == 1) then create instance of obj_meteoriteLv3, set variable exist to 0, and set Alarm 1 to 10.

obj_score has these events:

Create Event: set score to 0.

Draw Event: draw the value of score.

The problem is, no matter which sub-image the meteorite image_index has when clicked, the score will always be incremented by 5 points. It's like the else condition isn't working. How can I fix this? Please explain your answer. Thanks.

I add some images for better understanding. Link 1. Link 2

Grace Michelle
  • 223
  • 2
  • 4
  • 20

2 Answers2

2

In obj_meteoriteLv3 it's being destroyed before it can execute the rest of the code blocks. Move "Destroy Instance" to the bottom.

In obj_tempMeteoriteLv3 both variables "add" and "exist" are not necessary, instead have-

Create Event-

alarm[0] = 3600

alarm[1] = 10

Alarm[0] Event-

destroy_instance

Alarm[1] Event

Create_instance of obj_meteoriteLv3

alarm[1] = 10

Community
  • 1
  • 1
William
  • 21
  • 2
  • First, I want to make sure I do it right. I move all the `image_index` checking conditions to `Destroy Event`. So, I just have `Destroy Instance` action in `Left-Pressed Mouse Event`. Is that what you mean? The program isn't working correctly. I haven't clicked the meteorite, but the score is incremented. Even if the cursor doesn't hover above the meteorite, the score is incremented. – Grace Michelle Jun 11 '16 at 09:39
  • It scoring when they're out-of-bounds, I misunderstood. Move everthing in destroy event back to left-click event and move "destroy instance" to the very bottom – William Jun 12 '16 at 10:39
  • I have tried that, and it has no difference. Also, when I used your alarm event, after 2 minutes, the meteorites won't stop spawning. So, I used var `add` and `exist` – Grace Michelle Jun 13 '16 at 00:25
0

Potentially, when you click, every single meteorite is triggered. On the left click event of the meteorite, you did not check if the cursor was on the sprite. You have to use the position_meeting function, to which you pass the mouse position and the instance to click. it would look like :

if (position_meeting(mouse_x, mouse_y, id))
{
    //your destroy code
}

Moreover, when the instance_destroy(); line is read, the following code is ignored and the program jumps to the destroy_event. William explained it well and suggested to change the order of the lines of code, but you can also change the score in the destroy event directly.

I would like to add that checking for clicks in every instance of the meteorites is not optimal for performances.

My recommendation would be to use a single object to check for clicks (your player or your meteorite spawner, for example), in which you would check for clicks, and if a meteorite is touched, you would trigger it's destroy event. And in this event, you would increment the score and check the sprite.

Your click event would look like :

with (instance_position(mouse_x, mouse_y, obj_meteoriteLv3))
{
    instance_destroy();
}

and in the meteorite destroy event, you would check the image_index and change the score accordingly.

EDIT : why the score doesn't change

I believe you didn't declare the score as a global variable, but as an instance variable. So when you write "score = ..." in an other object, it creates a new score variable, unrelated to the precedent.

You have 2 options :

1 - Declare the score a global variable in the create event of obj_score:

globalvar score;
score = //your starting score

Be aware that a global variable can't be set on it's initialisation line.

2 - Change the score through the score object :

whenever the score has to change :

obj_score.score = ...

  • I have tried to use the method in this [link](http://gamedev.stackexchange.com/questions/121723/game-maker-studio-check-collision-with-subimages). The method is almost same like what you said. But, it doesn't work. – Grace Michelle Jun 15 '16 at 10:31
  • Could you please explain in which way it doesn't work, describe what happens that shouldn't ? – An intern has no name Jun 15 '16 at 12:29
  • Both in my question above and in the link that I gave, I had explained my problem. In the link, you can look at my first comment. – Grace Michelle Jun 15 '16 at 13:15
  • Sorry, I didn't notice you were the one asking the question on the other thread. So you said the code from liggiorgio didn't work ? I read it and didn't notice any problem, excepted for the line where the score is changed. the only issue I can imagine is where he wrote "score = ...", you have to make sure that a global variable named score exists, otherwise it won't work. I'll write this in my answer for better readability. – An intern has no name Jun 15 '16 at 14:26