If you didn't get what this is by reading the description, then you should take a look at the screenshot. It simply turns all objects over to the other side. In the example this is done only vertically, but by using the same code with a little tiny alternation this could work both vertically and horizontally. Especially the vertical thing can be used as a visual effect in games of all kinds, whether it is side-scrolling shooter or it is plattformers, or even in gameplay for some objects. The user then has to adapt himself to the new conditions, which is always intresting. Maybe you want to turn the gravity around also to make it even more funny!
Well, here goes the screenshot:
<img src="http://gmtutorials.com/files/mirrorscrn.PNG">
As you can see there are only two objects in my example, but this solution of mine works for an infinite number of objects. The only thing that limit is the speed, but as this is a once done done thing it should be okey.
First of all we need to make an index on which objects we want to affect, probably we'll not want to affect all in a real game. Therefore we make an index like this:
mirror_index[0] = obj1;
mirror_index[1] = obj2;
mirror_index[2] = 0;
Notice how I make the last one of them be assigned as zero, you should always end your index with a zero as this represents the end. Also notice that if you wanted to affect all you don't need this index, but you simply use "all" in the equation to come:
for(i=0; mirror_index[i] != 0; i+=1) {
with(mirror_index[i]) y = (room_height-sprite_height)-y;
}
My index I put in creation, as it was just a intializing thing. This I put in space release event, but it could of course be placed anywhere when you want to execute 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 is pretty much "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.
For each earlier stop it will change the position of some certain objects, this is the intresting part.
with(mirror_index[i]) y = (room_height-sprite_height)-y;
"with", that is a statement. It means that what is to follow will be executed from the specified object. As I defined mirror_index with the name of the objects, that is nothing strange or unusual, it's like saying with(obj1) or with(obj2), just like normal. And then all variables that is to follow will use the values of the objects from which they were executed.
The formula that y is assigned the value (room_height-sprite_height)-y is assuming that the origin of the sprites of all involved objects is set to (0,0) as this is default. Therefore I take room_height-sprite_height, but if you for an example have the origin set to the middle then you should have room_height-sprite_height/2.
Okey, the trick with the y position, which is really the point of this entire article I have yet not explained; and I should not. Because you try to figure it out youselves. I'm afraid that if I go into the maths, explaining it by words you wouldn't get to understand it anyway. It is very simple though, and has to do with how much space there is left on either side of the object and how that is relating to the position in the room; but you think about and you'll realize very soon.
If there is any questions on this, then please do comment and I'll try to answer.
<b>Example:</b> http://gmtutorials.com/files/mirrorlvl.gm6
.
Users logged in:
Comments
Loading comments...