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

2013/6/1

Transparency

1
2
3
4
Solringolt Solringolt

2013/6/3

#
yes, NormalShot, HeroBasicShot and Protector
danpost danpost

2013/6/3

#
I wrote the code for 'getAlphaAtFoot' for a player walking on hilly ground (not for a bullet hitting an enemy). However, it can be adjusted for that purpose. You probably want to use the 'head' (center of the right edge of the image) of the bullet (not the 'foot' -- center of the bottom edge of the image). The bullet is probably small enough where it really would not matter, though. At any rate (or at the speed at which the bullet is traveling), you probably need to make two or three smaller movements per act, checking for intersecting objects each time. To break the move up into individual pixel moves
int moves = 10;
int x = getX();
int y = getY();
for (int i=0; i<10; i++)
{
    setLocation(x, y);
    move(i);
    Actor actor = getOneIntersectingObject(Enemy.class);
    if (actor != null && getAlphaAtHead(actor) > 200)
    {
        if (actor instanceof Enemy)
        {
            ((Enemy)actor).hit(shotStrength);
            getWorld().removeObject(this);
            return;
        }
        if (actor instanceof Boss)
        {
            ((Boss)actor).hit(shotStrength);
            getWorld().removeObject(this);
            return;
        }
    }
}
I have a feeling that the method 'hit' is exactly the same in all of your boss part classes and the above was coded with that in mind. If so, remove them from all those classes and put one copy of it in the Boss class.
danpost danpost

2013/6/3

#
This code should replace everything from 'move(10)' on line 5 to 'else' on line 59. Change line 5 to:
if (WorldOption.menuDisplayed) return;
and drop the 'else' on line 59.
Solringolt Solringolt

2013/6/3

#
the hit method is the same in every part but is not situated in Boss.class it is rewritten in every Boss part so line 19 don't work. Greenfoot does the whole "for" in one act?
danpost danpost

2013/6/3

#
Solringolt wrote...
the hit method is the same in every part but is not situated in Boss.class it is rewritten in every Boss part so line 19 don't work. Greenfoot does the whole "for" in one act?
So, remove the hit method from the boss part classes and put it in the boss.class code (all sub-classes inherit public methods from their super-classes (just like Boss inherits 'setLocation' from the Actor class). Yes. Every 'act' method that is run, will do so to its completion each act cycle (or frame).
Solringolt Solringolt

2013/6/3

#
Is the return just a way to go out of the function?
Solringolt Solringolt

2013/6/3

#
The problem is I have a lifeLeft variable in every bosspart which isn't the same. I show you the code of one bosspart:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boss1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss1Part1 extends Boss
{
    /**
     * Act - do whatever the Boss1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int lifeLeft;
    public int pointWin;
    
    public Boss1Part1 (int life, int pointEarned)
    {
        lifeLeft = life;
        pointWin = pointEarned;
    }
    public void act() 
    {
        if (!WorldOption.menuDisplayed)randomBossMove();
        if (!WorldOption.menuDisplayed)
        {
            if (WorldOption.gameDifficulty == 1)
            {
                if (Greenfoot.getRandomNumber(5300)  <= 5)
                {
                     getWorld().addObject(new Enemy("enemy2.png",2,4,10),getX(),getY());
                }
            }
            
            if (WorldOption.gameDifficulty == 2)
            {
                if (Greenfoot.getRandomNumber(5300)  <= 10)
                {
                     getWorld().addObject(new Enemy("enemy2.png",2,4,10),getX(),getY());
                }
            }
            
            if (WorldOption.gameDifficulty == 3)
            {
                if (Greenfoot.getRandomNumber(5300)  <= 20)
                {
                     getWorld().addObject(new Enemy("enemy2.png",2,4,10),getX(),getY());
                }
            }
        }
        colisionCheck();
       
    }
    public void hit(int damage) 
    {
        
        
        lifeLeft = lifeLeft - damage;
        if (lifeLeft == 30)
        {
            setImage("boss1part12.png");
        }
        if (lifeLeft == 20)
        {
            setImage("boss1part13.png");
        }
        if (lifeLeft == 10)
        {
            setImage("boss1part14.png");
        }
        if(lifeLeft <= 0)
        {
           int numberOfBosses = getWorld().getObjects( Boss.class ).size();
           ((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
           if (numberOfBosses == 1)
           {
               dropBonus(3);
           }
           getWorld().removeObject(this);
        }
    }
}
danpost danpost

2013/6/3

#
Solringolt wrote...
Is the return just a way to go out of the function?
Yes. The 'return' statement exits the method immediately (without further processing of code within the method.
danpost danpost

2013/6/3

#
Here is what you could do: (1) move 'lifeLeft' and 'pointWin' (both of which I presume are in all boss part classes) to the boss class (2) add 'int part' to the boss class and in each subclass constructor assign its part value to it (3) move the 'hit' method to the boss class, coded as follows:
public void hit(int damage) 
{
    lifeLeft = lifeLeft - damage;
    if (lifeLeft == 30)
    {
        setImage("boss1part"+part+"2.png");
    }
    if (lifeLeft == 20)
    {
        setImage("boss1part"+part+"3.png");
    }
    if (lifeLeft == 10)
    {
        setImage("boss1part"+part+"4.png");
    }
    if(lifeLeft <= 0)
    {
       int numberOfBosses = getWorld().getObjects( Boss.class ).size();
       ((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
       if (numberOfBosses == 1)
       {
           dropBonus(3);
       }
       getWorld().removeObject(this);
    }
}
danpost danpost

2013/6/3

#
Later, if you add boss2 objects, add another field 'int boss' and assign its value in each constructor. Then use its value also in the image names.
Solringolt Solringolt

2013/6/3

#
can you explain me the 2nd point? I never assigned a constructor to a sublass and in this case i don't understand it..
danpost danpost

2013/6/3

#
The lines 18 through 22 in your code above is a (the) constructor for the 'Boss1Part1' class. It is what first gets executed when you create a new object of the class by using 'new Boss1Part1(int, int)'. Above, you are already setting values for the two fields 'lifeLeft' and 'pointWin'. Just set the value for 'part', also. In the above code, you would end up with:
public Boss1Part1(int life, int pointEarned)
{
    lifeLeft = life;
    pointWin = pointEarned;
    part = 1;
}
Solringolt Solringolt

2013/6/3

#
Ok I finally get it compiled but I think there is a mistake at line 8 of the HeroBasicShot code you gave me. The actor only takes enemy.class so boss.class aren't affected no? You told me I would need a getAlphaAtHead but you didn't give it to me right? I will try to adapt the getAlphaAtFoot.
Solringolt Solringolt

2013/6/3

#
I tried but without success....
danpost danpost

2013/6/3

#
You are right. Change 'Enemy.class' in line 8 to 'null'. If you want, you could do enemies first, then bosses, as follows:
for (int i=0; i<10; i++)
{
    setLocation(x, y);
    move(i);
    Actor actor = getOneIntersectingObject(Enemy.class);
    if (actor != null && getAlphaAtHead(actor) > 200)
    {
        ((Enemy)actor).hit(shotStrength);
        getWorld().removeObject(this);
        return;
    }
    actor = getOneIntersectingObject(Boss.class);
    if (actor != null && getAlphaAtHead(actor) > 200)
    {
        ((Boss)actor).hit(shotStrength);
        getWorld().removeObject(this);
        return;
    }
}
There are more replies on the next page.
1
2
3
4