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

2024/1/25

I need help making a bullet of some kind.URGENT!

da3m0nium da3m0nium

2024/1/25

#
I'm working on a game that has a dolphin that needs to find fish to eat and danger to avoid with the help of his sonar and I'm new on all Greenfoot related stuff, I started working a week ago, so I'm having some big problem with this and I have to finish the whole game in a week, so please help!! This is what I've done so far, and it's in the dolphin's code class:
int r=100;
    Actor sonar = new sonar();
    public void shoot(){
        if(Greenfoot.isKeyDown("space")) {  
            if(r==100){
                r--;
                getWorld().addObject(sonar, getX(), getY()); 
                sonar.move(5);
                if(sonar.getX()==400){
                getWorld().removeObjects(getWorld().getObjectsAt(sonar.getX(), sonar.getY(), sonar.class));
                }
            }
        }
        if(r==0){
                r=100;
            }
    }
Kyuubi Kyuubi

2024/1/26

#
i don't get the whole code, but a big problem is this part
if(Greenfoot.isKeyDown("space")) {  
            if(r==100){
                r--;
                getWorld().addObject(sonar, getX(), getY()); 
                sonar.move(5);
                if(sonar.getX()==400){
                getWorld().removeObjects(getWorld().getObjectsAt(sonar.getX(), sonar.getY(), sonar.class));
                }
            }
        }
the decrementation of r only happens once, not reaching 0 again, i don't quite understand the code, but try something like this
if(Greenfoot.isKeyDown("space")) {  
           while(r==100){
                r--;
                getWorld().addObject(sonar, getX(), getY()); 
                sonar.move(5);
                if(sonar.getX()==400){
                getWorld().removeObjects(getWorld().getObjectsAt(sonar.getX(), sonar.getY(), sonar.class));
                }
            }
        }
bafta la cod
danpost danpost

2024/1/26

#
da3m0nium wrote...
I'm working on a game that has a dolphin that needs to find fish to eat and danger to avoid with the help of his sonar and I'm new on all Greenfoot related stuff, I started working a week ago, so I'm having some big problem with this and I have to finish the whole game in a week, so please help!! This is what I've done so far, and it's in the dolphin's code class: << Code Omitted >>
A while loop, as Kyuubi suggests using, will not work as the loop completes in a single act step. The sonar will not be in the world for even one act step (it is added and removed during the same act step). You should probably have the movement of the sonar, plus its removal code, in the sonar class itself (not in the dolphin class).. The field for the sonar can remain in the dolphin class with the following in its action code:
if (sonar.getWorld() == null && Greenfoot.isKeyDown("space")) {
    getWorld().addObject(sonar, getX(), getY());
}
da3m0nium da3m0nium

2024/1/26

#
Thanks guys so much!!
You need to login to post a reply.