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

2012/6/6

Really need help

crazyjack12 crazyjack12

2012/6/6

#
In setLocation, i am trying to use an array as the "+" or "-" operator to change the positioning of an object, the operator chosen will be decided by the int which will act as the array's index number, the int xcord or ycord, is changed when the object is intercepting another object, which i have already programmed, however i cannot get greenfoot to recognise the array as an actual operator instead of an int value to use to change the location. if this does make any sense to you please reply quickly!! public void omg() { x = getX(); y = getY(); String Direction; Direction = new String; Direction = "+"; Direction = "-"; setLocation (x (Direction) 2, y (Direction) 2); <-- }
crazyjack12 crazyjack12

2012/6/6

#
does anyone even get what i am trying to do? :/
MatheMagician MatheMagician

2012/6/6

#
I see what you are trying to do, however, you are saving + and - as strings, and therefore java only recognizes them as strings, not mathematical operators. You could use an int array and instead use -1 instead of - and 1 instead of +. You could then multiple 2 times that number and add it to your x location. The altered code would look like this.
public void omg()
{
        x = getX();
        y = getY();
        int[] Direction = new int[2];
        Direction[0] = 1;
        Direction[1] = -1;
        setLocation (x  +(Direction[xcord])*2,  y+ (Direction[ycord])*2); <--
}
crazyjack12 crazyjack12

2012/6/6

#
ahhh, that's done it thank you MatheMagician :D
MatheMagician MatheMagician

2012/6/10

#
You are welcome.
danpost danpost

2012/6/10

#
The following makes it almost not worth it to even call a method.
public void omg()
{
    setLocation(getX() + (1 - 2 * xcord), getY() + (1 - 2 * ycord));
}
is a simplified version (no need for any array). BTW, I heard that 'omg' has a new meaning on Facebook and such: 'Obama must go' (go figure!).
MatheMagician MatheMagician

2012/6/11

#
I noticed that, but I assumed crazyjack12 might have made an array so that he would have more choices or something. But yes that is much simpler.
You need to login to post a reply.