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

#
Actually I figured out the thing, it was aids In World:
 public int getMoney()
    {
        return Money;
    }
    public void subMoney()
    {
        Money -=10;
    }
In Platform:
if (Greenfoot.mouseClicked(this) == true && deployed == false && ((MyWorld) getWorld()).getMoney() >= 10 )
        {
            getWorld().addObject(new Turret(), getX(), getY());
            deployed = true;
            ((MyWorld) getWorld()).subMoney();
        }
jamesN616 jamesN616

2018/5/5

#
Sorry for the confusion. Up until a moment ago, I didn't actually know you could call variables from other actors outside of a method, but there's definitely a lot to Greenfoot that I'm not fluent with. Anyways, you could do this instead:
//in TurretPlatform
public void changeMoney()
{
    MyWorld world = (MyWorld) getWorld();
    world.Money = amount;
    //set Money to a specific number
    //alternatively, this could be world.Money = world.Money + amount;
    //this lets you add to the total amount of money
}

//OR
public void changeMoney(int amount)
{
    MyWorld world = (MyWorld) getWorld();
    world.Money = amount;
    //or world.Money = world.Money + amount
}
Replace lines 31+ with either of these. I would recommend the second one because it allows you to define the amount of money you're adding as a parameter when you call the method.
danpost danpost

2018/5/5

#
You would probably use something like this in the TurretPlatform class:
if (!deployed && Greenfoot.mouseClicked(this) && ((MyWorld)getWorld()).Money >= 10)
B_rob1 B_rob1

2018/5/5

#
danpost wrote...
You would probably use something like this in the TurretPlatform class:
if (!deployed && Greenfoot.mouseClicked(this) && ((MyWorld)getWorld()).Money >= 10)
I have solved this hurdle, but I am trying to change the way you purchase the building, using calling cards that are displayed when hovered on the calling card & the TurretPlatform, currently it only properly displays the calling card when hovering on the Platform but not when moving the mouse on the calling card,
public class TurretPlatform extends Actor
{
    public boolean cardPlaced = false;
    public void act() 
    {
        displayPurchases();
    }    
    public void displayPurchases()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (cardPlaced == false && Greenfoot.mouseMoved(this))  
        {  
            getWorld().addObject(new turretCard(), getX()-30, getY());
            cardPlaced = true;
        }  
        if (cardPlaced == true && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))  
        {  
             removeTouching(turretCard.class);
             cardPlaced = false;  
        }
    }
}
If you could help me solve this issue it would be greatly appreciated.
danpost danpost

2018/5/6

#
You should probably retain a reference to the turretCard so you can check mouse movement on it also.
public class TurretPlatform extends Actor
{
    private Actor card = new turretCard();
    
    public void act()
    {
        displayPurchases();
    }
    
    public void displayPurchases()
    {
        if (card.getWorld() == null)
        {
            if (Greenfoot.mouseMoved(this))
            {
                getWorld().addObject(card, getX()-30, getY());
            }
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this) && !Greenfoot.mouseMoved(card))
            {
                getWorld().removeObject(card);
            }
        }
    }
}
The last part of line 21 is what was needed.
B_rob1 B_rob1

2018/5/8

#
@danpost what can I do to call getdeployedstatus() boolean from snipercard to TurretPlatform to add a layer of flexibility on making new towers
danpost danpost

2018/5/8

#
If by snipercard, you mean turretCard object, then:
turretCard tc = (turretCard) card;
boolean deplayedStatus = tc.getdeployedstatus();
or
boolean deplayedStatus = ((turretCard) card).getdeployedstatus();
You need to login to post a reply.
1
2