Legacy GM SOS!!! Tower Defense Game Tower Control Menu Problem

T

thken

Guest
Hello everyone, I am new for Game maker and I am making a tower defense game with game maker studio 1.4.

However I encountered a problem when i am making the tower control menu
(you can treat it as a menu next to any tower on the field with buttons "Upgrade tower", "Repair Tower", "Sell Tower", "Sell Tower". However, whether I put the code into my towerParent object or objectController object, there are always problems.

Be specific, when I put two towers from Tower Selection Menu to the field, I can open the Tower Control Menu when I click the last tower I put, but I cannot open the Tower Control Menu when I click the first tower I put.

Anyone with experience making tower defense game can help for this? Any hints for critical functions I should input or any approach are all appreciated.

My code is as follow:
In object objTowerParent, global left pressed event:

Code:
if(position_meeting(mouse_x,mouse_y,self))
{
    if(instance_exists(objTowerBattleMenu))
    {
        with(objTowerBattleMenu)
        {
            instance_destroy();
        }
        with(objSell)
            {
                instance_destroy();
            }
        with(objUpgrade)
            {
                instance_destroy();
            }
        with(objRepair)
            {
                instance_destroy();
            }
        with(objDestroy)
            {
                instance_destroy();
            }
        
    }
    else
    {
    
        instance_create(x,y,objTowerBattleMenu);
        objTowerBattleMenu.depth=-1000;
        instance_create(x+36,y,objSell);
 
        instance_create(x-36,y,objUpgrade);
        objSell.depth=-1100 ;
        objUpgrade.depth=-1100;
        instance_create(x+14,y+22,objDestroy);
        instance_create(x-14,y+22,objRepair);
        objDestroy.depth=-1100 ;
        objRepair.depth=-1100;
    }
}
else
{
    if(position_meeting(mouse_x,mouse_y,objUpgrade)){}
    else if(position_meeting(mouse_x,mouse_y,objSell)){}
    else if(position_meeting(mouse_x,mouse_y,objDestroy)){}
    else if(position_meeting(mouse_x,mouse_y,objRepair)){}
    else
    if(!position_meeting(mouse_x,mouse_y,objTowerBattleMenu))&&!(position_meeting(mouse_x,mouse_y,objUpgrade))&&!(position_meeting(mouse_x,mouse_y,objSell))
    {
        if(instance_exists(objTowerBattleMenu))
        {
            with(objTowerBattleMenu)
            {
                instance_destroy();
            }
            with(objSell)
            {
                instance_destroy();
            }
            with(objUpgrade)
            {
                instance_destroy();
            }
            with(objRepair)
            {
                instance_destroy();
            }
            with(objDestroy)
            {
                instance_destroy();
            }
        }
    
    
    
    }
}
 

Attachments

W

WinuX

Guest
Hello,
could you try this code:

if(position_meeting(mouse_x,mouse_y,self) && instance_exists(objTowerBattleMenu))
{
with(objSell)
{
instance_destroy();
}
with(objUpgrade)
{
instance_destroy();
}
with(objRepair)
{
instance_destroy();
}
with(objDestroy)
{
instance_destroy();
}
with(objTowerBattleMenu)
{
instance_destroy();
}
}

if(position_meeting(mouse_x,mouse_y,self) && !instance_exists(objTowerBattleMenu))
{
instance_create(x,y,objTowerBattleMenu);
objTowerBattleMenu.depth=-1000;
instance_create(x+36,y,objSell);

instance_create(x-36,y,objUpgrade);
objSell.depth=-1100 ;
objUpgrade.depth=-1100;
instance_create(x+14,y+22,objDestroy);
instance_create(x-14,y+22,objRepair);
objDestroy.depth=-1100 ;
objRepair.depth=-1100;
}

if(instance_exists(objTowerBattleMenu))
{
if(position_meeting(mouse_x,mouse_y,objUpgrade)){}
else if(position_meeting(mouse_x,mouse_y,objSell)){}
else if(position_meeting(mouse_x,mouse_y,objDestroy)){}
else if(position_meeting(mouse_x,mouse_y,objRepair)){}
}
 
T

thken

Guest
wait, the first part cannot function - "if(position_meeting(mouse_x,mouse_y,self) && instance_exists(objTowerBattleMenu)) {} "
it seems it is because this will make the second part valid and the seond part will run automatically
 
W

WinuX

Guest
wait, the first part cannot function - "if(position_meeting(mouse_x,mouse_y,self) && instance_exists(objTowerBattleMenu)) {} "
it seems it is because this will make the second part valid and the seond part will run automatically
Ah wait, i see the problem i removed something try this:

To make the code clean you need to make the script "battle_menu_destroy"

///battle_menu_destroy();
if(instance_exists(objTowerBattleMenu))
{
with(objTowerBattleMenu)
{
instance_destroy();
}
with(objSell)
{
instance_destroy();
}
with(objUpgrade)
{
instance_destroy();
}
with(objRepair)
{
instance_destroy();
}
with(objDestroy)
{
instance_destroy();
}
}


Then in the TowerObject you put in this code:


///Battle Menu
//Destroy the existing menu if there's one.
if(position_meeting(mouse_x,mouse_y,self))
{
battle_menu_destroy();
}

//Create new menu
if(position_meeting(mouse_x,mouse_y,self) && !instance_exists(objTowerBattleMenu))
{
instance_create(x,y,objTowerBattleMenu);
objTowerBattleMenu.depth=-1000;
instance_create(x+36,y,objSell);

instance_create(x-36,y,objUpgrade);
objSell.depth=-1100 ;
objUpgrade.depth=-1100;
instance_create(x+14,y+22,objDestroy);
instance_create(x-14,y+22,objRepair);
objDestroy.depth=-1100 ;
objRepair.depth=-1100;
}

//Button actions
if(instance_exists(objTowerBattleMenu))
{
if(position_meeting(mouse_x,mouse_y,objUpgrade)){}
else if(position_meeting(mouse_x,mouse_y,objSell)){}
else if(position_meeting(mouse_x,mouse_y,objDestroy)){}
else if(position_meeting(mouse_x,mouse_y,objRepair)){}
}

//Destroy menu when not klicked on Tower
if(!position_meeting(mouse_x,mouse_y,objTowerBattleMenu))&&!(position_meeting(mouse_x,mouse_y,objUpgrade))&&!(position_meeting(mouse_x,mouse_y,objSell))
{
battle_menu_destroy();
}

as you see the script is only there so we don't have to write the same destroy code again :D
you can of course also do it that way but i prefer scripts over repeating code.
I also commented the code, to make it easier to spot a mistake if there's one.
 
W

WinuX

Guest
or wait should you be able to have more than 1 menu open at the time?
then i must change something.
 
Top