Z
Zeathus
Guest
I want to make a 2D Platformer game but i just can't find a working tutorial on enemy movement. I am a total begginer with programming and if someone could help i would like to thank you!
hsp = 0;
speed = 3;
//1 will be right and -1 will be left
direction = 1;
//direction, speed
hsp = direction * speed;
//collision with wall
if(place_meeting(x+hsp,y,oWall){
while(!place_meeting(x+sign(hsp),y,oWall){
x = x + sign(hsp);
}
direction = direction * -1;
}
x = x + hsp;
Yea like the koopas from Super Mario World.What kind of enemy movement? When I think of enemy movement on a platform, I think of the Koopas in Super Mario World. They go back and forth. I would check a position ahead of you and below you to see if you would walk off the platform. If you would, turn around. You can use the bbox_functions. For example, to check below the koopa, check bbox_bottom + 1. To check ahead, you could also use the bbox_functions: bbox_right + 1 if the koopa is moving right, bbox_left - 1 if the koopa is moving left. image_xscale will be really helpful as you can flip the koopa back and forth.
Edit: unhelpful example code.
The walking worked but when he hit the wall the direction didn't change he just walked off!On the ememy create event add
Than you will need to make a wall objectCode:hsp = 0; speed = 3; //1 will be right and -1 will be left direction = 1;
This will be a pixel perfect collision with wall
In the enemy step event add
Code://direction, speed hsp = direction * speed; //collision with wall if(place_meeting(x+hsp,y,oWall){ while(!place_meeting(x+sign(hsp),y,oWall){ x = x + sign(hsp); } direction = direction * -1; } x = x + hsp;
I want like when he comes to a edge that he changes the direction. Like the koopas in Super Mario World!Hello, how do you want? Like in Mario? When an enemy hits the wall, it changes direction?
You are setting "speed" in the create event. You are moving the player twice. He will move "speed" in "direction" and "x + hsp". If you want to use the name without using the built-function, just change it to Speed or move_speed or something.On the ememy create event add
Than you will need to make a wall objectCode:hsp = 0; speed = 3; //1 will be right and -1 will be left direction = 1;
This will be a pixel perfect collision with wall
In the enemy step event add
Code://direction, speed hsp = direction * speed; //collision with wall if(place_meeting(x+hsp,y,oWall){ while(!place_meeting(x+sign(hsp),y,oWall){ x = x + sign(hsp); } direction = direction * -1; } x = x + hsp;
It worked I just had to change the built-function direction and it worked but is there a way I can make that when i jump on his head he diesYou are setting "speed" in the create event. You are moving the player twice. He will move "speed" in "direction" and "x + hsp" in the loop. If you want to use the name without using the built-function, just change it to Speed or move_speed or something.
I think you also need to set "hsp" to 0 in that loop. You move him against the wall, but keep "hsp" which means he will move into the wall.
Yeah I know it's no problem but is there a way of making it when I jump on his head he dies?Sorry, my fault, just change the speed to spd, because the speed is built in varriable
//oPlayer is the Player object
if(place_meeting(x,y-1,oPlayer)){
instance_destroy();
}
Thank you very much and one more question if I add a collision event and put it to the player and make it so that when he touches the player the room restarts will the death still work or do i have to make it differently?Add this command to the enemy's step event
Code://oPlayer is the Player object if(place_meeting(x,y-1,oPlayer)){ instance_destroy(); }
room_restart();
//this is the create event
grav = 0.4;
hsp = 0;
vsp = 0;
jumpSpeed = 10;
moveSpeed = 6;
image_index = 0;
image_speed = 0;
//this is the step event
// Get input
kLeft = -keyboard_check(ord("A"));
kRight = keyboard_check(ord("D"));
kJump = keyboard_check_pressed(ord("W"));
// Use input
move = kLeft + kRight;
hsp = move * moveSpeed;
if (vsp < 10) { vsp += grav; };
if (place_meeting(x, y + 1, Floor_obj)) { vsp = kJump * -jumpSpeed }
// H Collisions
if (place_meeting(x + hsp, y, Floor_obj))
{ while (!place_meeting(x + sign(hsp), y, Floor_obj))
{ x += sign(hsp); }
hsp = 0;
}
x += hsp;
// v Collisions
if (place_meeting(x, y + vsp, Floor_obj))
{ while (!place_meeting(x, y + sign(vsp), Floor_obj))
{ y += sign(vsp); } vsp = 0; }
y += vsp;
if(place_meeting(x,y-1,oPlayer)){
instance_destroy();
}
if(place_meeting(x,y-oPlayer.vsp,oPlayer)){
instance_destroy();
}
//Collision with enemy
if(place_meeting(x+hsp,y,oEnemy)){
room_restart();
}