1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

How to invert / change position of three objects?

Discussion in 'Programming' started by Bhreno Kevin, Mar 26, 2017.

Tags:
  1. Bhreno Kevin

    Bhreno Kevin Guest

    Explaining better:

    There is obj_piece1 at position (x, y + 64), obj_piece2 at position (x, y + 32) and obj_piece3 at position (x, y).
    Whenever I press "space", they should change positions with each other, so that everyone has the chance to stay in positions (x, y), (x, y + 32) and (x, y + 64).

    Example:
    In the image;
    Purple piece is obj_piece3;
    Orange piece is obj_piece2;
    and Pink piece is obj_piece1;

    Every time I press "space", the pieces change position, for example: obj_piece1 goes to the place of obj_piece2, obj_piece2 goes to the place of obj_piece3 and obj_piece3 goes to the place of obj_piece1, and so on. Help!
     

    Attached Files:

  2. Lite

    Lite Member

    Joined:
    Jul 8, 2016
    Posts:
    56
    In either a control object or each individual objects you can change it using

    In a step event for a control object:
    Code:
    if(keyboard_check_pressed(vk_space))
    {
         with(obj_piece1) { //set x and y to desired location }
         with(obj_piece2) { //set x and y to desired location }
         with(obj_piece3) { //set x and y to desired location }
    }
    You can do this in each objects Step event as well instead:
    Code:
    if(keyboard_check_pressed(vk_space))
    {
         x = //desired new x location;
         y = //desired new y location;
    }
     
  3. Bhreno Kevin

    Bhreno Kevin Guest

    I tried his method, however, instead of "changing positions," one entered "inside the other."
    Below is the image with the problem and the code I made.

    In step of obj_controll:

    Code:
    if instance_number(obj_pieces)=3
    {
    if(keyboard_check_pressed(vk_space))
    {
    with(obj_piece1){
    if instance_position(x,y-32,obj_piece2) {y-=32};
    if instance_position(x,y+64,obj_piece2) {y+=64};
    }
                                
    with(obj_piece2){
    if instance_position(x,y-32,obj_piece3) {y-=32};
    if instance_position(x,y+64,obj_piece3) {y+=64};
    }
    
    with(obj_piece3){
    if instance_position(x,y-32,obj_piece1) {y-=32};
    if instance_position(x,y+64,obj_piece1) {y+=64};
    }
    }
    }
    images:
     

    Attached Files:

  4. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    265
    I would create 3 variables: piece1pos, piece2pos, piece3pos

    piece1pos=whatever y the trio has fallen to
    piece2pos=piece1pos+sprite_height
    piece3pos=piece1pos+(sprite_height*2)

    then in your key press code:
    switch (y)
    {
    case piece1pos: y=piece2pos; break;
    case piece2pos: y=piece3pos; break;
    case piece3pos: y=piece1pos; break;
    }

    This is just a quick rough-out of the basic approach I'd take. Hopefully you can flesh it out yourself.
     
  5. Bhreno Kevin

    Bhreno Kevin Guest

    I think I did not explain very well ...

    Let us imagine 3 positions: pos1 (x,y+64), pos2 (x,y+32) and pos3 (x,y).
    When I press "space", the piece that is in pos1 (regardless if it is obj_piece1, obj_piece2 or obj_piece3) needs to go to pos2, the piece in pos2 goes to pos3, and the one in pos 3 changes to position pos1 (as if I were "choosing" the piece that would be underneath all ...)
     

    Attached Files:

  6. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    265
    You explained fine. That's exactly what my suggested code is designed to do. You might want to use a parent object for it, so it affects all 3 pieces without having to copy iy out 3 times.
     
    Bhreno Kevin likes this.
  7. Bhreno Kevin

    Bhreno Kevin Guest

    Oops, I did not realize it. Sorry!
    Now I'm going to test your code here, but first, I could ask myself a question (I'm a GM beginner): What should I put in place of "whatever and the trio has fallen to" in code?
     
  8. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    265
    Well, I guess I'd need to see what code you're using to make the pieces fall.
     
  9. Bhreno Kevin

    Bhreno Kevin Guest

    Practically this:

    From Create, it goes to Alarm [2], and it repeats itself until the piece reaches something solid.


    Alarm Event for alarm 2:
    if relative position (0,32) is collision free for Only solid objects
    jump relative to position (0,32)
    set Alarm 2 relative to 45
     
  10. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    265
    Alright, cool. SO, create a new object, we'll call it obj_piece_control. Make sure your other piece objects are parented to it, and then place it in the room.

    CREATE EVENT:
    drop=-64;
    piece1pos=drop;
    piece2pos=piece1pos+32;
    piece3pos=piece2pos+32;

    if object_index=obj_piece_control
    {
    instance_create(x,piece1pos,obj_piece1) //change x to wherever you want to create the piece
    instance_create(x,piece2pos,obj_piece2)
    instance_create(x,piece3pos,obj_piece3)
    }
    alarm[2]=45;

    STEP EVENT:
    piece1pos=drop
    piece2pos=piece1pos+sprite_height
    piece3pos=piece1pos+(sprite_height*2)

    if keyboard_check_pressed(vk_space)
    {
    switch (y)
    {
    case piece1pos: y=piece2pos; break;
    case piece2pos: y=piece3pos; break;
    case piece3pos: y=piece1pos; break;
    }
    }

    ALARM 2 EVENT:
    if place_free(x,y+32) {y+=32; drop+=32}
    alarm[2]=45;
     
  11. Bhreno Kevin

    Bhreno Kevin Guest

    Your code seems to work very well, however, it turns out that in my game there are 3 different types of pieces:
    The pieces that show which will be the next combination to descend;
    The pieces that descend;
    And the "playable" pieces;

    The ones that show the next match are created at the beginning of the game and randomly. Upon finding 3, they change to pieces that descend and begin to descend. Once they reach something solid, they then become "playable" pieces.

    Well, in case, your code should be put in the pieces that go down, right? It turns out that they are created from the pieces that show the next ones to come, and the "instance_create" in your code makes the game give error.
     
  12. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    265
    I was a bit hasty with my code and edited it (I thought I'd edited it before you'd seen it, but I guess not!). The error is fixed now, as well as another small error that was in the code. This code goes in a CONTROL object, which is a separate invisible object which automatically 'passes on' its code to its 'children' - Look up parenting in the manual. In theory the same object could control the drawing of the next combination as well.
     
  13. Bhreno Kevin

    Bhreno Kevin Guest

    Oh yes, I'll test now. Thank you very much for your help!
    One more thing: Is it possible to get the same result by replacing the 3 types of pieces with 3 variables? Example: var piece_next, var piece_down and var piece_playable?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice