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

2019/3/12

How do I spawn an actor that has a reference to another actor

BlueSnake BlueSnake

2019/3/12

#
How do I spawn an actor that has a reference to another actor the code I have for it is
if(Greenfoot.isKeyDown("space"))
       {if(timeLeft>= 50 && target>=1){
       Object object;
       object = getClass();
       Bullet bullet = (Bullet)object;
       Counter counter = bullet.getShip1();
       getWorld().addObject(bullet, getX(), getY());
       target--;
       timeLeft=0;}}
in the other actor it is set to
Counter counter=null;
public Bullet(Counter counter)
{}
public Counter getShip1()
{return counter}
does anyone know why its not working
danpost danpost

2019/3/12

#
In line 4, you assign a Class type object to a field called object . A class is not a bullet, so you cannot assign object (containing a Class object) to a field that is to retain a Bullet object. Other issues include: * the variable called counter (at line 6) is not used anywhere; so that line is as if not there; * the Bullet constructor Counter parameter is not dealt with in the constructor, so it is as if you passed nothing; I will presume you want to create a new bullet and pass it the Counter object. You will need to show the code of the class that created the Counter object.
You need to login to post a reply.