Login | Register

Wrap view example

April 3, 2007, 23:37 by Calle
[loading]
-->
One of the first good Game Maker games every created was called Dog Fight, by Carl Gustaffson. I played it sometimes in 2003, I think. It uses something similar to what we are about to create. When the ship goes out on one side of the screen, it reappears on the other. Not the entire ship comes out before you see parts of it on the other side; and this is the complication. Because how can you draw only parts of the sprite on the other side of the screen, and exactly only those parts that are not already shown of the ship. My solution is not ultimate, but it works fine and it looks really nice. Such as would the ship (not a ship in my example) actually exist on both sides of the room. First of all though, here is the code:

Step Event:
execute code:

if keyboard_check(vk_left) x -=4;
if keyboard_check(vk_right) x +=4;
if keyboard_check(vk_up) y -=4;
if keyboard_check(vk_down) y +=4;

if (x+sprite_width<0) x +=room_width;
if (x+sprite_width>room_width) x -=room_width;
if (y+sprite_height<0) y +=room_height;
if (y+sprite_height>room_height) y -=room_height;

Draw Event:
execute code:

draw_sprite(sprite_index,image_single,x,y);
draw_sprite(sprite_index,image_single,x+room_width,y);
draw_sprite(sprite_index,image_single,x-room_width,y);
draw_sprite(sprite_index,image_single,x,y+room_height);
draw_sprite(sprite_index,image_single,x,y-room_height);


Half of the code in step event is just for moving my character, that is irrelevant. The second part we will return to. The third part is also the last part and that is where we will start. The trick that we uses to produce the right effect is that we draw the same sprite at several positions. One is of course at where character really are, but we draw four more. One in each direction. The sprite that we draw to the right for an example, has an x position of x+room_width. That's because when our character disappears out from the room parts of that new sprite will become visible; just because we just that exact value room_width, the more close to zero x comes, the more close to room_width x+room_width will come. This could by the way be replaced by something else, like view or whatever you have need of.

One problem is that even though our character is replaced by an image, this is not the real object. It does not have the same properties and all, and we don't draw any picture to the right of that image anyway; therefore when the real object has completely disappeared from the room we have to move it. That is what we do in the second part of the code in step event. We move it to the exact position where the image was, therefore there is no visual change for the users. Everything is very smooth... go ahead now and check the example! Ask if there is anything that you'd like to know.

<b>Example:</b> http://gmtutorials.com/files/wrapviewexample.gm6

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games