• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED]Use Draw_sprite in one object and change object's Collision mask?

I'm new to GMS2, I make an object which have initial sprite , but I want this object to combine more sprites in some circumstance...
So I use "draw event" in this object and write things like:
draw_self;
draw_sprite(……);
draw_sprite(……);
...
but in this way, this object's collision mask will not change, just like the initial sprite.
I was wondering if there is a way to combine the collision mask too? Thank U!
 

mcube-12139

Member
Draw Event of the instance has nothing to do with its collision mask. My way to implement your goal is to create many instances, and program like this:
GML:
function combined_place_meeting(xx, yy, obj) {
    if (place_meeting(xx, yy, obj)) {
        return true;
    }
    
    with (child_1_object) {
        if (place_meeting(xx, yy, obj)) {
            return true;
        }
    }
    
    with (child_2_object) {
        if (place_meeting(xx, yy, obj)) {
            return true;
        }
    }
    ...
    
    return false;
}
 
Draw Event of the instance has nothing to do with its collision mask. My way to implement your goal is to create many instances, and program like this:
GML:
function combined_place_meeting(xx, yy, obj) {
    if (place_meeting(xx, yy, obj)) {
        return true;
    }
   
    with (child_1_object) {
        if (place_meeting(xx, yy, obj)) {
            return true;
        }
    }
   
    with (child_2_object) {
        if (place_meeting(xx, yy, obj)) {
            return true;
        }
    }
    ...
   
    return false;
}
thanks for help!

But I was still confused, My desire is to "drag object to move",so I want the things like: when I drag child_1 object or child_2 object or etc... they can move like one object. What should I do to use this function more specifically???
 

mcube-12139

Member
thanks for help!

But I was still confused, My desire is to "drag object to move",so I want the things like: when I drag child_1 object or child_2 object or etc... they can move like one object. What should I do to use this function more specifically???
Maybe you need a controller object and program for it like this:
GML:
// on create
children = [child_0_object, child_1_object, ...];
dragged = -1;

// on step
if (mouse_check_button_pressed(mb_left)) {
    for (var i = 0, sz = array_length(children); i != sz; i++) {
        var child = children[i];
        
        if (position_meeting(mouse_x, mouse_y, child)) {
            dragged = child;
            break;
        }
    }
}

if (!mouse_check_button(mb_left)) {
    dragged = -1;
}

if (dragged != -1) {
    var movementX = mouse_x - dragged.x;
    var movementY = mouse_y - dragged.y;
    for (var i = 0, sz = array_length(children); i != sz; i++) {
        with (children[i]) {
            x += movementX;
            y += movementY;
        }
    }
}
 
Maybe you need a controller object and program for it like this:
GML:
// on create
children = [child_0_object, child_1_object, ...];
dragged = -1;

// on step
if (mouse_check_button_pressed(mb_left)) {
    for (var i = 0, sz = array_length(children); i != sz; i++) {
        var child = children[i];
       
        if (position_meeting(mouse_x, mouse_y, child)) {
            dragged = child;
            break;
        }
    }
}

if (!mouse_check_button(mb_left)) {
    dragged = -1;
}

if (dragged != -1) {
    var movementX = mouse_x - dragged.x;
    var movementY = mouse_y - dragged.y;
    for (var i = 0, sz = array_length(children); i != sz; i++) {
        with (children[i]) {
            x += movementX;
            y += movementY;
        }
    }
}
Thank u so much!!!! Your codes inspired me a lot!
Due to the circumstance that I used "obj_mouse_picker" to manage "which instance I dragged ( inst.drag =true )", and make the specific instance to move in its obj event,
So I create a parent object to manage child objects like:
GML:
//create

child_A = instance_create_layer(...);

with (child_A )  { owner =other.id; }

  

child_B = instance_create_layer(...);

with (child_B){  owner =other.id;  }

    ....

}



//step  (dragged child follow the mouse, undragged child follow the dragged child)

if (child_A.drag){

    child_A.x = mouse.x;

    child_A.y = mouse.y;

    child_B.x = child_A.x +(whatever I want);

    child_B.y = child_B.y +(whatever I want);

if (child_B.drag){
     ...
}

}
maybe what I wrote is messy... but it's working! Thanks again.
 
Top