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

2017/11/17

A 'Shot' spawns at my mouses position, even though I have not implied it into my code.

ElAdriano807 ElAdriano807

2017/11/17

#
In my Tower Defense Game I have the Turret 'T1' and my Shot 'Shot'. When my Turret starts shooting there are randomly some shots coming from my mouses position but only when I move it. Do you have any idea why this might happen. Here are the code samples for the Turret:
public void shoot() {
        List<E1>enemies = getObjectsInRange(200, E1.class);
        E1 enemy;
        int test = 0;
        int target = 0;
        if(enemies.size() > 0 && placed && t % period == 0) {
            for(int i = 0; i < enemies.size(); i++) {
                enemy = enemies.get(i);
                if(enemy.getDeltaX() > test) {
                    test = enemy.getDeltaX();
                    target = i;
                }
            }
            enemy = enemies.get(target);
            turnTowards(enemy.getX(), enemy.getY());
            getWorld().addObject(new Shot(enemy), getX(), getY());
            
            //shot, shot speed, damage type (AOI), damage
        }
    }
and for my Shot:
import greenfoot.*;

public class Shot extends T1 {
    Actor enemy;
    public Shot(Actor target) {
        enemy = target;
    }
    public void act() {
        move(5);
        if(enemy.getWorld() != null) {
            turnTowards(enemy.getX(), enemy.getY());
        }
        if(intersects(enemy)) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
        }
    }    
}
P.S. I started the project in my school (they have an older version there) and it worked just fine but when I came home it started doing this. Thank you in advance :) ElAdriano
Nosson1459 Nosson1459

2017/11/17

#
i dont have an answer for u but im just curious as 2 y u use a loop
ElAdriano807 ElAdriano807

2017/11/17

#
I use the for-loop to get the Enemy with the highest 'DeltaX', so that my turret targets the Enemy that is closest to the 'Finish line'.
Nosson1459 Nosson1459

2017/11/17

#
does the problem occur randomly or at specific times like wen theres no enemy in the world?
Nosson1459 Nosson1459

2017/11/17

#
it would help of u posted the entire code for the classes
ElAdriano807 ElAdriano807

2017/11/17

#
The problem seems to occur random but with a high frequenzy(it feels like every second shot is spawned at the mouses position and sometimes they move away top hit the enemy). Here is the whole code for 'Turret':
import greenfoot.*;
import java.util.*;

public class T1 extends Turrets {
    int range = 200;
    int price = 450;
    int period = 100;
    
    int t = 0;
    boolean placed = false;

    public void act() {
        getPlaced();
        shoot();
        t++;
    }

    public boolean getPlaced() {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(!placed && Greenfoot.mouseMoved(null)) {
            setLocation(mouse.getX(), mouse.getY());
        }
        if(Greenfoot.mouseClicked(null) && !placed) {
            goIntoGrid();
            if(!isTouching(Way.class) && getX() < 1250 && getY() > 30 && !isTouching(T1.class)  && money > price) {
                placed = true;
            }
        }
        return placed;
    }
    
    public int getPrice() {
        return price;
    }
    
    public void goIntoGrid() {
        for(int i = 0; i < 25; i++) {
            if((getX()+25)%50<25) {
                setLocation(getX()-1,getY());
            }
            if((getX()+25)%50>24) {
                setLocation(getX()+1,getY());
            }
            if((getY()-55)%50<25) {
                setLocation(getX(),getY()-1);
            }
            if((getY()-55)%50>24) {
                setLocation(getX(),getY()+1);
            }
        }
    }

    public void shoot() {
        List<E1>enemies = getObjectsInRange(200, E1.class);
        E1 enemy;
        int test = 0;
        int target = 0;
        if(enemies.size() > 0 && placed && t % period == 0) {
            for(int i = 0; i < enemies.size(); i++) {
                enemy = enemies.get(i);
                if(enemy.getDeltaX() > test) {
                    test = enemy.getDeltaX();
                    target = i;
                }
            }
            enemy = enemies.get(target);
            turnTowards(enemy.getX(), enemy.getY());
            getWorld().addObject(new Shot(enemy), getX(), getY());
            
            //shot, shot speed, damage type (AOI), damage
        }
    }
}
For explaination: When I click on the corrosponding shop icon, I spawn this Actor and then when clicked again on the right ground(not the path or the shop) it stays where it is, looks for its target and then shoots. If needed I can upload the project so you can take a look at it.
danpost danpost

2017/11/17

#
Do you have any actors following the mouse? if so, what code do you have in its class? if not, what code do you have in your World subclass? Uploading it would certainly make it easier to find the cause.
ElAdriano807 ElAdriano807

2017/11/17

#
1. Except the Turret that is following the mouse before it is 'placed' I dont have an object following the mouse. 2. I have uploaded the project, so you should be able to see it on my profile.
ElAdriano807 ElAdriano807

2017/11/17

#
What I have found out is that, if I place the Turret on the map from myWorld and set 'placed' = true from the beginning on, the problem doesn't seem to occur.
danpost danpost

2017/11/17

#
Your issue (plus other issues you will have) come from your misunderstanding of what a subclass is for. Think of the Class tree as a Life tree, where it branches into the Plant and Animal kingdoms; then each branch into their different phylum, class, order, family, genus and species. Each subclass represents an entity of its superclass, but with more specificity. You would not find a bacterium under Human no more than you would subclass Turrets with Shot. They are two totally different things. An Enemy is not a Spawner. A Spawner may produce an Enemy, but, again, they are two different things. Notice how MyWorld is a specific type of the more generalized World and Spawner is a specific type of Actor -- that is proper subclassing. Fix your Class tree, first and foremost. After that, if you run into issues, you can re-upload the project and we can deal with them then.
ElAdriano807 ElAdriano807

2017/11/17

#
Wow, it seems to work perfectly now. No more shots coming from my mouse. Thank you very much for your help, Danpost :) It really has been that simple. I wouldn't ever have found out myself since we never learned something about the class tree in school. Thanks again, you saved my weekend :)
You need to login to post a reply.