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

2019/7/11

Static context

Fargesia Fargesia

2019/7/11

#
Hi guys! I've been having troubles with this static context thing, I declared a two dimensional matrix in my world code :
int plateau [][] = { { 8, 9, 10, 12, 11, 10, 9, 8 }, 
                               { 7, 7, 7, 7, 7, 7, 7, 7 }, 
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 1, 1, 1, 1, 1, 1, 1, 1 },
                               { 2, 3, 4, 6, 5, 4, 3, 2 } } ;
And in an actor's code I want to change some values (the matrix is a representation of the board, and the actor ,who's a piece, has to change the values as he moved) :
Plateau.plateau[plateaux.convertir(vieuxY)][plateaux.convertir(vieuxX)]=0;
            Plateau.plateau[plateaux.convertir(getY())][plateaux.convertir(getX())]=3;
But it says I cannot change from a static context. How do I do this? Adding static before declaring the matrix sure works but when I reset the values won't change back. Wich is annoying because then it doesn't represent the board at all... How do I do this? Thanks guys
Proprogrammer04 Proprogrammer04

2019/7/11

#
write static in front of int:
static int plateau [][] = { { 8, 9, 10, 12, 11, 10, 9, 8 }, 
                               { 7, 7, 7, 7, 7, 7, 7, 7 }, 
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 0, 0, 0, 0, 0, 0, 0, 0 },
                               { 1, 1, 1, 1, 1, 1, 1, 1 },
                               { 2, 3, 4, 6, 5, 4, 3, 2 } } ;
Proprogrammer04 Proprogrammer04

2019/7/11

#
That should help
danpost danpost

2019/7/11

#
You can make it static as long as you assign the array values in your world constructor:
public class Plateau extends World
{
    static int[][] plateau;
    
    public Plateau()
    {
        super(8, 8, 75);
        plateau = new int[][] {
            {  8,  9, 10, 12, 11, 10,  9,  8 }, 
            {  7,  7,  7,  7,  7,  7,  7,  7 }, 
            {  0,  0,  0,  0,  0,  0,  0,  0 },
            {  0,  0,  0,  0,  0,  0,  0,  0 },
            {  0,  0,  0,  0,  0,  0,  0,  0 },
            {  0,  0,  0,  0,  0,  0,  0,  0 },
            {  1,  1,  1,  1,  1,  1,  1,  1 },
            {  2,  3,  4,  6,  5,  4,  3,  2 }
         } ;
        // etc.
You need to login to post a reply.