• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Enemy Problem

B

betitoas1

Guest
Well, i am making a boss, and it moves by itself(randomly directions,0,90,180,270) all works perfectly except this, i have created a room for the boss and he goes outside of the room while he's walking, how can i make that the boss stays in the room bounds?
This is the code i'm using for the movement of the boss(He only chooses a random direction and he moves it's very simple)

Code:
///Create Event
hpdremboss = 100;
dir = 1;
canmove = true;
sp = 1.5;
///Step Event

//Moves randomly
dir = choose(1,2,3,4,dir,dir,dir,dir);
//Cases
if canmove = true {
switch(dir)
{
    case 1:
        motion_set(0,sp);
        canmove = false;
        alarm[0] = 60;
        break;
    case 2:
        motion_set(90,sp);
        canmove = false;
        alarm[0] = 60
        break;
    case 3:
        motion_set(180,sp);
        canmove = false;
        alarm[0] = 60
        break;
    case 4:
        motion_set(270,sp);
        canmove = false;
        alarm[0] = 60
        break;                       
        }
    }
This is the room:
FallenThreatsRoom.jpg

The boss is the red square, it's for a test xD
And this is the in-game room, if you see the boss is outside the map.
Runner 2016-07-28 21-21-35-18.jpg
 
S

SyntaxError

Guest
Code:
///Create Event
hpdremboss = 100;
dir = 1;
canmove = true;
sp = 1.5;
///Step Event

//Moves randomly
dir = choose(0, 1, 2, 3, dir, dir, dir, dir);

if (canmove == true)
{
        motion_set(dir * 90,sp);
        canmove = false;
        alarm[0] = 60;
                 
}

if (bbox_left < 0 || bbox_top < 0 || bbox_right > room_width || bbox_bottom > room_height) // If any part of the boss objects collision mask are outside of the room.
{
        motion_set(point_direction(x, y, room_width * .5, room_height * .5),sp); // Move him toward the center of the room.
}
This is a pretty simple solution and probably not desirable as he will move diagonally toward the very center of the room. There are many ways to do this but this may help you figure out your own method.
 
B

betitoas1

Guest
Code:
///Create Event
hpdremboss = 100;
dir = 1;
canmove = true;
sp = 1.5;
///Step Event

//Moves randomly
dir = choose(0, 1, 2, 3, dir, dir, dir, dir);

if (canmove == true)
{
        motion_set(dir * 90,sp);
        canmove = false;
        alarm[0] = 60;
                
}

if (bbox_left < 0 || bbox_top < 0 || bbox_right > room_width || bbox_bottom > room_height) // If any part of the boss objects collision mask are outside of the room.
{
        motion_set(point_direction(x, y, room_width * .5, room_height * .5),sp); // Move him toward the center of the room.
}
This is a pretty simple solution and probably not desirable as he will move diagonally toward the very center of the room. There are many ways to do this but this may help you figure out your own method.
Hahaha thank you so much i was not capable to figure a solution, but yes it works i had to modificate some things but it was very easy :) thank you again.
if (bbox_left < 656 || bbox_top < 720 || bbox_right > 1280 || bbox_bottom > 1120) // If any part of the boss objects collision mask are outside of the room.
{
motion_set(point_direction(x, y, room_width * .5, room_height * .5),sp); // Move him toward the center of the room.
}
 
Top