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

2014/5/2

Adding smokeBomb

1
2
akilino akilino

2014/5/3

#
i have 7 bad classes. Each one of them runs in different ways. Everyone is removed, as soon as they touch the bomb.class. When they do touch, the animation of adding the smokeBomb will occur. Bad.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Bad extends badGeral
{
    public static int PASSO = 1;
    private int dirX;
    private int dirY; 
    
    private int contadorBad = 0;

    //Construtor de inicialização
    public Bad()
    {
//         image1 = new GreenfootImage("bad.png");
//         image2 = new GreenfootImage("bad1.png");
//         image3 = new GreenfootImage("bad2.png");
//         image4 = new GreenfootImage("bad3.png");
        setImage(image1);
        dirX = 0;
        dirY = 1;
    }

    /**
     * Act - do whatever the bad wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        contadorBad++;
        moveBad();
        if(contadorBad == velocidade)
        {
            move();
            switchImage();
            contadorBad = 0;
        }
    } 

    //Muda de direção
    public void moveBad()
    {
        if(getY() == 28 && dirY == 1)
        {
            dirY = -1;
        }

        if(getY() == 1 && dirY == -1)
        {
            dirY = 1;
        }
    }

    public void move()
    {
        setLocation(getX() + PASSO * dirX, getY() + PASSO * dirY);
    }    
}
Bad1.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bad1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bad1 extends badGeral
{
    private static int PASSO = 1;
    private int dirX;
    private int dirY;

    private int contadorBad = 0;

    //Sequencia de imagens para sensação de movimento do Bad
    public Bad1()
    {
//         image1 = new GreenfootImage("bad.png");
//         image2 = new GreenfootImage("bad1.png");
//         image3 = new GreenfootImage("bad2.png");
//         image4 = new GreenfootImage("bad3.png");
        setImage(image1);
        dirX = 1;
        dirY = 0;
    }

    /**
     * Act - do whatever the bad1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        contadorBad++;
        moveBad();
        if(contadorBad == 3)
        {
            move();
            switchImage();
            contadorBad = 0;
        }
    }    

    //muda de direção
    public void moveBad()
    {
        if(getX() == 28 && dirX == 1)
        {
            dirX = -1;
        }

        if(getX() == 1 && dirX == -1)
        {
            dirX = 1;
        }
    }

    public void move()
    {
        setLocation(getX() + PASSO * dirX, getY() + PASSO * dirY);
    }
}
I also created the badGeral, where it would be supposed to put the methods, so I could just call them in each bad class. Problem is, I'm not having access to it, and I don't know why! I can't access the move() method, neither the moveBad() method.. badGeral().class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class badGeral extends pacActors
{
    public static final GreenfootImage image1 = new GreenfootImage("bad.png");
    public static final GreenfootImage image2 = new GreenfootImage("bad1.png");
    public static final GreenfootImage image3 = new GreenfootImage("bad2.png");
    public static final GreenfootImage image4 = new GreenfootImage("bad3.png");

    public int velocidade = 3;
    public int dirX;
    public int dirY;
    public static int PASSO = 1;
    /**
     * Act - do whatever the badGeral wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    
    }    

    public void switchImage()
    {
        if(getImage() == image1)
        {
            setImage(image2);
        }
        else
        if(getImage() == image2)
        {
            setImage(image3);
        }
        else
        if(getImage() == image3)
        {
            setImage(image4);
        }
        else
        if(getImage() == image4)
        {
            setImage(image1);
        }
    }
    
//     public void moveBad()
//     {
//         if(getY() == 28 && dirY == 1)
//         {
//             dirY = -1;
//         }
// 
//         if(getY() == 1 && dirY == -1)
//         {
//             dirY = 1;
//         }
//     }
}
danpost danpost

2014/5/3

#
The problem with accessing the values in the badGeral class is due to the fact that you are declaring fields with the same names in each of its subclasses. By doing this, you are hiding the fields declared in the badGeral class that are given to each instance of its subclasses. In other words, each instance has two 'dirX', 'dirY' and 'PASSO' fields. If you use 'dirX', you get the value of the one in that particular subclass; if you use 'super.dirX', you will get the one in the badGeral class. You can remove lines 6 through 8 from the bad class and lines 11 through 13 from the bad1 class and similar with all your subclasses of badGeral. If the only differences in your 7 bad type subclasses is the direction of movement, then a single class would be sufficient (especially if the direction and placement into the world was random).
danpost danpost

2014/5/3

#
Along the same lines (I noticed your rekindling of a 3-year-old discussion), when referring to image actors, I presume you mean actors that have no functionality other than to display an image. A single class could be used to display all those type of images (even if the image may change). For one that changes, all you need is a reference to that actor and you can then use (in pseudo-code) 'referenceName.setImage(greenfootImage)'.
akilino akilino

2014/5/3

#
danpost wrote...
The problem with accessing the values in the badGeral class is due to the fact that you are declaring fields with the same names in each of its subclasses. By doing this, you are hiding the fields declared in the badGeral class that are given to each instance of its subclasses. In other words, each instance has two 'dirX', 'dirY' and 'PASSO' fields. If you use 'dirX', you get the value of the one in that particular subclass; if you use 'super.dirX', you will get the one in the badGeral class. You can remove lines 6 through 8 from the bad class and lines 11 through 13 from the bad1 class and similar with all your subclasses of badGeral. If the only differences in your 7 bad type subclasses is the direction of movement, then a single class would be sufficient (especially if the direction and placement into the world was random).
And what do I have to do in order to get the move() method from the badGeral? Because I did super.move(), and nothing happened ... Also, my game is getting slow and got no clue of what I'm doing. I wanted that someone could take a look at my game, and see what's wrong, since I'm not an expert on this, and I'm getting stressed out with the deadline coming.
danpost danpost

2014/5/3

#
Copy lines 24 through 57 of your bad class and replace lines 46 through 57 in your badGeral class with it. Then, in all subclasses of badGeral, remove the 'act', the 'moveBad' and the 'move' methods. You should already have removed the 'dirX', 'dirY' and 'PASSO' fields from those classes, also. You will need to remove lines 14 through 21 of the badGeral class also.
You need to login to post a reply.
1
2