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

2019/5/28

Transparency on Click

1
2
3
Celesterra Celesterra

2019/5/30

#
Ok, that worked. Thank you! Now, how do I get the program to detect if two numbers have been clicked? If two numbers are the same or make a sum of ten (ex: 1 and 9), I want to put a cover over it (that is a class). However, if two numbers are clicked that aren't a match, I want them to reset to unclicked numbers. How would I do that?
danpost danpost

2019/5/30

#
Celesterra wrote...
how do I get the program to detect if two numbers have been clicked? If two numbers are the same or make a sum of ten (ex: 1 and 9), I want to put a cover over it (that is a class). However, if two numbers are clicked that aren't a match, I want them to reset to unclicked numbers.
Ahhh, now that is a different monster altogether. In that case, you need to keep a reference to the one initially clicked:
private static Number firstClicked;
It is made a class field (static) because all objects of the class need to see its value. Now, either the field will be null (not contain a Number object) or it will contain an object. This can be checked on to determine what to do when an object is clicked.
Celesterra Celesterra

2019/5/30

#
How would I utilize that into the code? I know that it would have to be within the if statement for the mouseClicked, but I don't know how to add it in. Where would I put that into use?
danpost danpost

2019/5/30

#
Celesterra wrote...
How would I utilize that into the code? I know that it would have to be within the if statement for the mouseClicked, but I don't know how to add it in. Where would I put that into use?
if (Greenfoot.mouseClicked(this))
{
    if (firstClicked == null)
    {
        firstClicked = this;
        ...
    }
    else
    {
        ...
        firstClicked = null;
    }
}
Celesterra Celesterra

2019/5/30

#
Ok, I wrote that down but I have no idea where to go from there. How do I detect if a different Number object is clicked?
if(Greenfoot.mouseClicked(this))
        {
            getImage().setTransparency(355 - getImage().getTransparency());
            clicked = true;
            
            if(firstClicked == null)
            {
                firstClicked = this;
                //What here?
            }
            
            else
            {
                //What here?
                firstClicked = null;
            }
        }
danpost danpost

2019/5/31

#
Celesterra wrote...
Ok, I wrote that down but I have no idea where to go from there. How do I detect if a different Number object is clicked?
Actually, with line 3 added like you have it, nothing is needed at line 9. At line 14, there is one important check that must be made before checking the numbers. We do not want to check the numbers before we ensure that the second click was not on the same actor as the first. So, the conditions will be:
if (firstClicked != this && (firstClick.number+number == 10 || firstClicked.number == number))
However, line 3 should be removed and you should set the transparency to exact values where needed. That is, line 9 should have:
getImage().setTransparency(255);
and there should be an else part to the long if statement above to reset the transparency of the first clicked number.
Celesterra Celesterra

2019/5/31

#
It works! Also, one last thing. I don't know if this is possible or not but, is there any way I could make the code only applicable to numbers adjacent to the firstClicked one? Like numbers vertically or horizontally side by side, no diagonals. How could I do that? Here is the working code so far: Number:
public class Number extends Actor
{
    //Variables and Arrays used
    private boolean clicked = false;
    private boolean match = false;
    public int number;
    
    Color orange = new Color(255,161,38);
    Color red = new Color(163,8,3);
    Color green = new Color(8,204,11);
    Color blue = new Color(37,113,234);
    Color[] colors = new Color[] {Color.BLACK, orange, red, green, blue, green, red, orange, Color.BLACK};
    
    private static Number firstClicked;
    
    public Number(int n)
    {
        number = n;
        
        //Creates an image that is 45x45
        GreenfootImage tile = new GreenfootImage(45, 45);
        
        //Sets the font of the image
        Font font = new Font("Courier", true, false, 45);
        tile.setFont(font);
        
        //Frames the tile with a gray square
        tile.setColor(Color.GRAY);
        tile.drawRect(0,0,45,45);
        
        //Displays the number and colors the number (depending on the number)
        tile.setColor(colors[n-1]);
        tile.drawString(""+ n ,11,38);
        
        //Sets the image of the tile
        setImage(tile);
    }
    
    public void act() 
    {
        //If a number is clicked, change the transparency of the image
        if(Greenfoot.mouseClicked(this))
        {
            getImage().setTransparency(355 - getImage().getTransparency());
            clicked = true;
            
            if(firstClicked == null)
            {
                firstClicked = this;
            }
            
            else
            {
                if(firstClicked != this && (firstClicked.number + number == 10 || firstClicked.number == number))
                {
                    getWorld().addObject(new Cover(), firstClicked.getX(), firstClicked.getY());
                    getWorld().addObject(new Cover(), getX(), getY());
                }
                
                else if(firstClicked != this && (firstClicked.number + number != 10 || firstClicked.number != number))
                {
                    firstClicked.getImage().setTransparency(getImage().getTransparency() + 155);
                    getImage().setTransparency(getImage().getTransparency() + 155);
                }
                
                firstClicked = null;
            }
        }
    }
}
And if you are wondering what I am trying to make, here is a link to a version of the game I am trying to make: https://en.pelala.net/numbri/ There is also another kind of match called a head and tail match. It takes the last number from the top row and the first number from a bottom row. How could I possibly make that? EX (bold number is the match): 1 2 3 4 5 6 7 8 9 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8
danpost danpost

2019/5/31

#
You can add another condition in the form of: && getNeighbours(<< range >>, false, Number.class).contains(firstClicked) That last one is a lot more complicated. You probably will have to deal with the differences in the coordinates of their locations.
Celesterra Celesterra

2019/5/31

#
I know where to put the difference code but, how would I write it? Would I write the difference code by subtracting/adding the location of a number from the world's width? Or how else could I do it? I think I know how to detect the height (each number is 45x45, so I would subtract 45 to get the number on a higher row). Please correct me if I am wrong.
danpost danpost

2019/5/31

#
Celesterra wrote...
I know where to put the difference code but, how would I write it? Would I write the difference code by subtracting/adding the location of a number from the world's width? Or how else could I do it?
As I do not know how you are placing the numbers into the world I cannot say exactly what is needed.
I think I know how to detect the height (each number is 45x45, so I would subtract 45 to get the number on a higher row). Please correct me if I am wrong.
The placement of the numbers, again, is what matters.
Celesterra Celesterra

2019/6/2

#
Here is a link to the scenario so far. https://www.greenfoot.org/scenarios/24023
Celesterra Celesterra

2019/6/2

#
Also, how do I make it so when there is a covered number (or more) in between two matchable numbers, I can match them? Ex (C is covered, bold is match): 1 2 3 4 5 6 7 8 9 1 C 1 2 1 3 1 4 1 5 C 6 1 7 1 8
danpost danpost

2019/6/3

#
I had a suggestion before your last post; but that last one ruined its effectiveness. I will have to think about what might be a good way to do all this.
Super_Hippo Super_Hippo

2019/6/3

#
Maybe storing the numbers in a 1D-Array could help. Then a number is also next to another one over line breaks (which is wanted) and they are in the same column when the division with the number of numbers in a row is the same.
danpost danpost

2019/6/3

#
Super_Hippo wrote...
Maybe storing the numbers in a 1D-Array could help. Then a number is also next to another one over line breaks (which is wanted) and they are in the same column when the division with the number of numbers in a row is the same.
That is exactly what I was going to suggest, but even that is troublesome (takes a bit more coding) with the last wanted match type. Q: (@Celesterra) Does the covers in-between match work for around the corners as well (end of one line to beginning of next)? Does the around the corner match work from end of last row to beginning of first row?
There are more replies on the next page.
1
2
3