I'm trying to make a forcefield in my game which will destroy incoming Bullets and will stop incoming Enemies from walking.
When the forcefield is spawned, it begins small, grows very fast, and then it's getting smaller again.
Now there are 2 problems with the forcefield's hitbox
- The hitbox is a square.
- The hitbox doesn't grow/get smaller when the image does so.
Here's the code of the forcefield-class:
And the code removing the bullet if it's touching the forcefield:
And if hasToRemove == true, it will remove itself.
I've also tried to do it with the next statement: getObjectsInRange(afmetingen, Bullet.class)
But the problem with this is that the bullets can be send by the Man or an Enemy, and the forcefield should only remove the ones which are coming from an Enemy. So I made the next method in the Bullet class:
And the next method in the Forcefield class:
But I can't call the public method getSender from the Bullet in the Forcefield.
Did I make a mistake or are there other solutions to my problems?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class ForceField extends Actor { private int afmetingen = 1 ; private int maxAfmetingen = 200 ; private int i = 0 ; private boolean scaledUp = false ; protected void addedToWorld() { getImage().scale(maxAfmetingen, maxAfmetingen); } public void act() { imageChanger(); } private void imageChanger() { if (scaledUp == false ) { afmetingen = afmetingen + 10 ; setImage( "Krachtveld2.png" ); getImage().scale(afmetingen, afmetingen); if ( afmetingen > 200 ) { scaledUp = true ; } } if (scaledUp == true ) { if ( i == 10 ) { setImage( "Krachtveld2.png" ); getImage().scale(afmetingen, afmetingen); afmetingen--; i = 0 ; } if ( afmetingen <= 1 ) { getWorld().removeObject( this ); } i++; } } } |
1 2 3 4 | else if (getOneIntersectingObject(ForceField. class ) != null && mySender != "Man" ) { hasToRemove = true ; } |
1 2 3 4 | public String getSender() { return mySender; } |
1 2 3 4 5 6 7 8 9 10 | private void removeIncomingBullets() { if (getObjectsInRange(afmetingen, Bullet. class ) != null ) { if (getObjectsInRange(afmetingen, Bullet. class ).getSender() == "Enemy" ) { getWorld().removeObjects(getObjectsInRange(afmetingen, Bullet. class )); } } } |