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

2017/1/27

Creating different teams with the same actor class.

yungbae yungbae

2017/1/27

#
Hi, I'm making a player versus player game designed for two. Before I had two actors that were functionally the same with different names and art for the red and blue team. I want to merge them into one actor class. How would give each a variable when they are created so that there is always one on each team? And how would I make it so it does not get hit by it's own projectile if they are both using the same projectile class? Thank you!
danpost danpost

2017/1/27

#
You could give the projectile a reference to the actor that creates it:
// instead of
public Projectile()
// use
public Projectile(Player player)
Add a reference field to the Projectile class for the player:
private Player owner;
and set it in the constructor
owner = player;
You can also add a method to get the referenced owner in the Projectile class:
public Player getOwner()
{
    return owner;
}
Now, each player create projectiles as follows:
Projectile projectile = new Projectile(this);
and can check to see if any projectile is theirs or belonging to the other player. For example:
Projectile projectile = (Projectile)getOneIntersectingObject(Projectile.class);
if (projectile != null && projectile.getOwner() != this)
{ // etc.
You need to login to post a reply.