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

2019/3/30

setting koordinates problem

plsFast plsFast

2019/3/30

#
i want to move an object to specific koordinates but i dont want to type high numbers like 300 for x and y so i gave them numbers from 1-7
        int XO =this.getX();
        int YO = this.getY();
        int eingX = x;
        int eingY = y;
        int X;
        int Y;
        if(x == 1)
        {
            X = 30;
        }
        if(x == 2)
        {
            X = 150;
        }
        if(x == 3)
        {
            X = 270;
        }
        if(x == 4)
        {
            X = 400;
        }
        if(x == 5)
        {
            X = 520;
        }
        if(x == 6)
        {
            X = 645;
        }
        if(x == 7)
        {
            X = 775;
        }
        
        if(y == 1)
        {
            Y = 30;
        }
        if(y == 2)
        {
            Y = 150;
        }
        if(y == 3)
        {
            Y = 270;
        }
        if(y == 4)
        {
            Y = 400;
        }
        if(y == 5)
        {
            Y = 520;
        }
        if(y == 6)
        {
            Y = 645;
        }
        if(y == 7)
        {
            Y = 775;
        }
        if(x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=6 && x!=7)
        {
            X = XO;
        }
        setLocation(X,Y);
x and y are the int numbers the player has to give (1-7) but it says that X and Y might not have been initialized
CarolK CarolK

2019/3/30

#
There's a case that the X and Y won't be initialized. If X isn't 1,2...,7 and same for Y, then you'll just end up with random numbers and that's why that error appeared. You should first initialize them with a default case, which won't be modified if X and Y aren't 1,2,....7. You have to give them values before you're going for the ifs. Also, I think it might be better if you'd use a switch(). It verifies every case you're interested in and it works like an 'ifs home'. Look, you can try that:
int XO =this.getX();
int YO = this.getY();
int eingX = x;
int eingY = y;
int X;
int Y;
switch(x)
{
	case 1 : X = 30;break;
	case 2 : X = 150;break;
	case 3 : X = 270;break;
	case 4 : X = 400;break;
	case 5 : X = 520;break;
	case 6 : X = 645;break;
	case 7 : X = 775;break;
	default : X=XO;break;
}
switch(y)
{
	case 1 : Y = 30;break;
	case 2 : Y = 150;break;
	case 3 : Y = 270;break;
	case 4 : Y = 400;break;
	case 5 : Y = 520;break;
	case 6 : Y = 645;break;
	case 7 : Y = 775;break;
	default : Y=YO;break;
}

setLocation(X,Y);
The default case will automatically initialize the X and Y. If you want to know more about switch(), you can read here Switch Explained (C++) . Hope it helps :D
plsFast plsFast

2019/3/30

#
thanks man
danpost danpost

2019/3/31

#
This does the same:
int[] coords = { 30, 150, 270, 400, 520, 645, 775 };
int x = (x > 0 && x < 8 ? coords[x-1] : getX());
int y = (y > 0 && y < 8 ? coords[y-1] : getY());
setLocation(x, y);
You need to login to post a reply.