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

2018/8/12

Actor get position from another actor

RevoltecX7 RevoltecX7

2018/8/12

#
Hi, I would like to ask you guys. When I have 2 actors(Player -for this situation it will don't move AND Enemy - it will go forward to Player). So my question is, how can I get x/y positions(in Enemy class) of Player position? I want to make Enemy moving to the player. Thank you for help me. :)
RevoltecX7 RevoltecX7

2018/8/12

#
And second question When I have "2" actors (Player - he will be in contact with enemy AND Enemies(2 and more) - what they will come to Player)... How to check IF Player is in contact with any of the enemies. I can do it with a lot of ifs. But is there any simple way for array/list? Or something?
danpost danpost

2018/8/12

#
(1) you can use code similar to the following:
List players = getWorld().getObjects(Player.class);
if (!players.isEmpty())
{
    Actor player = (Actor)players.get(0);
    int playerX = player.getX();
    int playerY = player.getY();
    // etc.
}
(2) It is hard to determine what code to use and where to use it without more info. Would need context -- full purpose for need of list of touching enemies (exactly what you intend to do with each enemy that might be in the list).
RevoltecX7 RevoltecX7

2018/8/12

#
(1) What that mean in the first line "List"? I don't get it. Also I have only one player and one monster (at the moment). (2)Okay, so when you can do it with list - how it will looks like? When I will create new project - 1 Player(Player) and 2 Enemies(Enemy1, Enemy2). How it will looks like when I will put the enemies to list/array and then match contact between this classes? PS: In (2) - I know how to make it like - in every enemy put condition that will match if it touch the player. But there is any other ways how to do that?
danpost danpost

2018/8/12

#
RevoltecX7 wrote...
(1) What that mean in the first line "List"? I don't get it. Also I have only one player and one monster (at the moment).
The World instance method, getObjects(Class clss), returns a List object containing all clss type objects in the world (or an empty list, if no objects of type clss are in the world). So, the first line creates a variable called players which is of type List and the aforementioned list is assigned to that variable.
(2)Okay, so when you can do it with list - how it will looks like? When I will create new project - 1 Player(Player) and 2 Enemies(Enemy1, Enemy2). How it will looks like when I will put the enemies to list/array and then match contact between this classes? PS: In (2) - I know how to make it like - in every enemy put condition that will match if it touch the player. But there is any other ways how to do that?
There are almost always more than one way to do something. Sometimes there are no shortcuts, however. I am not saying there is no easier way in your case. You were too vague with your last response. Unfortunately, I will not be able to respond quickly to any more replies. Need to head back to house with no internet service now.
Super_Hippo Super_Hippo

2018/8/12

#
Maybe you could also check it in the Player class.
if (isTouching(Enemy.class))
Then you have a superclass "Enemy" that all Enemy classes extend (if you really need more than one).
RevoltecX7 RevoltecX7

2018/8/12

#

danpost:

(1) I am asking, because "List" notice me this class doesn't exit. (U know, I don't need any list of something, there are only 2 classes). Also when I change "List" to "Player" or "EnemyXX" (XX-numbers 01/02/etc).. it writes me more errors. But I realize there is a way - create public functions for getX getY in Player.class and then call it in EnemyXX.class (2)Nah, okay. I will try it with that my "old" way. :D

Super_Hippo:

U mean, I should try all Enemies put in subclass of class Enemy.class? mmm. I will try it.
Super_Hippo Super_Hippo

2018/8/12

#
For the List thing, you either need to import the List class
import java.util.List;
Or you could use this instead:
if (!getWorld().getObjects(Player.class).isEmpty())
RevoltecX7 RevoltecX7

2018/8/12

#
Okay, I got it. Thanks! :) It's now working all what I want to do. Thanks for helps!
You need to login to post a reply.