Legacy GM Sidescrolling Camera Issues

MGSting

Member
The game I'm developing in GM 1.4 is a sidescrolling beat-em-up like Final Fight and as such, it has a sidescrolling camera that stops moving in a battle area. Here's the code for the camera object:

Code:
Create Event
==========================
view_object[0] = OBJ_Camera;

view_vborder[0] = 384;
view_hborder[0] = 512;

view_visible[0] = true;

x = OBJ_Player.x;
y = OBJ_Player.y;

TargetX = x;
TargetY = y;

State = "Player";

MoveSpeed = 15;
Code:
Step Event
=======================================
MyBR = instance_nearest(x,y,OBJ_BattleRegion);

switch State{

    case "Player":

    TargetX = OBJ_Player.x;
    TargetY = OBJ_Player.y;
    
    if(MyBR.IsActive == true){
        State = "Region";
    }
    break;

    case "Region":
        var MinX = OBJ_BattleRegion.LeftEdge+512;
        var MaxX = OBJ_BattleRegion.RightEdge-512;
        
        var MinY = OBJ_BattleRegion.TopEdge+384;
        var MaxY = OBJ_BattleRegion.BottomEdge-384;   
        
        TargetX = clamp(OBJ_Player.x,MinX,MaxX);
        TargetY = clamp(OBJ_Player.y,MinY,MaxY);
        
        if(MyBR.IsActive == false || distance_to_object(OBJ_BattleRegion) > 0){
             State = "Player";
        }
        
        break;

}


if(distance_to_point(TargetX,TargetY) < MoveSpeed){

    x = TargetX;
    y = TargetY;
    speed = 0;

}else{

    move_towards_point(TargetX,TargetY,MoveSpeed);

}
And here's the code for the battle region, which is the source of the problem.

Code:
Create Event
=========================================
IsActive = false;

RegionHeight = sprite_height;
RegionWidth = sprite_width;

LeftEdge = x-RegionWidth/2;
RightEdge = x+RegionWidth/2;
TopEdge = y-RegionHeight/2;
BottomEdge = y+RegionHeight/2;

HasEnemies = false;
Code:
Step Event
========================================
if(instance_exists(OBJ_Enemy) && point_in_rectangle(OBJ_Enemy.x, OBJ_Enemy.y,LeftEdge,TopEdge,RightEdge,BottomEdge)){

    HasEnemies = true;

}else{

    HasEnemies = false;
    IsActive = false;

}
To test the engine, I created a 3000 x 768 room with two battle zones, having three enemies placed within them. The first one works like it should, but the second causes the camera to shake like crazy. I tried making the room an extra 700 pixels large and spacing both battle zones 768 pixels apart, it didn't work. I tried making the second battle zone 768 pixels away from the end of the room, it didn't work. I think there might be an error in the code that would cause this instead of where I place the battle zones within the room. I could just have one battle zone per room in the finished product, however since enemies don't respawn that would make for a boring experience.

Thanks in advance.
 

CloseRange

Member
you're referencing an object instead of an instance. this means that when getting the data stored inside of the 'object' it will look for the first instance created/placed. This is fine for things like OBJ_player because you will only ever have one of them, so instead you need to figure out what instance of OBJ_BattlRegion you want to reference.
I recomend using instance_nearest.
Code:
    case "Region":
        var MinX = OBJ_BattleRegion.LeftEdge+512;
        var MaxX = OBJ_BattleRegion.RightEdge-512;
        
        var MinY = OBJ_BattleRegion.TopEdge+384;
        var MaxY = OBJ_BattleRegion.BottomEdge-384;   
        
        TargetX = clamp(OBJ_Player.x,MinX,MaxX);
        TargetY = clamp(OBJ_Player.y,MinY,MaxY);
        
        if(MyBR.IsActive == false || distance_to_object(OBJ_BattleRegion) > 0){
             State = "Player";
        }
        break;
that is your "Region" code right now. Change it to this:
Code:
    case "Region":
        var REF = instance_nearest(x, y, OBJ_BattleRegion);
        var MinX = REF.LeftEdge+512;
        var MaxX = REF.RightEdge-512;
        
        var MinY = REF.TopEdge+384;
        var MaxY = REF.BottomEdge-384;   
        
        TargetX = clamp(OBJ_Player.x,MinX,MaxX);
        TargetY = clamp(OBJ_Player.y,MinY,MaxY);
        
        if(MyBR.IsActive == false || distance_to_object(OBJ_BattleRegion) > 0){
             State = "Player";
        }
        break;
 
Top