Programming help [Solved]

Brenden

Member
So I am making a Roulette game just for fun but i'm have an issue with one of my codes.
I have gone though it and debugged it and I found the main issue is this code which is on my obj_holding global left mouse button pressed.
Code:
if (obj_chip.mouse_on[1]){
    h=1
}
if (obj_chip.mouse_on[2]){
    h=2
}
if (obj_chip.mouse_on[3]){
    h=3
}
if (obj_chip.mouse_on[4]){
    h=4
}
if (obj_chip.mouse_on[5]){
    h=5
}
With this code I have a variable mouse_on[1-5] in my obj_chip and it is an array. Every other code goes though just fine but the variable h wont change when I make mouse_on true through this code on my obj_chip step event. ( var chip is a creation code for each chip which varys from 1 to 5)
Code:
if (mouse_x >= x-24 && mouse_x <= x+24 && mouse_y >= y-24 && mouse_y <= y+24){
    mouse_on[chip]=true;
}else{
    mouse_on[chip]=false;
}
I don't know if you are aloud to do what I am doing but if you have any smart advice it would be of great help!
If you need any other details just comment.
 
A

Aura

Guest
If there are multiple instances of obj_chip in the room, your code won't work because you are using obj_chip.mouse_on[n], so GM:S would refer to only one instance of obj_chip and not for all of them. A better way would be to use a controller object for storing the mouse_on[n] array. Or simply make it global.
 
A

Annoyed Grunt

Guest
As @Aura said, each one of your chips is holding an entire array, which is completely unneeded. You can also just store the chip's number in a global variable instead of using so many ifs. You can also use point_in_rectangle instead of that series of &&s.

Code:
if point_in_rectangle(mouse_x, mouse_y, x - 24, y - 24, y - 24, y + 24) {
    global.h = chip;
}
else {
    globa.h = chip;
}
 

Brenden

Member
I got it to work by changing this code by changing the var mouse_on to global because I did have more then one obj_chip.
Code:
if point_in_rectangle(mouse_x, mouse_y, x - 24, y - 24, x + 24, y + 24) {
    global.mouse_on[chip]=true;
}else{
    global.mouse_on[chip]=false;
}
I had to change the point_in_rectangle a bit but thanks anyways to @Annoyed Grunt and @Aura for your help!
 
Top