Tuesday, February 7, 2017

GameMaker Studio 2 Simple Parallax Background

Many tutorials of Game Maker Language parallax background over the internet is using view_xview() and view_yview() function. Unfortunately, this function is deprecated in GameMaker Studio 2. 😕

While searching for the rightway to do the parallax background, the very basic view_current example script from the gamemaker documentation give a nice parallax effect too. 


Solid color background :
So, here is the description from view_current syntax :

Description

This read only variable is only valid in the Draw Event and returns the current view port being rendered. The return value will change during the draw event when you have various views as the draw event is called once for each view port in succession. So when (for example) you are using view port[0] and view port[1] in your game room, the draw event for ALL instances will be run twice, once for each port, and with this variable you can check to see what view is currently being drawn. In general, this is used to only render specific details to a single port when multiple view ports are visible in the room at the same time. See the example code below.

Syntax:
view_current;

Returns:
Real (view port index from 0 to 7)

Example:
if view_current == 0
   {
   var xx = camera_get_view_x(view_camera[0]);
   var yy = camera_get_view_y(view_camera[0]);
   draw_text(xx + 32, yy + 32, "Player 1");
   }
else
   {
   var xx = camera_get_view_x(view_camera[1]);
   var yy = camera_get_view_y(view_camera[1]);
   draw_text(xx + 32, yy + 32, "Player 2");
   }

The above code checks to see which view is currently being drawn and then draws a different text to each view based on the return value.

How to implement it?

Simply create an object and create an instance layer. Put the layer behind other layer and put the object there.

On the object draw event :


if view_current == 0
   {
   var xx = camera_get_view_x(view_camera[0]);
   var yy = camera_get_view_y(view_camera[0]);
   draw_sprite_tiled(sprBackground,0,xx/2,yy);
   }



And thats it. A nice one-layer parallax background. 
Please comment for the rightway to do it. Hey, i'm a newbie anyway. 😁




4 comments:

  1. Hey,

    thank u soooo much for the tutorial. I was looking 3 days for a simple solution to this problem. All videos and guides are for lower versions of gamemaker 2. But a simple draw does the trick...

    Here a the "dirty Code" for the problem:

    Create:

    parallax [1] = 95;
    parallax [2] = 85;
    parallax [3] = 75;


    posY [1]= 100;
    posY [2]= 100;
    posY [3]= 100;

    posX [1]= 100;
    posX [2]= 100;
    posX [3]= 100;


    Draw Event:
    {
    var xx = camera_get_view_x(view_camera[0]);
    var yy = camera_get_view_y(view_camera[0]);



    draw_sprite(background_mid, 0, (xx * parallax [3]/100) + posX[3] , yy + posY[3]);
    draw_sprite(spr_background__Sun, 0, (xx* parallax [1]/100)+ posX[1] , yy + posY[1]);
    }

    ReplyDelete
    Replies
    1. Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download Now

      >>>>> Download Full

      Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download LINK

      >>>>> Download Now

      Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download Full

      >>>>> Download LINK p7

      Delete
  2. Hey than you so much for posting this, I've been looking for a good way to implement parallax in gms 2 for months. A lot of the other tutorials have parallax that doesn't work for objects that don't move very much, I never would have thought to use draw_sprite_tiled.

    ReplyDelete
  3. Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download Now

    >>>>> Download Full

    Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download LINK

    >>>>> Download Now

    Tukitanotes.Blogspot.Com: Gamemaker Studio 2 Simple Parallax Background >>>>> Download Full

    >>>>> Download LINK CE

    ReplyDelete