Mirroring A Room

This is a simple tutorial that turns all objects over to the other side of the screen. Mirroring a room can particularly be used as a visual effect in games of all kinds, whether it is a side-scrolling shooter or a platformer. It can even be used in gameplay for some objects. The user then has to adapt himself to the new conditions, which is always interesting. As you can see there are only two objects in my example, but this solution works for an infinite number of objects. The only thing that limits it is the speed, but as this is only done once it should be okay. First of all, we need to make an index of which objects we want to mirror, as we probably won’t want to affect all objects in a real game. Therefore we make an index like this:

mirror_index[0] = obj1; mirror_index[1] = obj2; mirror_index[2] = 0;

Note that the last one should be assigned as zero. You should always end your index with a zero as this represents the end. Also note that if you wanted to affect all of the objects you don’t need to create this index. Instead, simply use “all” in the following code:

for(i=0; mirror_index[i] != 0; i+=1) { with(mirror_index[i]) y = (room_height-sprite_height)-y;}

I placed this code in the creation event, as it was part of the initialization. The mirroring code I placed in the space release event, but it could of course be placed anywhere you like to create the effect. And what do I do? Well, first of all I use the “for” statement to loop through my index. ! is a binary operator that turn 1 into 0 and 0 into 1; therefore what my expression says “if mirror_index[i] is not equal to zero - then go on”, and I can do this because I have defined the end of the index with a zero. The next line of code will change the position of the objects in the index and this is the interesting part:

with(mirror_index[i]) y = (room_height-sprite_height)-y;

The “with” statement means that what is to follow will be executed for the specified object. Since as I defined mirror_index with the id of the objects, it’s like saying with(obj1) or with(obj2). And then all variables that follow will use the id of the associated object. The formula states that y is assigned the value (room_heightspriteheight)-y and it assumes that the origin of the sprites of all the involved objects is set to (0,0). Therefore I take room_height-sprite_height, but if you, for example, have the origin set to the middle then you should use room_height-sprite_ height/2.