DevLog: Creating a Platformer Pt.4

DAY 7 (Cont.)

Last night I talked about with() and ‘other’ and how you can use them to approach your code structure in a new way and hopefully, a simpler way. I also started to use these techniques to start tackling moving platforms.

I added both horizontal moving platforms and sub-pixel movement to the platforms last night. Again, to add sub-pixel movement, I collect the rounded sub-pixel numbers and add them to the movement calculations when they exceed a whole number.

// Example
var vxNew, vyNew;

// Handle sub-pixel movement
cx += vx;
cy += vy;
vxNew = round(cx);
vyNew = round(cy);
cx -= vxNew;
cy -= vyNew;

repeat (abs(vyNew)) {
    if (!place_meeting(x, y + sign(vyNew), oStopper)) {
        with (oParEntity) {
            if (place_meeting(x, y + 1, other.id)) {
                if (!PlaceMeetingException(x, y + sign(vyNew), oParSolid, other.id))
                    y += sign(vyNew);
            }
            
            if (place_meeting(x, y - 1, other.id))
                y += sign(vyNew);
            
        }
        y += sign(vyNew);
        phy_position_y = y;
    }
    else
        vy *= -1;
}

The horizontal movement was a bit more complex, but it now works how I’d like it to. I will be releasing a complete platformer engine over on the Game Maker Marketplace when this is all said and done.

Composite Objects

I’m not super sure what this idea is actually referred to as, but I tend to call it ‘composite objects’. Basically, what I want is a system that easily allows me to have several objects moving as one. This will be important once I start working level design. For example, I may want to “attach” spikes to a moving platform and/or have a moving platform that is a move complex shape.

We can do this by moving objects relative to a parent object. To begin, I will setup some simple variables for objects that may move along with a parent object.

parent = 0;
parentInit = false;

During the ‘begin step’ of the first frame that the object is live, I’ll find the relative distance from the designated parent object.

if (!parentInit) {
    if (parent) {
        dx = x - parent.x;
        dy = y - parent.y;
    } else {
        dx = 0;
        dy = 0;
    }
    parentInit = true;
}

From there, just move the object to the parent’s new position while maintaining the same offset.

if (parent) {
    x = parent.x + dx;
    y = parent.y + dy; 
}

Custom Creation Code

Another feature of Game Maker is the ability to customize creation code. This is additional creation code that will be executed after (or before if you change your GM preferences) the object’s normal creation code. You can set custom creation code by right-clicking an object in the room editor and scrolling down to ‘creation code’. This is where we will setup the parent object.

So I right-click a spike object and add “parent = platform1;”. This brings us to another point: do NOT go looking for the default instance name given to objects in Game Maker. You can also right-click an object and rename a specific instance. Name your parent platforms something simple and use that name to assign it to the spike(s).

With this being done, all of the movement basics are implemented. Next I will focus on some other things that we’ll want upfront. A basic camera, gamepad support, and things of that nature.

Thanks for reading,

-Z

Advertisements
DevLog: Creating a Platformer Pt.4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s