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

2013/7/10

drawing a radius around the towers when their selected

darkmist002 darkmist002

2013/7/10

#
if you wanted to make it where when the tower is selected, you wanted to show the radius of the tower, would you have to create an image for each tower? i figured you would have to use an if statement that tells if you click on the tower, but not really sure if i have to create an image for each tower and draw an oval around it for size range, or if there is a way to have it in the parent class and just draw an oval for the size range. if not, then you'd have to have a method that says
    GreenfootImage g = new GreenfootImage("image name of specific tower");
 if(Greenfoot.mouseClicked(this))
{
    g.drawOval(this.getX(), this.getY(), range, range);
}
else if (Greenfoot.mouseClicked(null))
   g = g;
would that work?
darkmist002 darkmist002

2013/7/10

#
tried what i posted and it didn't work. also tried this but it didn't work either:
if(Greenfoot.mouseClicked(this))
        {
//         if(Greenfoot.mouseClicked(this) && canUpdate() && lvl < 4)
//         {
            radiusPic();
            if(canUpdate() && lvl < 4)
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if(mouse.getClickCount() == 2)
                {
                    ((tdpath)getWorld()).mineralsSub(uprice);
                    update();
                }
            }
        }
public void radiusPic()
    {
        GreenfootImage g = new GreenfootImage("fasttower.png");  
//         if(Greenfoot.mouseClicked(this))  
//         {  
            g.drawOval(this.getX(), this.getY(), range, range);  
//         }  
        //else if(Greenfoot.mouseClicked(null)) 
           //g;
    }
danpost danpost

2013/7/10

#
You cannot use mouse clicks on tower object to do multiple things unless there was a way to absolutely determine what you want to do each time (you are using mouse clicks to update tower objects). You probably should just use the condition that the mouse is on the object (and the object is placed) for the determination of showing the range of the tower. It might be better to create a new actor class to show the range of the towers. There are some good reasons for suggesting this, but let us just say that it may avoid problems later. How you go about doing it is up to you. You could create one that constantly follows the mouse and anytime it intersects a tower object, shows the range of the tower; and when not on a tower can have a transparent image. Or, you could have each tower detect the mouse moving on and off the placed tower; so, when it moves on, create the range showing object and when it moves off, remove it. There are probably other way to go about it; but, hopefully I gave you some ideas.
darkmist002 darkmist002

2013/7/11

#
i got it to draw one now (by making it register it when i double click it, and set the upgrade system to work when it is triple clicked), but it isn't centered on the image, and doesn't center on it when i upgrade it and redouble click on it to see the new range. (in the fasttower class)
public void radiusPic()
    {
        GreenfootImage g = new GreenfootImage("fasttower.png");  
        g.setColor(Color.BLUE);
//         g.fillOval(0, 0, range-1, range-1);
//         g.setColor(Color.RED);
        g.drawOval(0, 0, range*2-1, range*2-1);
            //g.drawOval(this.getX(), this.getY(), range, range);
        setImage(g);

    }
(in the tower parent class)
if(Greenfoot.mouseClicked(this))
        {
//         if(Greenfoot.mouseClicked(this) && canUpdate() && lvl < 4)
//         {
//             radiusPic();
            GreenfootImage g = new GreenfootImage("fasttower.png");
            setImage(g);
            MouseInfo mouse = Greenfoot.getMouseInfo();
                if(mouse.getClickCount() == 2)
                {
//                     ((tdpath)getWorld()).mineralsSub(uprice);
//                     update();
                    radiusPic();
                }
                if(mouse.getClickCount()== 3 && (canUpdate() && lvl < 4))
                {
                    {
                        ((tdpath)getWorld()).mineralsSub(uprice);
                        update();
                    }
                }
        }
is there a method that makes the drawn opal center on the image?
darkmist002 darkmist002

2013/7/11

#
tried doing it with an if statement in the world act method to check if the mouse moved over the tower object, but nothing happened, which is why i did it with 2 mouse clicks instead. coding for moving it over: (world class)
public void act()
    {
        if(Greenfoot.mouseMoved(ft))
        {
            ft.radiusPic();
        }
        if(Greenfoot.mouseMoved(null))
        {
            GreenfootImage g = new GreenfootImage("fasttower.png");
            ft.setImage(g);
        }
    }
danpost danpost

2013/7/11

#
For having it work with the mouse over the tower, the code should be in the tower class act method (or a method it calls):
// add an instance field
private boolean mouseOn;
// after 'if (!placed) return;'
if (!mouseOn && Greenfoot.mouseMoved(this))
{
    // add range image or create and add a range object at location of tower
    mouseOn = true;
}
if (mouseOn && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
{
    // remove range image or remove range object
    mouseOn = false;
}
darkmist002 darkmist002

2013/7/11

#
i got it to create an oval with the double clicking code, but it won't center it. and i don't think the range is right for the created circle but i can't tell since it won't center it.
darkmist002 darkmist002

2013/7/11

#
ok, got it to center now. had to reput the image of the tower in after making a new image of the range. got the idea from another td i saw on here (the author in the readme says ninto). coding for it:
tpic = new GreenfootImage("fasttower.png");
public void radiusPic()
    {
        g = new GreenfootImage(range *2, range*2);
        g.setColor(Color.BLUE);
        int h = tpic.getHeight();
        int w = tpic.getWidth();
        g.drawOval(0, 0, getRange()*2 -1, getRange()*2 -1);
        g.drawImage(tpic, range-w/2, range-h/2);
        setImage(g);

    }
the getRange() is a method to get the int value for range, since i was having trouble before getting the range after updating. probably would work with the range value itself now tho.
darkmist002 darkmist002

2013/7/11

#
only problem i can't figure out is why it won't let me use the radiusPic() method and the regPic() method in the parent class. it works fine if i leave them blank in there and override them in the children classes, but for some reason it won't let me do it in the parent. I define the tpic variable in each child class constructor like i do with their damage, range, and attackspeed variables, but for some reason it doesn't register in the methods.
danpost danpost

2013/7/11

#
If you declare both the 'tpic' and 'g' images in the tower class and create the images in the constructors of the subclasses, then you should be able to use the code I provided in the tower class using 'setImage(tpic);' and 'setimage(g);' at lines 11 and 6 in my code above. When upgrading the tower the 'g' image will need to be updated, also.
You need to login to post a reply.