1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

My Mega Man X Engine Code

Discussion in 'Programming' started by cheezy_squeezy, Jun 27, 2016.

  1. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Someone asked me to share the code for my Mega Man X Engine found here:



    Here's the information for obj_player. Everything else is standard stuff. obj_options basically stores my control variables and obj_player_dash is the fading effect while the player dashes. They are pretty basic so I will only share the code for the player There are 101 tutorials on how to make bullets, etc.

    Information about object: obj_player
    Sprite: spr_player_idle
    Solid: false
    Visible: true
    Depth: 0
    Persistent: false
    Parent:
    Children:
    Mask:
    No Physics Object
    Create Event:
    execute code:

    /// Initialize

    // Platform Physics
    hspd = 0;
    vspd = 0;
    jspd = 15;
    grav = 0.5;
    fric = 1;
    spd = 8;

    facing = 0;
    dashing = false;
    dashended = false;
    wall = false;
    fallmax = 30;

    canblur = true;
    canshoot = true;
    shooting = false;

    bulletsalive = 0;
    shotcharge = 0;
    shotchargemax = 125;

    execute code:

    /// Create necessary objects

    if !instance_exists(obj_options) {
    instance_create(0,0,obj_options);
    }

    if !instance_exists(obj_view) {
    instance_create(x,y,obj_view);
    }

    Alarm Event for alarm 0:
    execute code:

    /// Dash ending

    dashended = true;

    Alarm Event for alarm 1:
    execute code:

    /// Can blur

    canblur = true;

    Alarm Event for alarm 2:
    execute code:

    /// Canshoot

    canshoot = true;

    Alarm Event for alarm 3:
    execute code:

    /// Not shooting

    shooting = false;

    Step Event:
    execute code:

    /// Platform physics

    // Get the control input
    var right = obj_options.right;
    var left = obj_options.left;
    var jump = obj_options.jump;
    var jumprelease = obj_options.jumprelease;
    var dash = obj_options.dash;
    var dashhold = obj_options.dashhold;
    var dashreleased = obj_options.dashreleased;

    // Check for ground
    if (place_meeting(x, y+1, obj_solid)) {
    if dashended = true && !place_meeting(x,y-1,obj_solid) && !place_meeting(x,y-33,obj_solid) {
    dashing = false;
    dashended = false;
    }
    vspd = 0;

    // Jumping
    if (jump) {
    if dashhold {
    dashing = true;
    dashended = true;
    }
    vspd = -jspd;
    shooting = false;
    dashended = true;
    }
    } else {
    // Gravity
    if (vspd < fallmax)
    {
    vspd += grav;
    }
    if (jumprelease && vspd <-2) {
    vspd = -2;
    }
    }

    // Moving right
    if !(dashing = true && place_meeting(x,y+1,obj_solid)) {
    if (right) {
    if (hspd < spd) {
    if hspd = 0 && wall = 0{
    shooting = false;
    }
    hspd += fric;
    } else {
    hspd = spd;
    }
    }
    // Moving left
    if (left) {
    if (hspd > -spd) {
    if hspd = 0 && wall = 0 {
    shooting = false;
    }
    hspd -= fric;
    } else {
    hspd = -spd;
    }
    }
    }

    // Check for not moving
    if !(dashing = true && place_meeting(x,y+1,obj_solid)) {
    if ((!right && !left) || (left && right)) {
    if (hspd != 0) {
    if (hspd < 0) {
    hspd += fric;
    } else {
    hspd -= fric;
    }
    }
    }
    }

    // Horizontal Collisions
    if place_meeting(x+hspd, y, obj_solid) {
    while (!place_meeting(x+sign(hspd), y, obj_solid)) {
    x += sign(hspd);
    }
    hspd = 0;
    }

    // Move Horizontally
    x += hspd;

    // Vetical Collisions
    if place_meeting(x, y+vspd, obj_solid) {
    while (!place_meeting(x, y+sign(vspd), obj_solid)) {
    y += sign(vspd);
    }
    vspd = 0;
    }

    // Move Vertically
    y += vspd;

    if vspd > fallmax {
    vspd = fallmax;
    }

    if !place_meeting(x,y+1,obj_solid) && place_meeting(x+1,y,obj_solid) {
    dashing = false;
    wall = 1;
    } else {
    if !place_meeting(x,y+1,obj_solid) && place_meeting(x-1,y,obj_solid) {
    dashing = false;
    wall = 2
    } else {
    wall = 0;
    }
    }

    if wall = 1 {
    image_xscale = 1;
    facing = 180;
    if jump {
    if dashhold {
    dashing = true;
    vspd = -20;
    hspd = -spd*2;
    dashended = true;

    } else {
    vspd = -20;
    hspd = -spd*1.25;
    }
    }
    }

    if wall = 2 {
    image_xscale = -1;
    facing = 0;
    if jump {
    if dashhold {
    dashing = true;
    vspd = -20;
    hspd = spd*2;
    dashended = true;
    } else {
    vspd = -20;
    hspd = spd*1.25;
    }
    }
    }

    if wall >= 1 {
    fallmax = 5;
    } else {
    fallmax = 25;
    }

    if dashreleased {
    if place_meeting(x,y-1,obj_solid) || place_meeting(x,y-33,obj_solid) {
    dashended = true;
    } else {
    dashing = false;
    }
    }

    if dash && dashing = false && place_meeting(x,y+1,obj_solid) {
    dashing = true;
    alarm[0] = 40;
    shooting = false;
    }

    if dashing = false {
    fric = 1;
    spd = 8;
    mask_index = spr_player_mask
    }

    if dashing = true {
    if place_meeting(x,y-1,obj_solid) || place_meeting(x,y-33,obj_solid) {
    if facing = 0 {
    if left {
    facing = 180;
    }
    }
    if facing = 180 {
    if right {
    facing = 0;
    }
    }
    }
    if place_meeting(x,y+1,obj_solid) {
    mask_index = spr_player_dash_mask;
    if facing = 0 {
    spd = 16;
    hspd = 16;
    fric = 2;
    if left {
    if place_meeting(x,y-1,obj_solid) || place_meeting(x,y-33,obj_solid) {
    dashended = true;
    } else {
    dashing = false;
    }
    }
    }
    if facing = 180 {
    spd = 16;
    hspd = -16;
    fric = 2;
    if right {
    if place_meeting(x,y-1,obj_solid) || place_meeting(x,y-33,obj_solid) {
    dashended = true;
    } else {
    dashing = false;
    }
    }
    }
    }
    spd = 16;
    fric = 2;
    if canblur = true {
    var trail = instance_create(x,y,obj_player_dash);
    trail.sprite_index = sprite_index;
    trail.image_xscale = image_xscale;
    trail.image_index = image_index;
    trail.image_speed = 0;
    canblur = false;
    alarm[1] = 3;
    }
    }

    if !(dashing = true && place_meeting(x,y+1,obj_solid)) && wall = 0 {
    if left {
    facing = 180;
    }
    if right {
    facing = 0;
    }
    }

    execute code:

    /// Shoot

    var shoot = obj_options.shoot;
    var charge = obj_options.charge;
    var chargereleased = obj_options.chargereleased;

    // Shoot pellets
    if shoot && canshoot = true {
    if facing = 0 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x+90,y+20,obj_bullet);
    } else {
    bullet = instance_create(x+65,y-23,obj_bullet);
    }
    bullet.direction = 0;
    bullet.image_angle = 0;
    instance_create(x,y,obj_muzzleflash);
    }
    if facing = 180 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x-90,y+20,obj_bullet);
    } else {
    bullet = instance_create(x-65,y-23,obj_bullet);
    }
    bullet.direction = 180;
    bullet.image_angle = 180;
    instance_create(x,y,obj_muzzleflash);
    }
    canshoot = false;
    alarm[2] = 5;
    alarm[3] = 15;
    shooting = true;
    }

    // Charge
    if charge {
    shotcharge += 1;
    }

    if shotcharge > shotchargemax {
    shotcharge = shotchargemax;
    }

    // Half charged effect
    if shotcharge >= 30 {
    if !instance_exists(obj_charge1) {
    instance_create(x,y,obj_charge1);
    }
    } else {
    if instance_exists(obj_charge1) {
    with obj_charge1 instance_destroy();
    }
    }

    // Fully charged bubble effect
    if shotcharge >= 125 {
    if !instance_exists(obj_charge2) {
    instance_create(x,y,obj_charge2);
    }
    } else {
    if instance_exists(obj_charge2) {
    with obj_charge2 instance_destroy();
    }
    }

    // Shooting charge shots
    if chargereleased {
    if shotcharge = 125 {
    if facing = 0 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x+80,y+20,obj_chargeshot2);
    } else {
    bullet = instance_create(x+50,y-23,obj_chargeshot2);
    }
    bullet.direction = 0;
    bullet.image_angle = 0;
    instance_create(x,y,obj_muzzleflash);
    }
    if facing = 180 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x-80,y+20,obj_chargeshot2);
    } else {
    bullet = instance_create(x-50,y-23,obj_chargeshot2);
    }
    bullet.direction = 180;
    bullet.image_angle = 180;
    instance_create(x,y,obj_muzzleflash);
    }
    }
    if shotcharge >= 30 && shotcharge < 125 {
    if facing = 0 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x+80,y+20,obj_chargeshot1);
    } else {
    bullet = instance_create(x+50,y-23,obj_chargeshot1);
    }
    bullet.direction = 0;
    bullet.image_angle = 0;
    instance_create(x,y,obj_muzzleflash);
    }
    if facing = 180 {
    if dashing = true && place_meeting(x,y+1,obj_solid) {
    bullet = instance_create(x-80,y+20,obj_chargeshot1);
    } else {
    bullet = instance_create(x-50,y-23,obj_chargeshot1);
    }
    bullet.direction = 180;
    bullet.image_angle = 180;
    instance_create(x,y,obj_muzzleflash);
    }
    }
    if shotcharge >= 30 {
    shooting = true;
    alarm[3] = 20;
    canshoot = false;
    alarm[2] = 25;
    }
    shotcharge = 0;
    }
     
    Last edited: Dec 1, 2016
    Shodo and megasxlr89 like this.
  2. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Here's the code for the sprites for the player. This goes in the step event. I couldn't put it in the post because it exceeded the character limit.

    execute code:

    /// Sprites

    if facing = 0 && wall = 0 {
    image_xscale = 1;
    }

    if facing = 180 && wall = 0 {
    image_xscale = -1;
    }

    // Sprite
    if wall >= 1 {
    if shooting = true {
    sprite_index = spr_player_buster_wall;
    } else {
    sprite_index = spr_player_wall;
    }
    } else {
    if !place_meeting(x,y+1,obj_solid) {
    if y < yprevious {
    image_speed = 0.125;
    if shooting = true {
    sprite_index = spr_player_buster_jump;
    } else {
    sprite_index = spr_player_jump;
    }
    } else {
    image_speed = 0.125;
    if shooting = true {
    sprite_index = spr_player_buster_fall;
    } else {
    sprite_index = spr_player_fall;
    }
    }
    } else {
    if dashing = true {
    image_speed = 0.125;
    if shooting = true {
    sprite_index = spr_player_buster_dash;
    } else {
    sprite_index = spr_player_dash;
    }
    } else {
    if hspd = 0 {
    image_speed = 0.1;
    if shooting = true {
    sprite_index = spr_player_buster_idle;
    } else {
    sprite_index = spr_player_idle;
    }
    } else {
    // Moving
    if hspd != 0 {
    image_speed = 0.3;
    if shooting = true {
    sprite_index = spr_player_buster_run;
    } else {
    sprite_index = spr_player_run;
    }
    }
    }
    }
    }
    }
     
    Last edited: Dec 1, 2016
    Shodo, Someoneudk and megasxlr89 like this.
  3. Toxicosis

    Toxicosis Member

    Joined:
    Jun 25, 2016
    Posts:
    95
    Can you please put it in code tags and add some tabulation so we can see the nesting more clearly? I'm a huge fan of MMX, and I'm aiming for something sorta similar, so I'm really curious about this, but I'm also a beginner to coding.
     
    Treecase86 likes this.
  4. Someoneudk

    Someoneudk Guest

    I'm new to coding. When you write in your post "execute code" are you saying to open up a new code file to continue on? Also, the codes "Alarm Event for alarm 0" or 1 and so on, doesn't seem to work right. I get the error of "Assignment operator expected." at the bottom. Finally, is the beginning of your first post in the Create Event? Thanks.
    EDIT: Ah, nevermind. I figured it out by myself. Thanks anyway.
     
    Last edited by a moderator: Jul 25, 2016
  5. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Sorry, I saw this posted on while mobile and couldn't reply and forgot about it until today. Glad you figured it out, good luck!

    Sorry, the actual code was indented to make it neater but because I took the whole information of the object it got rid of my formatting.
    Edit: I've also put in code as a tag, thanks for alerting me about that :)
     
  6. Shadowblitz16

    Shadowblitz16 Member

    Joined:
    Jun 23, 2016
    Posts:
    601
    I would really line the sine wave code for the 4th charge shot (spiral crush buster)
    I can't seem to reproduce its sine offsets
     
  7. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    I'm sorry, what? There are only 3 shots: normal shot, half charged shot and fully charged shot. All the sprites are from Mega Man X4 for the PSX, with no particles or anything like that. The sprite origins for the shots are either centered or the y co-ordinate is centered and the x-co-ordinate is just before the tip of the shot.
     
  8. Shadowblitz16

    Shadowblitz16 Member

    Joined:
    Jun 23, 2016
    Posts:
    601
    oh I was talking about the one from MMX1 sorry
     
  9. amusudan

    amusudan Lousiest of Potatoes

    Joined:
    Jun 20, 2016
    Posts:
    183
    Please put hte code in (seperate) spoiler tags, perhaps even make a pastebin (preserves indentations).
     
  10. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Ok, I know what you're talking about. But I haven't done anything to do with MMX1? This is all from Mega Man X4.
     
  11. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Spoiler tags? I might be being an idiot but I don't see how to do that? And I'd rather not put it on pastebin.
     
  12. amusudan

    amusudan Lousiest of Potatoes

    Joined:
    Jun 20, 2016
    Posts:
    183
    I believe you need to do [+spoiler+] to start it and [+/+spoiler+] to end it
     
  13. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    Oh ok, I've never seen that anywhere. I've put the code in spoiler tags now.
     
    amusudan likes this.
  14. Shodo

    Shodo Guest

    Hello there, I'm new to coding too. Thanks for the code. But still, I've some issues and can't make it work right. Is there any possible way to share the source file? I'm trying to work on Zero play game and I get some errors with wall jump and dashing, this without saying about my problem with sprites haha. Thank you.
     
  15. cheezy_squeezy

    cheezy_squeezy Member

    Joined:
    Jun 27, 2016
    Posts:
    19
    I don't have the source file from the video anymore, it's been updated a lot and sharing it would be a headache. Can you post any code and I can maybe see what's wrong with it?

    For wall jumping you just put the same code for jumping when grounded into the check for if you're wall sliding.
    For dashing you just check if the dash button is pressed and if it is then set the dashing variable to true, and add a check for if it's set to true that will double your speed and hspd depending on the direction you're facing.

    What are you having trouble with with sprites? I get all of them from Sprites-Inc or Spriter's Resource and I've listed the code that controls how the sprites are drawn onto the player.
     
  16. Shodo

    Shodo Guest

    Sorry for the late response, and, if it's not to bother you, here what I've got:

    I tried two diferent ways to make the engine for a Mega Man game, one I'm using what I've learned through a course from Udemy. Which is the only the base programing I understand. And the other was using your code.

    For starters, whenever I try to import sprites I've to change(edit) the spaces between then, mostly 50x50 px, creating from a strip. Or there's any other way I can just copy and paste part of the Sprite sheet to make it easier to work with?

    And this is my code. When I change the idle sprit to a walk Sprite, the code on wall jump does not work correctly.

    ///move_state();
    //control the player
    var right = keyboard_check(vk_right);
    var left = keyboard_check(vk_left);
    var up = keyboard_check_pressed(vk_up);
    var up_release = keyboard_check_released(vk_up);
    var down = keyboard_check(vk_down);
    var maxjumps = 2;
    //gravity
    if (!place_meeting(x, y+1, Solid)){
    vspd += grav;

    //controll jump height
    if (up_release and vspd < -6) { vspd = -6; }
    } else {
    vspd = 0;

    //reset jumps when touch the ground
    jumps = maxjumps;


    //sprite idle
    if (hspd == 0) {
    sprite_index = spr_zero_idle;
    image_speed = .15;
    }
    }
    //jump and double jump
    if ((up) and jumps > 0) {
    vspd = -14;
    jumps -= 1;
    }


    //move to the sides
    if (right) {
    hspd = spd;
    //slide down left wall
    if (place_meeting(x+1, y, Solid) and (!place_meeting(x, y+1, Solid) and !left)){
    vspd = spd;
    //reset jumps when touch the wall
    jumps = maxjumps;

    //wall jump to left
    if ((up) and jumps > 0) { vspd = -14; hspd = -20; jumps -= 1;}
    }
    }
    if (left) {
    hspd = -spd;
    //slide down right wall
    if (place_meeting(x-1, y, Solid) and (!place_meeting(x, y+1, Solid) and !right)){
    vspd = spd;

    //reset jumps when touch the wall
    jumps = maxjumps;

    //wall jump to right
    if ((up) and jumps > 0) { vspd = -14; hspd = 20; jumps -= 1;}
    }
    }
    //turn around
    if (hspd != 0){ image_xscale = sign(hspd);}

    //friction
    if (!right and !left) {hspd = 0;}
    move(Solid);
     
  17. Pantheon #538

    Pantheon #538 Member

    Joined:
    Sep 17, 2018
    Posts:
    1
    How did you set up obj_options?
    I'm just having a real hard time with the options and view objects, and i'm not sure what other objects i need to make.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice