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

2013/12/16

Proximity and colour help wanted

ScratchMan ScratchMan

2013/12/16

#
To create my latest scenario I would like help on the following points- 1) How would I make an actor detect how many other actors of a certain colour are within a distance x? 2) How do I set the colour of an actor? Any help will be greatly appreciated
Hiimachicken Hiimachicken

2013/12/16

#
you would need to make different images with different colors then use the
setImage("IMAGENAME");
to set the image put your image file where it says 'IMAGENAME'
danpost danpost

2013/12/16

#
1) depends on how you create the actors of different colors 2) depends on what resources you have to create the actors of different colors You need to supply some background about your actors (a) what images do you have or are you creating for your actors; (b) what actor class(es) are you using for these actors; (c) what fields do you have for these classes; .etc.
ScratchMan ScratchMan

2013/12/21

#
The scenario is simple, all the actors are squares that fluctuate between being black or white depending on what amount of the 8 nearest actors that are black or white. All actors come under the Actor class. I need the proximity measure to find out how many squares are within a certain distance (all the squares are static so it will always be the 8 closest) I have no idea what you mean by 'fields' i hope this helps to clarify
danpost danpost

2013/12/21

#
Please show the code of the actor class that is for the squares.
ScratchMan ScratchMan

2013/12/22

#
Currently, it looks a bit like this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class square here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class square extends Actor
{
    /**
     * Act - do whatever the square wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        squareDeath();
        setColour();
        squareSpawn();
    }
    public void squareDeath()
    {
        if(/**square is black*/)
        {
            if(/**number of black squares within x distance is <2*/)
            {
                /**set image to white-square*/;
            }
            else if(/**number of black squares within x distance is>3*/)
            {
                /**set image to white-square*/;
            }
            else
            {
                /**set image to black-square*/;
            }
        }
    }
     public void setColour()
    {
        if(/**square is black*/)
        {
            if(Greenfoot.mouseClicked(this))
            {
                /**set image to white-square*/
            }
        }
        if(/**square is white*/)
        {
            if(Greenfoot.mouseClicked(this))
            {
                /**set image to black-square*/
            }
        }
    }
    public void squareSpawn()
    {
        if(/**square is white*/)
        {
            if(/**number of black squares within x distance === 3*/)
            {
                /**set image to black square*/
            }
        }
    }
}
danpost danpost

2013/12/22

#
Alright, I think I know what you are trying to do; and you will not get the proper results in this way. The state of all the squares on one frame is dependent on the state of all the squares around it on the previous frame. If you change the state of each square individually, then you are changing the conditions that determine the resultant change in neighboring squares. It takes a two-step process to accomplish what you want to do. First, for all squares, determine what the next state should be; second, for all squares, change them to what they should be.
ScratchMan ScratchMan

2013/12/30

#
Thanks, would a timer delay do that?
danpost danpost

2013/12/30

#
The changing of all the squares has to be synchronized. Just do both steps each act method. This would be accomplished by putting the code in your world class. (1) for all squares, determine next state (2) for all squares, set to next state Two back-to-back 'for' loops in the world act method would suffice. You will need either an instance field in the square class or a two-dimensional array in the world class to hold the next state -- the values of which are set by the first loop and used by the second to change the squares.
You need to login to post a reply.