Warping between maps
From Spheriki
How hard could it possibly be to warp somewhere in Sphere? Depends how hard you want it to be, really.
Contents |
The basics
The functions you need to know about are:
- SetPersonX(person, x)
- SetPersonY(person, y)
- SetPersonLayer(person, layer)
- ChangeMap(filename)
The rest presumes you're already in the map engine, moving your input guy around.
Simple example
So you could make your own warp function like so:
// start code
// Put this function into your main script file
function Warp(x, y, layer, map) {
var person = GetInputPerson();
if (map) ChangeMap(map);
if (layer >= 0) SetPersonLayer(person, layer);
SetPersonX(person, x);
SetPersonY(person, y);
}
// end code
And then you could put:
Warp(50, 50, 0, "whatever.rmp");
Into a trigger, and you'd go to (50, 50) on "whatever.rmp". (A trigger is where you right click on the map in the editor, choose Insert Entity > Trigger and type code in the box for when your guy walks over the tile.)
x and y are in pixels. You don't have to give it a layer or a map.
Simple example explanation
Let's explain the code:
function Warp(x, y, layer, map) {
} This means that we're making our own function, called 'Warp', and it has 4 parameters, called: x, y, layer and map. Inside the function, we have:
var person = GetInputPerson(); if (map) ChangeMap(map); if (layer >= 0) SetPersonLayer(person, layer); SetPersonX(person, x); SetPersonY(person, y);
1) var person = GetInputPerson();
This means, "make a variable called 'person' and set the value of GetInputPerson() to it."
2) if (map) ChangeMap(map);
This means "if we have a map parameter, we change the map."
3) if (layer >= 0) SetPersonLayer(person, layer)
This means, "if we have a layer parameter, and 'layer' is greater than or equal to 0, change the layer that 'person' is on to 'layer'."
4) SetPersonX(person, x)
This means, "set the x value of 'person' to 'x'."
5) SetPersonY(person, y)
Same as 4 but for y.
6) What about the lines that started with // ?
Those are just comments. They're ignored by Sphere, but helpful to people.
Two warps, one place
That seems rather simple, I thought you said it could be more complex? Sure it can, e.g.
// start code
function WarpObject(x, y, layer, map) {
if (this instanceof WarpObject == false) {
return new WarpObject(x, y, layer, map);
}
this.x = x;
this.y = y;
this.layer = layer;
this.map = map;
}
WarpObject.prototype.warp_to = function() {
var person = GetInputPerson();
if (this.map) ChangeMap(this.map);
if (this.layer >= 0) SetPersonLayer(person, this.layer);
SetPersonX(person, this.x);
SetPersonY(person, this.y);
}
// end code
And then you can do:
var town_door_1 = WarpObject(120, 64, 0, "town.rmp"); var house_1 = WarpObject(60, 60, 0, "house.rmp");
And in one trigger, you can write:
town_door_1.warp_to();
And in the other trigger you can write:
house_1.warp_to();
The only real advantage that this has, is it looks pretty and say you have two doors that go to the same place? You only have to write out the code once, just remember the name you gave the object :)
Screen transitions
"I want it to fade in and out!" I hear you cry. Sure:
EvaluateSystemScript("screen.js");
FadeOut(1000); // 1000 milliseconds = 1 second.
// Warp() or warp_object.warp_to() here
// draw what you want to fade into, e.g.
// RenderMap();
FadeIn(1000);
Note: Remember the drawing logic rule: Draw, FlipScreen, and somehow delay so we can see it. :) (Although in this case, FadeIn/FadeOut acts as our FlipScreen.)
So we get:
EvaluateSystemScript("screen.js");
function FancyWarp(x, y, layer, map) {
FadeOut(1000); // 1000 milliseconds = 1 second.
Warp(x, y, layer, map);
UpdateMapEngine();
RenderMap();
FadeIn(1000);
}
By Flikky (from the Sphere CHM, adapted and updated for the wiki.)

