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

2014/4/26

Using Y position values from other classes

qwakery qwakery

2014/4/26

#
Hi, I'm relatively new to Greenfoot and java so this may be very simple to fix. I'm trying to compare the Y values of different classes so that if a player one counter lands on the same 'ladder space' as a player two counter, the player two counter moves back to the bottom 'rung'. When I tried using normal dot notation, it came up with an error about how the non-static method getY() could not be referenced from a static context. How do I solve my problem?
Super_Hippo Super_Hippo

2014/4/26

#
You can only call getY() with an object, since there could be more than one object of this class in your scenario. A static variable is always the same for all objects of this class. If it is not static, then every object of this class can have a different value in this variable. If your player 1 and player 2 are two different classes which only appear once in your game, then you could just hold the y-value as a "public static int" in both classes and compare this int. Otherwise you have to get a reference to the object which you want to compare the y value with and not to the class. To do this, you can hold a reference of your players in the world class and get the right reference through this way around. As I don't know your scenario, it also could be possible to get the desired object with something like "getOneObjectAtOffset(...)" or similar.
qwakery qwakery

2014/4/27

#
There are 2 objects of each player counter class. Another rule is that you cant move your counter onto the rung with your other counter on - how would I make it so that it knows what rung the counter is going to jump to with the current dice roll, and switches my boolean method canMove to false? Thanks
danpost danpost

2014/4/27

#
Try something like this (in pseudo-code):
1
2
3
4
5
6
7
8
9
10
11
12
move(die_count);
if (intersects_counter)
{
    if (counter_owner is this_owner)
    {
        move(-(die_count));
    }
    else
    {
        counter.setLocation(bottom_rung_location);
    }
}
I will presume that the class of your counter has a field in it that makes one counter distinguishable from another as far as whether it is a player one counter or a player two counter.
Super_Hippo Super_Hippo

2014/4/27

#
Oh, well, interesting, that the word 'counter' has so many meanings. I thought it would be something that counts number or whatever and was confused... I have to understand your scenario. There are two players with each two 'counters' (=tokens=gaming pieces, if I got that right, now). The players roll a die (or more dice) and climb a ladder. If a counter of one player moves to a rung where the other player is, he has to go down until he is on a free rung. Counters can't land on the ladder of the other counter of the same team. Did I get that right? So, a question: Is there one ladder where all climb or are there two ladders, one for each? Probably you can use just one class for all counters, if there isn't something special that forbids that, but I don't think so.
danpost danpost

2014/4/27

#
@Super_Hippo, from what he wrote -- 'There are 2 objects of each player counter class.' -- I fear that my presumption was incorrect and that you last statement should not be taken lightly.
qwakery qwakery

2014/4/27

#
Sorry, it's counter as in movable piece :/ There is one ladder, and if a player 1 counter lands on the same rung as a player 2 counter, the player 2 counter falls to the bottom of the ladder. I made 2 counter classes (Player 1 and player 2) because they're different colours and that's how Ive been taught.
danpost danpost

2014/4/27

#
You can have two classes, one for each player; but, you should probably have them both extend an intermediate class before Actor. For example, 'Player1 extends Player' and 'Player2 extends Player'. Then, your Player class can maintain the bulk of the code for both players and you can check for a Player object, instead of each one trying to look for the other and one of its own.
Super_Hippo Super_Hippo

2014/4/27

#
Actually, it is useless to use them as two classes since you can decide in the constructor, which color it should be and in which team he is. So something like
1
2
3
4
5
6
7
8
public boolean team;
 
public Counter(boolean t)
{
    team = t;
    if (team) /* use player 1 picture */
    else /* use player 2 picture */
}
Or use int instead of boolean if you may want to use more teams later. Add a Counter to the world like this then.
1
addObject(new Counter(true /* or false*/), x, y);
To jump, you can orientate on this code. (not tested, could be some typos in it)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int y = distance_between_each_ladder * number_of_ladders_to_walk;
   Counter counter = (Counter) getOneObjectAtOffset(0,-y,Counter.class);
   if (counter != null) //If there is another counter on the desired rung
   {
       if (counter.team != team) // If the counter is from the other player
       {
           setLocation(getX(), getY()-y); //Climb up to the desired rung
           counter.setLocation(counter.getX(), counter.getY() + distance_between_each_ladder); //Move the other counter one rung down
       }
       else
       {
           //Do nothing, since the desired rung is blocked by an own counter
       }
   }
   else
   {
       setLocation(getX(), getY()-y); //Climb up to the desired rung
   }
If you really want to use two classes, then you have to adjust it a little for both, but it is quite useless to use two.
You need to login to post a reply.