How to avoid more than one selection of an object in a room?

S

Shahil

Guest
I am making a car puzzle game..

Info:
Users must select a car(by pressing left button of a mouse)..if the car that got select..when user press left or right car move left/right by 32 px..User can select only one car at a time..if the there is one car selected..if user try to select other car the event must exit..and if the user select the selected car the selected car become deselected..by selecting a car user can select other car..

How can i do this?

Note: i had made but it get selected more than one car..
 

Attachments

sp202

Member
Put this in a controller object:
Code:
if mouse_check_pressed(mb_left)
{
selected=instance_place(mouse_x,mouse_y,car)
}
 
S

Shahil

Guest
Put this in a controller object:
Code:
if mouse_check_pressed(mb_left)
{
selected=instance_place(mouse_x,mouse_y,car)
}
Instance are already present in the room.. If user clicked at the car it get selected.. but i want to avoid multiple selection at the same time
 
S

Shahil

Guest
Yes, that code prevents multiple selection.
It is not working.. still i can select multiple object at the same time..

It is not working.. still i can select multiple object at the same time..
 
S

Shahil

Guest
Yes, that code prevents multiple selection.
Information about object: object0

Sprite: sprite0
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
execute code:

sel = 0



Step Event:
execute code:


if(sel = 1) {

if(keyboard_check_pressed(vk_left)) {
if(!place_meeting(x-32,y,object0))
x -= 32
}
if(keyboard_check_pressed(vk_right)) {
if(!place_meeting(x+32,y,object0))
x += 32
}
}


Mouse Event for Left Pressed:
execute code:


if(sel = 1) {
sel = 0
}else {
sel = 1
}



Draw Event:
execute code:

draw_sprite(sprite0,0,x,y);


if(sel == 1) {
draw_set_color(c_black)
draw_rectangle(x,y,x+sprite_width,y+sprite_height,true);
}
 

sp202

Member
That's because your faulty code is still in there. Use this code alone:
Code:
if mouse_check_pressed(mb_left)
{
with(car){sel=false}
inst=instance_place(mouse_x,mouse_y,car)
inst.sel=true
}
 
Last edited:
S

Shahil

Guest
Thank you sp202 ..

Finally after long time of coding and logic i had solved it....



Information about object: obj_car

Sprite: spr_car
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
execute code:

sel = false

global.selected = 0


Step Event:
execute code:


if(sel == true && global.selected = 1) {
if(keyboard_check_pressed(vk_left)) {
if(!place_meeting(x-32,y,obj_car))
x -= 32
}
if(keyboard_check_pressed(vk_right)) {
if(!place_meeting(x+32,y,obj_car))
x += 32
}
}


Mouse Event for Left Pressed:
execute code:


if(sel = false && global.selected = 0) {
sel = true
global.selected = 1
}else if(sel = true && global.selected = 1) {
sel = false
global.selected = 0
}


/* else {
sel = false
global.selected = 0
} */


Draw Event:
execute code:

draw_sprite(spr_car,0,x,y)



draw_set_color(c_black);

if(sel = true)
draw_rectangle(x,y,x+64,y+32,true)
 

sp202

Member
That method is a lot more involved and requires an extra variable, I think you're better off using that chunk I gave you because you can expand it easily if needed.
 
S

Shahil

Guest
That method is a lot more involved and requires an extra variable, I think you're better off using that chunk I gave you because you can expand it easily if needed.
Ok i will use this...
 
Top