Reputation: 223
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_meteoriteLv3
s 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
Upvotes: 1
Views: 1138
Reputation: 496
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 = ...
Upvotes: 0
Reputation: 21
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
Upvotes: 2