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

2014/9/24

When i shoot my bullet shoots then disapears which makes an error

coder04 coder04

2014/9/24

#
Will somebody help me sort this out please. I need it so it does not shoot when it is at the edge of the world ENEMYLASER code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemylaser here. * */ public class Enemylaser extends Mover { private int life; /** * Act - do whatever the Enemylaser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(20.0); ifAtWorldEdge(); eat(); life--; if (life == 0) { getWorld().removeObject(this); } } public void ifAtWorldEdge() { if (atWorldEdge()) { getWorld().removeObject(this); } } public void eat() { Actor Ship; Ship = getOneObjectAtOffset(0, 0, Ship.class); if (Ship != null) { World world; world = getWorld(); world.removeObject(Ship); } } } ENEMYSHIP code import greenfoot.*; public class Miniship extends Actor { private int timer = 0; public void act() { move(4); fire(); reduceTime(); randomTurn(); turnAtEdge(); } public void fire() { if (timer == 0) { getWorld().addObject(new Enemylaser(), getX(), getY()); Actor laser = getOneObjectAtOffset(0,0,Enemylaser.class); laser.setRotation(getRotation()); timer = 100; Greenfoot.playSound("laser.wav"); } } public void reduceTime() { if (timer > 0) timer--; } /** * With a 10% probability, turn a bit right or left. */ public void randomTurn() { if ( Greenfoot.getRandomNumber(100) < 10 ) { turn( Greenfoot.getRandomNumber(40)-20 ); } } /** * If we reach the edge of the world, turn a little bit. */ public void turnAtEdge() { if (atWorldEdge()) { turn(7); } } public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } } Thanx if you can help
Super_Hippo Super_Hippo

2014/9/24

#
In your 'fire' method, add this line at the beginning:
if (atWorldEdge()) return;
And please use the 'code' tags! It is very easy and will make things much easier.
coder04 coder04

2014/9/24

#
It crashed when the ship shoots and bullet hits the edge of the world
import greenfoot.*; 

public class Miniship extends Actor
{
    private int timer = 0;
    public void act()
    {
        move(4);
        fire();
        reduceTime();
        randomTurn();
        turnAtEdge();
    }
  
  
       public void fire()
    {
        if (atWorldEdge()) return; 
        if (timer == 0)
        {
            getWorld().addObject(new Enemylaser(), getX(), getY());
            Actor laser = getOneObjectAtOffset(0,0,Enemylaser.class);
            laser.setRotation(getRotation());
            timer = 100;
            Greenfoot.playSound("laser.wav");
        }
    }
    
       public void reduceTime()
    {
        if (timer > 0)
            timer--;
    }
    
    /**
     * With a 10% probability, turn a bit right or left.
     */
    public void randomTurn()
    {
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(40)-20 );
        }        
    }
    
    /**
     * If we reach the edge of the world, turn a little bit.
     */
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(7);
        }
    }
    public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    }  
    
} 
Super_Hippo Super_Hippo

2014/9/24

#
What do you mean with "it crashed"? What was the error message? By the way, instead of adding the 'Enemylaser' and get this object with 'getOneObjectAtOffset', you can either do
Enemylaser laser = new Enemylaser();
getWorld().addObject(laser, getX(), getY());
laser.setRotation(getRotation());
Or just
getWorld().addObject(new Enemylaser(getRotation), getX(), getY());
With the last way, you only need to create a constructor in the 'Enemylaser' class like this:
public Enemylaser(int rot)
{
    //only needed if you already have another constructor wich needs to be executed (e.g. for setting the image or something)
    this();
    setRotation(rot);
}
(Thank you for using the 'code' tags!)
coder04 coder04

2014/9/24

#
actor not in world error
Super_Hippo Super_Hippo

2014/9/24

#
Move the 'ifAtWorldEdge()' line from 'Enemylaser's 'act' method to the end of the 'act' method. As an alternative, you can change the method to this:
   public boolean ifAtWorldEdge()  
    {  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
             return true;
       }  
       return false;
    }
Then you can have the following in the 'act' method:
if (ifAtWorldEdge()) return;
The problem right now is, that you remove the object from the world when it hits the edge. After that, you are trying to call the 'eat' method which requires the 'Enemylaser' to be in the world.
coder04 coder04

2014/9/24

#
It worked! Thanx so much!
You need to login to post a reply.