This site requires JavaScript, please enable it in your browser!
Greenfoot back
B_rob1
B_rob1 wrote ...

2018/5/5

[Help] Turret Aiming within range

1
2
B_rob1 B_rob1

2018/5/5

#
I am fairly new to Greenfoot and I am trying to make a turret that will attack the nearest unit.class within a set area, I know that the following will be used: getObjectsInRange(_radius_, _cls_)
B_rob1 B_rob1

2018/5/5

#
public class TurretPlatform extends Actor
{
    public boolean deployed = false;
    public void act() 
    {
        buyTurret();
    }    
    public void buyTurret()
    {
        
        if (Greenfoot.mouseClicked(this) == true && deployed == false)
        {
            getWorld().addObject(new Turret(), getX(), getY());
            deployed = true;
        }
    }
}
This is my current code to spawn the turret on the platform, later I will add a currency that will be required to create one, but my current hurdle is making the turret have limited range & Distance priority
jamesN616 jamesN616

2018/5/5

#
Try doing this:
//in Turret class

public void findClosestInRange()
{
    List<unit> units = getObjectsInRange(radius, unit.class
    if(units.size != 0)
    {
        int unitX = units.get(0).getX();
        int unitY = units.get(0).getY();
        turnTowards(unitX, unitY);
}
}
This creates a list of all units in the given radius, so the nearest unit is stored in the list at 0. If there is an object close by, get that objects x and y positions, and aim at it. You'll need to specify an int "radius", the radius around the turret This method, findClosestInRange(), should be called from act(); Additionally, you'll need to add the line: import java.util.List; on the line after import greenfoot.*; you can add code in a separate method to attack the units.
jamesN616 jamesN616

2018/5/5

#
make sure to close parentheses on line 5, sorry about that
B_rob1 B_rob1

2018/5/5

#
jamesN616 wrote...
Try doing this:
//in Turret class

public void findClosestInRange()
{
    List<unit> units = getObjectsInRange(radius, unit.class
    if(units.size != 0)
    {
        int unitX = units.get(0).getX();
        int unitY = units.get(0).getY();
        turnTowards(unitX, unitY);
}
}
This creates a list of all units in the given radius, so the nearest unit is stored in the list at 0. If there is an object close by, get that objects x and y positions, and aim at it. You'll need to specify an int "radius", the radius around the turret This method, findClosestInRange(), should be called from act(); Additionally, you'll need to add the line: import java.util.List; on the line after import greenfoot.*; you can add code in a separate method to attack the units.
Quick question, what does != 0 mean? I have seen this before but I don't know what it does.
jamesN616 jamesN616

2018/5/5

#
units.size() returns the number of objects in the list units "!=" means does not equal, so if the list doesn't have anything in it, then the turret doesn't need to aim anywhere. Another tricky thing is that if one object is in range, the units.size() will be 1, but that unit is stored at index 0. Also, in my carelessness, I neglected to add parentheses after size. Should look like ..."units.size()"...
B_rob1 B_rob1

2018/5/5

#
jamesN616 wrote...
units.size() returns the number of objects in the list units "!=" means does not equal, so if the list doesn't have anything in it, then the turret doesn't need to aim anywhere. Another tricky thing is that if one object is in range, the units.size() will be 1, but that unit is stored at index 0. Also, in my carelessness, I neglected to add parentheses after size. Should look like ..."units.size()"...
I out the part that you left out, thanks for clearing up the != for me!
jamesN616 jamesN616

2018/5/5

#
No problem. Good luck with your project!
B_rob1 B_rob1

2018/5/5

#
@jamesN616 Thanks for the help so far, but what can i do to call my public int Money = 10; from MyWorld to TurretPlatform.class for the purpose of checking if the Money is enough to purchase and to subtract money after the purchase
jamesN616 jamesN616

2018/5/5

#
There's a lot going on here, but I think this is what you're trying to do:
    //In world
    //in act
    
    public boolean getMoney()
    {
        if(Greenfoot.getMouseInfo() != null)
        {
            int X = Greenfoot.getMouseInfo().getX();
            int Y = Greenfoot.getMouseInfo().getX();
            List<TurretPlatform> platforms = getObjectsAt(X,Y);
            if(platforms.size() != 0)
            {
                TurretPlatform platform = (TurretPlatform) platforms.get(0);
                if(Money >= 10)
                {
                    platform.buyTurret(); 
                }
            }
        }
        
    }
    
    //not in act 
    public void countMoney(int dollars)
    {
        Money = Money + dollars;
    }
    
    //in TurretPlatform
    //in act
    public void changeMoney(int dollars)
    {
        MyWorld world = (MyWorld) getWorld();
        world.countMoney(dollars);
    }
jamesN616 jamesN616

2018/5/5

#
In the world's act method, you'll check if the mouse is touching the turret platform. You do this by getting a list of turrets that the mouse is on, and converting the first turret platform into an actor, so that we can call methods in only the one we're touching. From earlier, your "buyTurret()" method looks like it adds a new turret when run, so we'll call that method from the world when the mouse clicks on it. Also in the world, we have a method to keep track of how much money we're spending/gaining In turret platform, we have a method to call the world's countMoney() method, while specifying the amount, + or - to be added. You can call this method and specify the change in money in the method parameters wherever you're changing the amount of money In the world, you'll also need to add the import java.util.List; to get the List library
jamesN616 jamesN616

2018/5/5

#
Another amendment: changeMoney(int dollars) in TurretPlatform shouldn't be in act(), it should be with however you're changing money
jamesN616 jamesN616

2018/5/5

#
jamesN616 wrote...
Another amendment: changeMoney(int dollars) in TurretPlatform shouldn't be in act(), it should be with however you're changing money
more amendments: line 9 should be ...getY(); getObjectsAt() needs a specified class to look for, so getObjectsAt(X,Y,TurretPlatform.class);
B_rob1 B_rob1

2018/5/5

#
jamesN616 wrote...
There's a lot going on here, but I think this is what you're trying to do:
    //In world
    //in act
    
    public boolean getMoney()
    {
        if(Greenfoot.getMouseInfo() != null)
        {
            int X = Greenfoot.getMouseInfo().getX();
            int Y = Greenfoot.getMouseInfo().getX();
            List<TurretPlatform> platforms = getObjectsAt(X,Y);
            if(platforms.size() != 0)
            {
                TurretPlatform platform = (TurretPlatform) platforms.get(0);
                if(Money >= 10)
                {
                    platform.buyTurret(); 
                }
            }
        }
        
    }
    
    //not in act 
    public void countMoney(int dollars)
    {
        Money = Money + dollars;
    }
    
    //in TurretPlatform
    //in act
    public void changeMoney(int dollars)
    {
        MyWorld world = (MyWorld) getWorld();
        world.countMoney(dollars);
    }
hold up your confusing me, i just want to be able to access the variable Money from MyWorld to TurretPlatform can you help me with that b
B_rob1 B_rob1

2018/5/5

#
jamesN616 wrote...
jamesN616 wrote...
Another amendment: changeMoney(int dollars) in TurretPlatform shouldn't be in act(), it should be with however you're changing money
more amendments: line 9 should be ...getY(); getObjectsAt() needs a specified class to look for, so getObjectsAt(X,Y,TurretPlatform.class);
i already have a method that spawns the turret on the platform, and it works fine at the moment, i just need to add another && (World Money) >= 10 and it should fix the purchasing part
There are more replies on the next page.
1
2