• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

My Mega Man X Engine Code

C

cheezy_squeezy

Guest
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 by a moderator:
C

cheezy_squeezy

Guest
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 by a moderator:
T

Toxicosis

Guest
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.
 
S

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:
C

cheezy_squeezy

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.
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!

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.
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 :)
 
S

Shadowblitz16

Guest
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
 
C

cheezy_squeezy

Guest
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
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.
 
A

amusudan

Guest
Please put hte code in (seperate) spoiler tags, perhaps even make a pastebin (preserves indentations).
 
C

cheezy_squeezy

Guest
Please put hte code in (seperate) spoiler tags, perhaps even make a pastebin (preserves indentations).
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.
 
S

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.
 
C

cheezy_squeezy

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.
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.http://www.sprites-inc.co.uk/
 
S

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);
 
P

Pantheon #538

Guest
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.
 
Top