GML - Teleportals
Requires Collision Correction to be completed (but not any questions or extension activities), as this activity continues on from the end of that example.
in addition to the objects used in Collision Correction:
Object | Sprite | Properties |
---|---|---|
objPortalA | sprPortalA | Visible |
objPortalB | sprPortalB | Visible |
//in addition to the code before this if position_meeting(x, y, objPortalA) { x = objPortalB.x + (horiz*2); y = objPortalB.y; } if position_meeting(x, y, objPortalB) { x = objPortalA.x + (horiz*2); y = objPortalA.y; }
// left and right movement inputs AND animations: if (keyboard_check(vk_right)){ horiz = move_speed; facing = 1; }; if (keyboard_check(vk_left)){ horiz = -move_speed; facing = -1; }; if (horiz != 0 and not jumped) { sprite_index = sprWalk; } if (keyboard_check(vk_nokey)){ horiz = horiz * frict; //friction if (abs(horiz) < 1 and abs(vert) < 1) { sprite_index = sprStill }; }; if position_meeting(x, y, objPortalA) { x = objPortalB.x + (horiz*2); y = objPortalB.y; } if position_meeting(x, y, objPortalB) { x = objPortalA.x + (horiz*2); y = objPortalA.y; } if place_meeting(x, y, objLadder) { if (keyboard_check(vk_up) or keyboard_check(vk_down)) { tethered = true; } } else { tethered = false; } // add constant force of gravity to our desired vert speed: if not tethered{ vert = vert + grav; } else { // tethered climbing: if (keyboard_check(vk_up)) vert = -climb_speed; if (keyboard_check(vk_down)) vert = +climb_speed; if (keyboard_check(vk_nokey)) vert = 0; } // only ALLOW jump IF the objBlock is 1 pixel BELOW the player, // or in other words, if the player is EXACTLY standing on floor: if (place_meeting(x, y+1, objBlock) and keyboard_check(vk_up)) { vert = jump_speed; jumped = true; } // jump animations: if (jumped) { // up down animations: if (vert > 0) { sprite_index = sprJumpUp; } else { sprite_index = sprLanding; } // reset jump IF on ground again: if (place_meeting(x, y+1, objBlock)) { jumped = false; } } // check for a HORIZONTAL collision: if (place_meeting(x+horiz, y, objBlock)) { // move AS CLOSE AS WE CAN to the HORIZONTAL collision object: // sign() can only return 1, -1, or 0. // sign(-7) == -1 (i.e. negative 1) // sign(7) == 1 (or +1 or positive 1) // sign(0) == 0 while (!place_meeting(x+sign(horiz), y, objBlock)) { x = x + sign(horiz); // move 1 pixel at a time towards object } horiz = 0; // no left right movement on HORIZONTAL collisions } // we have worked out our horizontal speed, now APPLY the change: x = x + horiz; // check for a VERTICAL collision: if (place_meeting(x, y+vert, objBlock)) { // move AS CLOSE AS WE CAN to the VERTICAL collision object: while (!place_meeting(x, y+sign(vert), objBlock)) { // remember, sign() can only return 1, -1, or 0: y = y + sign(vert); // // move 1 pixel at a time towards object } vert = 0; // no up down movement on VERTICAL collisions } // we have worked out our vertical speed, now APPLY the change: y = y + vert;
try and figure out how to do a vertical portal. also try one with an inversed vertical force?