Coding help

S

SendTheGravy

Guest
I'm SUPER new to game maker and just need a bit of help with some coding. Let's say I have two Objects, Object A and Object B. I want to make it so when I interact with Object A, Object B's sprite changes. How would I go about doing this? Help is much appreciated :)
 

Slyddar

Member
It depends on what you mean by interact. To change an instances sprite, you can set it's sprite_index to the name of the new sprite. So you would need to put this change in the event you are referring to when interacting.

For example you could create a mouse left pressed event in Object A, and add the code
Code:
objectb.sprite_index = new_sprite_name;
Adjusting the names to what your objects and sprites are of course.
This would change the sprite of ALL instances of object B to the new sprite when you click on ANY instance of object A.
 

TailBit

Member
How far distance?
Overlapping, then you can use the collision event .. or instance_position
otherwise you might want to use distance_to_object .. maybe even instance_nearest

if you use the collision event:
Code:
if(keyboard_check_pressed(vk_space){
    other.sprite_index = noone;
}
in a collision event you can refer to the other object by using other, you might want to use something else then noone as the sprite :p

if you use the step event, you can do the same:
Code:
// keyboard check first .. because it is less taxing then collision checks
if(keyboard_check_pressed(vk_space){
    
    // we then check if we collide with obj_B and store it's id in a temp variable so we can refer back to it
    var ins = instance_position(x,y, obj_B);

    if(ins != noone){ // instance_position would return noone if we are not colliding
        // now we can use ins to refer to sprite_index of obj_B
        ins.sprite_index = noone;
    }
}
alternative using with
Code:
// keyboard check first .. because it is less taxing then collision checks
if(keyboard_check_pressed(vk_space){

    // "with" is safe to use with a value like "noone" that instance_position can return
    with(instance_position(x,y, obj_B) ){

        // this code will be performed inside the instance of the object found by instance_position, or not happen at all
        sprite_index = noone;

    }
}

now if you want a certain distance:

Code:
// keyboard check first .. because it is less taxing then collision checks
if(keyboard_check_pressed(vk_space){
    
    // we then check if we collide with obj_B and store it's id in a temp variable so we can refer back to it
    var ins = instance_nearest(x,y, obj_B);

    if(ins != noone){
        
        // note that this takes a object and not the instance we found .. but we wanted the nearest anyway
        if(distance_to_object(obj_B) < 10){
            ins.sprite_index = noone;
        }

    }
}
 
Top