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

2015/1/11

trying to make bullet dissapear when it hits enemy but cannot find symbol variable getWorld

1
2
danpost danpost

2015/1/11

#
Still waiting on the entire bullet class code.
DesaGrammer DesaGrammer

2015/1/11

#
This I my example hope it helps
Actor x_wing = getOneIntersectingObject(X_wing.class); 
        if(x_wing != null) {
            World myWorld = getWorld();
            myWorld.removeObject(x_wing);
            Space space = (Space)myWorld;
            Counter counter = space.getCounter();
            counter.addScore();
            myWorld.removeObject(this);
        }
        else if(hitTheEdge() !=null) {
            World myWorld = getWorld();
            myWorld.removeObject(this);
        }
danpost danpost

2015/1/11

#
@DesaGrammer, can you post your 'hitTheEdge' method, please.
DesaGrammer DesaGrammer

2015/1/11

#
/* * A method that tells if I hit the edge and what edge i hit */ public String hitTheEdge() { int x = getX(); int y = getY(); World myWorld = getWorld(); int rightSide = myWorld.getWidth() - 1; int bottomSide = myWorld.getHeight() -1; if(y == 0) { return "top"; } else if (x == 0) { return "left"; } else if(x == rightSide) { return "right"; } else if(y == bottomSide) { return "bottom"; } else { return null; } }
DesaGrammer DesaGrammer

2015/1/11

#
   /*
     * A method that tells if I hit the edge and what edge i hit
     */
    public String hitTheEdge() {
        int x = getX();
        int y = getY();
        World myWorld = getWorld();
        int rightSide = myWorld.getWidth() - 1;
        int bottomSide = myWorld.getHeight() -1;
        if(y == 0) {
            return "top";
        }
        else if (x == 0) {
            return "left";
        }
        else if(x == rightSide) {
            return "right";
        }
        else if(y == bottomSide) {
            return "bottom";
        }
        else {
            return null;
        }
    }
danpost danpost

2015/1/11

#
DesaGrammer wrote...
/* * A method that tells if I hit the edge and what edge i hit */ ...
Ok. The name of the method is misleading for what the method does. Maybe 'getEdgeHit' would be more appropriate.
DesaGrammer DesaGrammer

2015/1/11

#
Ok but that's what I learnt and its worked for me but I'll try your way Thanks
khalid11 khalid11

2015/1/12

#
My whole bullet class

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends Actor
{
    int time=10;
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        destroyEnemies ();
        move(5);
        kill();
        if (this.atWorldEdge()) {
            getWorld().removeObject(this);
        }     
    }

    public bullet()
    {
        GreenfootImage image=getImage();
        image.scale(40,6);
        setImage(image);
    }  

    public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level1 level1 = (Level1)myWorld;
            Counter counter = level1.getCounter();
            counter.addScore();
    }
}
    public void kill()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor blueenemy = getOneIntersectingObject(blueenemy.class);
        if(blueenemy != null) 
        {
            getWorld().removeObject(blueenemy);                    
        }
    }

}
danpost danpost

2015/1/12

#
Oh, wait. You removed the line that caused the error, yet you still want to use that line. No wonder I could not find where the problem was (you removed it). Also, for some reason (maybe because you rearranged the act method), the error messages were pointing to a location in your code that you had apparently change or rearranged. It is important to include the problem code when asking for help and not to show an error message that is created from one coding and then showing some other coding. First, put the 'getWorld().removeObject(this);' line back in the 'destroyEnemies' method. Then, clear the terminal, compile the project and run it, firing shots, until the error occurs. Finally, copy/paste the error message and also the act method (if you have altered anything other than adding that one line in the 'destroyEnemies' method, re-post the entire class again).
khalid11 khalid11

2015/1/12

#
terminal
java.lang.ClassCastException: Level2 cannot be cast to Level1
	at bullet.destroyEnemies(bullet.java:52)
	at bullet.act(bullet.java:29)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)

java.lang.ClassCastException: Level2 cannot be cast to Level1
	at rocket.hitenemy(rocket.java:75)
	at rocket.act(rocket.java:45)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
khalid11 khalid11

2015/1/12

#
bullet class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends Actor
{
    int time=10;
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        destroyEnemies ();
        move(5);
        kill();
        if (this.atWorldEdge()) {
            getWorld().removeObject(this);
        }     
    }

    public bullet()
    {
        GreenfootImage image=getImage();
        image.scale(40,6);
        setImage(image);
    }  

    public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level1 level1 = (Level1)myWorld;
            Counter counter = level1.getCounter();
            counter.addScore();
            if (this.atWorldEdge()) {
                getWorld().removeObject(this);
            }  

        }
    }

    public void kill()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor blueenemy = getOneIntersectingObject(blueenemy.class);
        if(blueenemy != null) 
        {
            getWorld().removeObject(blueenemy);                    
        }
    }

}
You need to login to post a reply.
1
2