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

2015/4/24

Problems with Neighbours

1
2
Yurey Yurey

2015/4/24

#
hey guys i need 2 use getNeighbours thats the problem i dont know how 2 use this method and what can do with the list i will get.
1
getNeighbours (5, true, Engel.class)
thats the bit of code i have at the moment all i need is the Vektor between the Teufel.class and the Engel.class 4 shooting a projectile thats the next problem can i use projectiles effektiv with a cell size of 10 ?
danpost danpost

2015/4/24

#
First, your terminology needs to be addressed. Classes can in no way be 'neighbors', 'intersectors' or even in a world. Objects created from Actor subclasses (aka: actors, or Actor objects) are what are placed into a world. You can refer to your actors as Teufel objects and Engel objects. By using 'true' in the 'getNeighbours' method call for also including diagonal steps and with a step-count of '5', you are allowing multiple possible directions for your projectiles. That being said, and with you using a cell size greater than one, even if you got the vector, its movement would not look right (it may miss the mark totally or it might seem to 'jump' when shifting sideways between two rows or columns of cells). These problems will not occur if you use 'false' in the 'getNeighbours' method instead of 'true' as there is no sideways shifting or attempt at it. When you use 'getNeighbours', is it possible that more than one Engel object be within the area checked? if so, you would need to determine which one you will fire your projectile at. If not (or if it does not matter which, if more than one), then:
1
2
3
4
5
6
7
8
Actor engel = null;
if (! getNeighbours(5, true, Engel.class).isEmpty())
    engel = (Actor)getNeighbours(5, true, Engel.class).get(0);
if (engel != null)
{
    // determine vector
    // fire projectile
}
would be what you are currently looking for. However, the issues stated above are still in question.
Yurey Yurey

2015/4/24

#
i need 2 search diagonal2 because my player is also able 2 move diagonal and yes there is only 1 instance of the engel class active what is the best option 2 determine this vector ?
Super_Hippo Super_Hippo

2015/4/24

#
First, please stop using numbers as words ('2' for 'to' or '4' for 'for'). You have to search diagonal if you want to shoot diagonal, not if you can walk diagonal. Shooting diagonal with a cell size greater than one is not a great idea as danpost wrote. You don't need a 'vektor', you only need the direction in which the bullet should move. And if you don't want to calculate it, you can also use the 'turnTowards' method. By the way, the 'getNeighbours' method will search in a square (if you write in 'true'), the 'getObjectsInRange' method searches in a circle (approximately). So for example (2, true) will search for these:
  • x x x x x
  • x x x x x
  • x x x x x
  • x x x x x
  • x x x x x
while (2, false) will search like this:
  • o o x o o
  • o x x x o
  • x x x x x
  • o x x x o
  • o o x o o
(bold x is current position, every x is checked)
1
2
3
4
5
6
7
8
for (Object obj :  getObjectsInRange(50, Engel.class) //or getNeighbours, 50 if you change cell size to 1
{
    (Actor) engel = (Actor) obj;
    Bullet b = new Bullet();
    addObject(b, getX(), getY());
    b.turnTowards(engel);
    b.move(2);
}
danpost danpost

2015/4/24

#
Yurey wrote...
i need 2 search diagonal2 because my player is also able 2 move diagonal and yes there is only 1 instance of the engel class active what is the best option 2 determine this vector ?
Once you determine that 'engel' is not 'null', you can use 'engel.getX()' and 'engel.getY()' to get the location coordinates of the Engel object and use them with 'getX()' and 'getY()', the location coordinates of the Teufel object to get the distance (the hypoteneuse of the differences in the corresponding coordinates) and the angle (see the java.lang.Math API documentation for the method to use and how to use it).
Super_Hippo Super_Hippo

2015/4/24

#
Code above should look like this: (Was not fast enough with editing) ;) It could look like this: (Maybe Bullet is 'Kugel' or 'Projektil' in your case)
1
2
3
4
5
6
7
8
for (Object obj :  getObjectsInRange(50, Engel.class) //or getNeighbours, 50 if you change cell size to 1
{
    Actor engel = (Actor) obj;
    Bullet b = new Bullet();
    addObject(b, getX(), getY());
    b.turnTowards(engel);
    b.move(2);
}
danpost danpost

2015/4/24

#
Super_Hippo wrote...
You don't need a 'vektor', you only need the direction in which the bullet should move. And if you don't want to calculate it, you can also use the 'turnTowards' method.
It is true that a 'vektor' is not needed; however the direction may not be enough information for the projectile to reach the target. If the projectiles moves one cell per act, then just by setting their rotation, they will only move along the major axes and diagonals (only 8 possible directions of movement) as other cells will not be reaching on any single movement. You could have the projectile save its original location and give it its target destination and have it count its moves; that way you can start at the original location and move a bit further each time toward the target location.
Yurey Yurey

2015/4/25

#
ok i will stop using numbers for words ^^ excuse me addObject didnt work i get this error report: cannot find symbol - methodaddobject(Teufelfireball,int,int) this is the code of the class:
1
2
3
4
5
6
7
8
9
10
11
public void searchTarget()
   {
      for (Object obj :  getObjectsInRange(5, Engel.class))
      {
         Actor engel = (Actor) obj;
         Teufelfireball b = new Teufelfireball();
         addObject(b, getX(), getY());
         b.turnTowards(engel);
         b.move(2);
      }
   }
danpost danpost

2015/4/25

#
Yurey wrote...
ok i will stop using numbers for words ^^ excuse me addObject didnt work i get this error report: cannot find symbol - methodaddobject(Teufelfireball,int,int)
Line 7 should probably be:
1
getWorld().addObject(b, getX(), getY());
Yurey Yurey

2015/4/25

#
now i have a problem there:
1
b.turnTowards(engel);
error report: method turnTowards in class greenfoot.Actor cannot be applied to given types; Required:int,int; found:greenfoot.Actor; reason: actual and formal argument lists differ in length
Super_Hippo Super_Hippo

2015/4/25

#
Oh right... use engel.getX() and engel.getY() as parameters..
Yurey Yurey

2015/4/25

#
OK now the fireball is there but it dont move ? :/
danpost danpost

2015/4/25

#
The 'b.move(2);' statement in your 'searchTarget' method only initially places it 2 cells forward from where it was added into the world at (I will not presume that this is actually where you want the 'b' object to start from). You need to use the 'act' method of the Teufelfireball class to make it continuously move.
Yurey Yurey

2015/4/25

#
ok now the fireball moves but there are more than just 1 fireball
danpost danpost

2015/4/25

#
Yurey wrote...
ok now the fireball moves but there are more than just 1 fireball
Insert as the first line in the 'searchTarget' method the following line:
1
if (!getWorld().getObjects(Teufelfireball.class).isEmpty()) return;
or alternatively, you can remove the '!' and instead of 'return;', enclose the 'for' loop in new curly brackets for the 'if' block.
There are more replies on the next page.
1
2