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

2013/7/1

need help for calling a method

1
2
vikasvmads vikasvmads

2013/7/4

#
how can i use the greenfoot package that you use in above,,i am trying to use that package but that give me an error that-"package greenfoot dose not exist"
darkmist002 darkmist002

2013/7/4

#
as in the game i'm making or some package in it? if it's the game i can reupload it onto the site. here's the link for the page it's on, reuploaded it if that's what you where talking about. http://www.greenfoot.org/scenarios/8919
darkmist002 darkmist002

2013/7/4

#
well, probably would've gotten this code for the attacks to work if the school comps had greenfoot 2.2, but right now they have 2.1.2, so i can't use the turnToward() method. here's what i got for it, but it doesn't work as of now. towerAttacks class (parent class of the different tower attacks):
public class towerAttacks extends Actor
{
    /**
     * Act - do whatever the towerAttacks wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    tower t;
    public towerAttacks(tower T)
    {
        t = T;    
    }
    
    public towerAttacks()
    {
        
    }
    
    public void act() 
    {
        // Add your action code here.
        //t.attack();
        moveToEnemy();
        isHit();
    }   
    
    public void moveToEnemy()
    {
        int x = t.enemyPosX();
        int y = t.enemyPosY();
        //turnTowards(x, y); //could use if school comps had greenfoot 2.2
        int dx = x - getX();
        int dy = y - getY();
        double angle = Math.toDegrees(Math.atan2(dy, dx));
        double distance = Math.sqrt((dx*dx) + (dy*dy));
        int z = (int) angle;
        setRotation(z);
        move(1);
    }
    
    public void isHit()
    {
        if(getOneObjectAtOffset(0, 0, enemies.class) != null)
        {
            //do attack damage
            getWorld().removeObject(this);
        }
        if(getX() <=0 || getX() >= getWorld().getWidth() || getY() <= 0 || getY() >= getWorld().getHeight())
        {
            getWorld().removeObject(this);
        }
        
    }
}
tower class (parent of the different towers):
public class tower extends Actor
{
    /**
     * Act - do whatever the tower wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    /**be sure to use the radius method of the world/actor class to see around the tower a certain
     * distance. also add radius upon upgrading the towers. figure out a way to show the radius
    */
    public int attack;
    public int attspd; //in milliseconds
    public int range;
    public int attTimer;
    private long count = 0;
    private long aTime = 0;
    public int price;
    public int x = 0;
    public int y = 0;
    //private int wave = 1; //have wave class come in as well
    public int lvl = 1;
    
    public void act() 
    {
        // Add your action code here.
        /**
         * create the levels of the towers in here. possibly make the attack spd and damage in here
         * as well, in which case also special effects. also put radius of each tower in here for sight
         * or in each specific tower.
         */
        placing();
        attack();
    }    
    
    public void attack()
    {
        //List<enemies> e = getObjectsInRange(range, enemies.class);
        //if(canAttack() && !e.isEmpty())
        if(canAttack() && !getObjectsInRange(range, enemies.class).isEmpty())
        {
            /** attack first enemy.
             * if(clock)
             * {
              *     e(0).health - attack;
              *}
              *
            **/
            
            //enemies temp = e.get(0);
            enemies temp = (enemies)getObjectsInRange(range, enemies.class).get(0);
            x = temp.getX();
            y = temp.getY();
            shoot();
            temp.decHealth();  /**change to a method like:
                                 * public void attack()
                                 *{
                                 *   temp.decHealth(attack) <---would have to change the 
                                 *                  decHealth method to accept an int and 
                                 *                  would lower the health not by decreasing by
                                 *                  1, but by 1*the int (attack of each tower)
                                 *} 
                                **/
            attTimer = attspd;

        }
    }

    private boolean canAttack()
    {
        if(attTimer > 0)
        {
            attTimer --;
        }
        return attTimer == 0;
    }
    
    public void shoot()
    {
        
    }

    public int enemyPosX()
    {
        return x;
    }
    
    public int enemyPosY()
    {
        return y;
    }
    
    public int getPrice()
    {
        return price;
    }
    
    public void placing()
    {
        if(Greenfoot.mouseClicked(this))// && Greenfoot.mouseDragged(null))
        {
// //             MouseInfo m = Greenfoot.getMouseInfo();
// //             setLocation(m.getX(), m.getY());
//         }
        if(Greenfoot.mouseMoved(null) && Greenfoot.mouseClicked(this))
        {
            MouseInfo m = Greenfoot.getMouseInfo();
            setLocation(m.getX(), m.getY());
        }
    }
    }
}
and the tower i've been testing it with:
public class fasttower extends tower
{
    /**
     * Act - do whatever the fasttower wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //att lv 1/2/3          1/2/4/8
    //att spd lv 1/2/3      1/.75/.5/.35
    //price                 5
    //upgrade price         10
    
    //private int attack = 1;
    //private long attspd = 1000; //milliseconds
    //private int range = 5;
    //private int lvl = 1;
    //private long count = 0;
    //private long aTime = 0;
   // private int wave = 1; //have wave class come in as well
    //private minion1 m1;
    //private Object unit;
    
//     public fasttower(minion1 M1)
//     {
//         m1 = M1;
//     }
    
    public fasttower()
    {
        attack = 1;
        attspd = 60;
        range = 55;
        attTimer = 0;
        price = 5;
    }
    
    public void act() 
    {
        // Add your action code here.
        //use getObjectsInRange() method for actors to make a radius for towers
        //update();
        attack();
        //shoot();
    }   
    
    private void update()
    {
        /**if have minerals and click on tower, upgrade to next lv
         * will have to add a minerals class and have it seen in every class.
          *if(minerals == 10 && clickontower && (lv <= 4))
          *{
          *     lvl++;
          *     attack += (attack*lvl);
          *     attspd = attspd - 200;
          *     range += (range*lvl);
          *}
          **/
        
    }
    
    public void shoot()
    {
        int X = getX();
        int Y = getY();        
        ((tdpath)getWorld()).addObject(new fastAttack(), X, Y);
    }
    
}
darkmist002 darkmist002

2013/7/4

#
on a different subject, i did get a damage system in place, and also started on an upgrading system, although it won't upgrade it in game as of now (it will if you right click and click the update method, but clicking on the tower doesn't work right as of now). also added music.
danpost danpost

2013/7/5

#
It is not the tower object that you need to pass to the towerAttack object -- it is the enemies object that you found was in range of the tower that needs to be passed. Also you do not need to know the distance between the towerAttack object and the enemies object, just the angle.
darkmist002 darkmist002

2013/7/8

#
the reason i passed the tower object was so that i could give the enemies object position to the attack object. didn't even use the distance so not sure why i kept it in. not sure how to tell the attack object the position of the enemy at the position unless i create the attack object when the tower sees the unit, and change the attack objects constructor so that it gets the x and y from the position of the unit.
darkmist002 darkmist002

2013/7/8

#
well, i can get the attack objects to move to the units now from what i said ^, but if it misses the unit, it doesn't follow it, it sits there till the next unit comes and then attacks it. should i just make the move higher than 2 to make sure it doesn't miss, or is there a way to get it to follow it and attack it? (of course i haven't added the attack damage to it yet, so it just moves to the unit and removes itself when it interests with it). here's the coding that makes it follow it: fastAttack (child of the towerAttack)
public class fastAttack extends towerAttacks
{
    /**
     * Act - do whatever the fastAttack wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    //damage        1/2/4
    //att spd       .75/.5/.35 sec
    int x, y;
    public fastAttack(int X, int Y)
    {
        x = X;
        y = Y;
    }
    
    public fastAttack()
    {
        
    }
    
    public void act() 
    {
        // Add your action code here.
        moveTo(x, y);
        isHit();
    }   

    
    private void moveTo(int x, int y)
    {
        double xX = x - getX();
        double yY = y - getY();
        double angle = Math.atan2(yY, xX)*180/Math.PI;
        setRotation( (int) angle);
        move(2);

    }
}
here's the towerAttack (parent class)
public class towerAttacks extends Actor
{
    /**
     * Act - do whatever the towerAttacks wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    tower t;
    public towerAttacks(tower T)
    {
        t = T;    
    }
    
    public towerAttacks()
    {
        
    }
    
    public void act() 
    {
        // Add your action code here.
        //t.attack();
        moveToEnemy();
        isHit();
    }   
    
    public void moveToEnemy()
    {
        int x = t.enemyPosX();
        int y = t.enemyPosY();
        //turnTowards(x, y); //could use if school comps had greenfoot 2.2
        int dx = x - getX();
        int dy = y - getY();
        double angle = Math.toDegrees(Math.atan2(dy, dx));
        double distance = Math.sqrt((dx*dx) + (dy*dy));
        int z = (int) angle;
        setRotation(z);
        move(1);
    }
    
    public void isHit()
    {
        if(getOneObjectAtOffset(0, 0, enemies.class) != null)
        {
            //do attack damage
            getWorld().removeObject(this);
        }
        else if(getX() <=0 || getX() >= getWorld().getWidth() || getY() <= 0 || getY() >= getWorld().getHeight())
        {
            getWorld().removeObject(this);
        }
        
    }
}
danpost danpost

2013/7/8

#
Again, I suggest that instead of passing the tower object to the constructor of the towerAttack object you should be passing the enemies object that was found in proximity of the tower which triggered the towerAttack object to be created. This way, the towerAttack object can continuously track the location of the enemies object and chase it down.
darkmist002 darkmist002

2013/7/8

#
ok, got it, was taking what you said the wrong way. just passed the unit itself instead of the coordinates, and it works now thank you.
darkmist002 darkmist002

2013/7/8

#
hmm, i do need help with one more thing on the attack objects. while trying to get an update system to work, i made it where updating increases attack speed, so more attack objects come out. how do i get the object to remove itself if the object it was spawned for is removed? i'm getting an error cuz the actor isn't in the world anymore. tried coding where if the enemies object i passed into that attack object ==null, then it should remove itself, but it isn't working.
danpost danpost

2013/7/8

#
Your reference to the object should not become 'null' when it is removed from the world; but, if the object is not in the world, then using 'getWorld' on the object should return 'null'.
if (enemy.getWorld() == null)
darkmist002 darkmist002

2013/7/9

#
thanks, added an if else statement to the act method of the attack objects with that, and it works now.
You need to login to post a reply.
1
2