Jump to content


Photo

Complication


  • Please log in to reply
4 replies to this topic

#1 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 29 May 2012 - 02:59 AM

hi, i am trying to change obj A to object B and back to object A with all their events true.

However, I have encounter an error, where I am able to change from obj A to B , but not to object A but still is able to perform object A events. Meaning, visually it did not change back to object A but its events still run.


object A code, left pressed event

{
if instance_exists(rightdirection_obj)
if distance_to_object(rightdirection_obj)<100
{
rightbelt=instance_nearest(x,y,rightdirection_obj);
with(rightbelt)instance_change(leftdirection_obj,true);
}
instance_change(change_to_right_obj,true);
}


object B code , left pressed event


{
if instance_exists(leftdirection_obj)
if distance_to_object(leftdirection_obj)<50
{
experiment=instance_nearest(x,y,leftdirection_obj);
with(experiment)instance_change(rightdirection_obj,true);
with(self)instance_change(left_press_obj,true);
}
}



where did I do wrong ?
  • 0

#2 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 29 May 2012 - 06:17 AM

It is the well-known problem on changing an instance in respond to mouse/keyboard event. When the mouse button is pressed, Game Maker first executes the event for all instances of A existing in the room, then for all instances of B. What happens is:
1. Game Maker executes button events for existing A. So, A turns into B in its event.
2. Game Maker executes button events for existing B. So, B turns back into A.

You rarely need to switch between two objects. In most of cases, it is enough to have one object and change its property appropriately. For instance, if their difference is only the sprite, you can just have one object and switch its sprite. Suppose that you have two sprites, spr_left and spr_right:
// Button press event
if (sprite_index == spr_left) {
    // code for left belt
    sprite_index = spr_right;
}
else { // if it is spr_right
    // code for right belt
    sprite_index = spr_left;
}
(It is important to use else statement so that only one of blocks is executed at a time.)

Edited by torigara, 29 May 2012 - 06:17 AM.

  • 0

#3 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 29 May 2012 - 07:59 AM

It is the well-known problem on changing an instance in respond to mouse/keyboard event. When the mouse button is pressed, Game Maker first executes the event for all instances of A existing in the room, then for all instances of B. What happens is:
1. Game Maker executes button events for existing A. So, A turns into B in its event.
2. Game Maker executes button events for existing B. So, B turns back into A.

You rarely need to switch between two objects. In most of cases, it is enough to have one object and change its property appropriately. For instance, if their difference is only the sprite, you can just have one object and switch its sprite. Suppose that you have two sprites, spr_left and spr_right:

// Button press event
if (sprite_index == spr_left) {
    // code for left belt
    sprite_index = spr_right;
}
else { // if it is spr_right
    // code for right belt
    sprite_index = spr_left;
}
(It is important to use else statement so that only one of blocks is executed at a time.)


Tried and is not working. So I improvise and use this code instead but the image still stuck at only one. why?

[code=auto:0] :


{
if instance_exists(rightdirection_obj)
if distance_to_object(rightdirection_obj)<100
{
rightbelt=instance_nearest(x,y,rightdirection_obj);
with(rightbelt)instance_change(leftdirection_obj,true);
}
sprite_index=right_button_spr;
rightbutton_event();
}


the rightbutton_event is done in script.

Edited by darkkyoun, 29 May 2012 - 08:00 AM.

  • 0

#4 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 29 May 2012 - 09:35 AM

the rightbutton_event is done in script.

And what it does...? Anyway I don't think it should be there. Instead, you need another pair of if-else statements to determine which part to execute depending on what sprite you are, as I've shown in the example. Assuming it is in the left button event:
if (sprite_index == left_button_spr)
{
    if (instance_exists(rightdirection_obj))
    {
        rightbelt = instance_nearest(x, y, rightdirection_obj); // You need to take the nearest instance first...
        if (distance_to_object(rightbelt) < 100) // then check its distance
        {
            with (rightbelt) instance_change(leftdirection_obj, true);
        }
    }
    sprite_index = right_button_spr;
}
else { // if sprite_index is right_button_spr
    // Put your code for right
    sprite_index = left_button_spr;
}

  • 0

#5 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 29 May 2012 - 11:22 AM


the rightbutton_event is done in script.

And what it does...? Anyway I don't think it should be there. Instead, you need another pair of if-else statements to determine which part to execute depending on what sprite you are, as I've shown in the example. Assuming it is in the left button event:
if (sprite_index == left_button_spr)
{
    if (instance_exists(rightdirection_obj))
    {
        rightbelt = instance_nearest(x, y, rightdirection_obj); // You need to take the nearest instance first...
        if (distance_to_object(rightbelt) < 100) // then check its distance
        {
            with (rightbelt) instance_change(leftdirection_obj, true);
        }
    }
    sprite_index = right_button_spr;
}
else { // if sprite_index is right_button_spr
    // Put your code for right
    sprite_index = left_button_spr;
}



Yes Thanks +D it worked , however I did a little modification to your code. Dun misunderstand , I kind of dun really understand certain line of code. That is why I modify it.
{
    if sprite_index=right_button_spr  // if this object is 'using' this sprite
        {
            if instance_exists(leftdirection_obj)
                if distance_to_object(leftdirection_obj)<50
            {
                experiment=instance_nearest(x,y,leftdirection_obj);
                with(experiment)instance_change(rightdirection_obj,true);
            }
            sprite_index=left_button_spr; //turn this object into left_button_spr after the above event is successfully executed 
        }
    else
        {
            leftbutton_event(); //this is the actions needed for left_button_spr
            sprite_index=right_button_spr;
        }
}

Edited by darkkyoun, 29 May 2012 - 11:23 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users