Repos

MELHARFI-2D-Game-Engine.

Got to repo.

12. Remove, Clean, FixedObjectLayer

Remove object

When you want to remove an object from screen like an enemy or whatever object, you must remove it from the layer where it's stored.

If it's a background object so you should look for it in Manager.manager.GfxBgrList, and so on for the other layers.

Let's suppose you have made an object as follow

                        
using System;
using System.Drawing;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "manager");
            Bmp img1 = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(img1);
        }
    }
}
                        
                      

After some events you want to remove that object from the game you just need to do as follow :

But the best way is add control programatically not by drag and drop, yet nothing stop you to do so.

                        
namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "manager");
            Bmp img1 = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(img1);
        }
    }
}

                        
                      

Pay attention that object img1 must be declared in the same scoop, im mean the code bellow is working :

                        
namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "manager");
            Bmp img1 = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(img1);

            manager.ObjectLayer.Remove(img1);
        }
    }
}
                        
                      

The object is removed just after it's added, and it did work because the variable img is known cause we are in the same scope of block-code.

What happen if you are out of that scope, like if you are in another class, then the object img1 is know recognized.

The code bellow will rise an exception

                        
using System;
using System.Drawing;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "manager");
            Bmp img1 = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(img1);
            manager.ObjectLayer.Remove(img1);

            deletePlayer(manager);
        }

        private void deletePlayer(Manager manager)
        {
            manager.ObjectLayer.Remove(img1); /// error in this line, cause it does not exist in the current context (out of its declaration scope)
        }
    }
}
                        
                      

And here is handy using of the Name property of an object, meaning with that you can look for it in the layer and then remove it by its name as follow :

                        
using System;
using System.Drawing;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "manager");
            Bmp img1 = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
            manager.ObjectLayer.Add(img1);
            manager.ObjectLayer.Remove(img1);

            deletePlayer(manager);
        }

        private void deletePlayer(Manager manager)
        {
            manager.ObjectLayer.RemoveAll(f => f.Name == "supermario");
            
        }
    }
}
                          
                        
                      

If you don't associate a name to an object then you can't remove it or deal with cause you lost your identification name.

  • Cleaning Objects
  • In case you want to clear all objects from screen depending on witch layer you want, you just use this:

    manager.BackgroundLayer.Clear();

    manager.ObjectLayer.Clear();

    manager.TopLayer.Clear();

    Otherwize if you want to clear all layers :

    manager.TopLayer.Clear();

    manager.Clear();

  • FixedObjectLayer
  • First let's talk about a situation could happen to you, because it was the case for me in my RPG game.

    Admitting that we have map full of objects and players ..., and you want your player to move to the other screen (next map),

    In that point of time you should clean the full screen of objects and players because you are moving to another map with its own object and different players.

    You do so you can use the 4 lines of codes shown before to clean the map, BUT what about some important stuff like your life point, you information, stats, hud ...

    These informations should not be removed and should remain fixed on the screen whatever you do unless if you use many instance of Game Engine, but assuming you use only one instance for all your stuff.

    Maybe you can use your own logic by checking every object and decide if you want to keep it or not, but it's going to need a lot of conditions and not really what we want cause we are lazy :)

    To escape that issue, i made another layer FixedObjectLayer to store every important object that you don't want to be removed.

    Let's add a text control a slife point that we want it to be as Fixed object

                            
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using MELHARFI.Manager;
    using MELHARFI.Manager.Gfx;
    
    namespace _2dProject
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Manager manager = new Manager(this, "manager");
                manager.Background = Color.CadetBlue;
    
                Bmp supermario = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
                manager.ObjectLayer.Add(supermario);
    
                Txt lifePoints = new Txt("Life points : 5", new Point(0, 0), "lifePoint", Manager.TypeGfx.Top, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, manager);
                manager.TopLayer.Add(lifePoints);
                manager.FixedObjectLayer.Add(lifePoints);   // <-- fixing object
            }
        }
    }
    
                            
                          

    if we clean screen using :

                            
    using System;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using MELHARFI.Manager;
    using MELHARFI.Manager.Gfx;
    
    namespace _2dProject
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Manager manager = new Manager(this, "manager");
                manager.Background = Color.CadetBlue;
    
                Bmp supermario = new Bmp(@"supermario.png", new Point(100, 200), "supermario", Manager.TypeGfx.Object, true, manager);
                manager.ObjectLayer.Add(supermario);
    
                Txt lifePoints = new Txt("Life points : 5", new Point(0, 0), "lifePoint", Manager.TypeGfx.Top, true, new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, manager);
                manager.TopLayer.Add(lifePoints);
                manager.FixedObjectLayer.Add(lifePoints);   // <-- fixing object
                cleanScreen();
            }
    
            private void cleanScreen()
            {
                // catching manager in out of its declaration scope.
                Manager manager = Manager.Managers.First(f => f.Name == "manager");
                manager.Clear();
            }
        }
    }
                            
                          

    Services

    App Developpement

    Want me to dev an app for you ? dont hesitate to contact me.

    Programming

    Are you looking for a coder/teammate for your project ? Let's give it a try.

    Job Invitation

    Have a proposal for me ? we can discuss about it.

    Donation maybe ?!

    You want to buy me a coffe ? m.elharfi@gmail.com