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

2013/10/13

Making a subclass appear from an action within another subclass

james440763 james440763

2013/10/13

#
I am trying to make a very basic top down game, with a main character and enemies, and I want to be able to have an "attack" button that will make a subclass (The actual attack, in the case it's a ball of fire) appear either to the top, bottom, left or right of my character for the duration of the key being pressed. So I tried getting my main class' coordinates inside of my "attack" class, but I can't seem to do it. Are there any alternatives if what I'm asking is impossible?
Zamoht Zamoht

2013/10/13

#
You should add parameters to the attacks constructor. public Attack(int playerX, int playerY) { //Use the players coordinates to what ever you need to do with them. } Now when you add the attack you should create it like this: new Attack(getX(), getY()) if the attack is added in the player class.
danpost danpost

2013/10/13

#
If you detect the key in the class of the main character, you can pass the main character to the attack class when creating an attack object ( 'getWorld().addObject(new Fireball(this), getX()+xOffset, getY()+yOffset);' )
danpost danpost

2013/10/13

#
Passing the main character will allow a constant check on its coordinates in the world. Why do you need the coordinates of the main character in the attack class? It seems you could just control the fireball from the class of the main character. Add an instance field for a reference to a fireball object. When the key is detected down and the reference is 'null', set the reference to a new fireball object and add it into the world. When the key is detected NOT down and the reference is NOT 'null', remove the referenced fireball from the world and nullify the reference. The main character can also control the location of the fireball if the reference is NOT 'null'.
You need to login to post a reply.