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

2021/12/24

Trying to remove an Object once its touched 3 times.

tomzz tomzz

2021/12/24

#
public class Shooting extends Rocket { private int life = 3; public void act() { MyWorld ShootingsWorld = (MyWorld)this.getWorld(); //this moves the bullet upwards setLocation(getX(), getY() - 5); if(isTouching(Enemy.class)){ life--; if(life==0){ ShootingsWorld.removeEnemy((Enemy)getOneIntersectingObject(Enemy.class)); life = 3; } } } } my code looks like this. But for some reason it instantly removes the object on one touch?! and the life count decreases by a lot. Any solutions?
tomzz tomzz

2021/12/25

#
The issue here is that when it touches the object it doesn't decrement by a consistent value.?
danpost danpost

2021/12/25

#
The problem is that because the Enemy actors are not being removed when hit by the bullet, the bullet continues to touch the enemy for multiple act steps. To avoid this, you will need either a boolean field to indicate when touching an enemy or a reference field to retain which enemy is currently being touched. Then use the field to determine if life should decrease or not. The field, of whichever type, must be maintained to the last known state and checked with the current state to detect a change in the state.
danpost danpost

2021/12/25

#
tomzz wrote...
public class Shooting extends Rocket
A Shooting object is NOT of type Rocket. Its class should not extend the Rocket class.
danpost danpost

2021/12/25

#
From what I can tell, your Shooting object is to kill every third enemy touched. Is that really what you want? You should explain what should happen each time it touches an enemy, when an enemy should be removed and when the Shooting actor is to be removed (if at all).
tomzz tomzz

2021/12/25

#
My Idea was to remove the object being touched when its touched 3 times. So if the enemy is shot 3 times it should be removed. But ya I get it now the issue was as the bullet passes through the act method runs again and initiates the isTouching method, to avoid that I added a check for it to only be initiated once and made the life a static variable. But doing that arose more issues as the static variable belongs to the class and not an object. So if I were to do multiple objects, they are all governed by a single variable. What should I do know? I made the life static because each bullet is its own object, so making the life static makes sense. public class Shooting extends Actor { //this is the enmies life count private static int life = 3; public boolean enemyShot = false; public void act() { MyWorld ShootingsWorld = (MyWorld)this.getWorld(); //this moves the bullet upwards setLocation(getX(), getY() - 5); if(isTouching(Enemy.class)){ if(!enemyShot){ life--; if(life<=0){ ShootingsWorld.removeEnemy((Enemy)getOneIntersectingObject(Enemy.class)); life = 3; } enemyShot = true; } } } public String showLife(int life){ return String.format("Life:%d",life); } }
danpost danpost

2021/12/25

#
So, basically, you want each enemy to have a "health" value of 3 and for it to decrease each time it is hit by a bullet. It would then make sense to have the field as non-static in the class of the enemy. It would also, then, make sense to have the collision code between enemies and bullet in the class of the enemies also (not in the class of the bullet). To further clarify, as far as the bullets are concerned -- are they to pass through enemies and possibly hit multiple enemies as they continues through them?
tomzz tomzz

2021/12/25

#
No, as soon as the bullets hit the enemy. The bullets should instantly disappear. Here is my code, for now the removal of the enemy and the bullet works perfectly but I am limited to one enemy at a time. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; public class Enemy extends Actor { private int counter; private boolean movingDirection = false; private int firstPosition; private Shooting shooter = null; private boolean enemySet = false; public void setshooter(Shooting other){ enemySet = true; shooter = other; } public void act() { MyWorld EnemysWorld = (MyWorld)this.getWorld(); getWorld().showText(null,getX(), getY()+20); if(counter == 0 || movingDirection == true){ firstPosition = getX(); counter++; } movingDirection = EnemyMove(firstPosition, getX(),movingDirection); if(isTouching(Shooting.class)){ EnemysWorld.removeBullets((Shooting)getOneIntersectingObject(Shooting.class)); } if(enemySet) getWorld().showText(shooter.showLife(shooter.getLife()), getX(), getY()+20); } public boolean EnemyMove(int firstposition,int position2,boolean movingDirection){ if(firstposition >= 590 || !movingDirection){ firstposition = getX() * -1; } if(firstposition < 0 && position2 > 10){ move(-1); } else if(position2 < 590){ move(1); return true; } return false; } import greenfoot.*; import java.util.ArrayList; public class Shooting extends Actor { //this is the enemies life count private static int life = 3; public boolean enemyShot = false; private Enemy enemy; private int EnemiesX; private int EnemiesY; public static int scoreCounter=0; public Shooting(Enemy enemy){ this.enemy = enemy; } public void act() { MyWorld ShootingsWorld = (MyWorld)this.getWorld(); getWorld().showText(null,10,10); //this moves the bullet upwards setLocation(getX(), getY() - 5); if(isTouching(Enemy.class)){ if(!enemyShot){ life--; if(life == 0){ EnemiesX = enemy.getX(); EnemiesY = enemy.getY(); getWorld().showText(null,EnemiesX, EnemiesY+20); } if(life<=0){ ShootingsWorld.removeEnemy((Enemy)getOneIntersectingObject(Enemy.class)); scoreCounter++; life = 3; } enemyShot = true; } } getWorld().showText("Score: " +scoreCounter,50,10); } public String showLife(int life){ return String.format("Life:%d",life); } public int getLife(){ return life; } SO this code works perfectly, but how can I change it so that I can have multiple enemies at a time?
danpost danpost

2021/12/25

#
danpost wrote...
So, basically, you want each enemy to have a "health" value of 3 and for it to decrease each time it is hit by a bullet. It would then make sense to have the field as non-static in the class of the enemy. It would also, then, make sense to have the collision code between enemies and bullet in the class of the enemies also (not in the class of the bullet).
Enemy actors have life, not bullets. Move the field and collision codes (which would also remove bullets) to the Enemy class. It would simply be:
if (isTouching(Shooting.class))
{
    removeTouching(Shooting.class);
    life--;
    if (life == 0) getWorld().removeObject(this);
}
tomzz tomzz

2021/12/25

#
This works perfectly!! Thank you! Makes sense :)
You need to login to post a reply.