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

2014/11/12

Changing world help

bmblevins98 bmblevins98

2014/11/12

#
Im working on a school project now and i cant seem to change the world from world1 to world2, code im using is here. it is my 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
{
     private static final int damage = 16;
     private boolean stillalive = true;
     private int Scaryman2Eaten;
     
    
    /** A bullet looses one life each act, and will disappear when life = 0 */
    private int life = 30;
    private int bulletspeed = 5;
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;
    }
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
    }
    public bullet()
    {
        Scaryman2Eaten = 0;
    }
    public bullet(int speed)
    {
        bulletspeed = speed;
    }
      /**
     * The bullet will damage it if it hits them.
     */
    public void act()
    {
        lookForScaryman2();
        lookForscaryman3();
        if(life <= 0) {
            getWorld().removeObject(this);
        } 
        else {
            life--;
            move(bulletspeed);
            checkScaryman2Hit();
            if (stillalive)
            {
                checkscaryman3Hit();
            }
           
        }
       
        if (Scaryman2Eaten == 3)
        {
            Greenfoot.setWorld(new Town2());
        }
       
    }
    
    /**
     * Check whether we have hit an asteroid.
     */
    private void checkScaryman2Hit()
    {
        Scaryman2 scaryman2 = (Scaryman2) getOneIntersectingObject(Scaryman2.class);
        if (scaryman2 != null){
            getWorld().removeObject(this);
            scaryman2.hit(damage);
            stillalive = false;
        }
    }
    private void checkscaryman3Hit()
    {
        scaryman3 scaryman3 = (scaryman3) getOneIntersectingObject(scaryman3.class);
        if (scaryman3 != null){
            getWorld().removeObject(this);
            scaryman3.hit(damage);
            stillalive = false;
        }
    }
    
    public bullet(Vector speed, int rotation)
    {
        
        setRotation(rotation);
        
        
    }
      public void lookForScaryman2()
    {
        if ( canSee(Scaryman2.class) ) 
        {
            eat(Scaryman2.class);
            Scaryman2Eaten = Scaryman2Eaten + 1;
            
        }
        if (Scaryman2Eaten == 3)
        {
            Greenfoot.setWorld(new Town2());
        }
    }
    public void lookForscaryman3()
    {
        if ( canSee(scaryman3.class) ) 
        {
            eat(scaryman3.class);
            
            
        }
    }
}
n a school project now and i cant seem to change the world from world1 to world2, code im using is
danpost danpost

2014/11/12

#
Unless you are reusing the same bullet object over and over, any value in 'scaryman2Eaten' will be lost when the bullet is removed from the world (each bullet will have its own 'scaryman2Eaten' field that is initialized to zero when the bullet is created). I doubt you would want to reuse the same bullet, so my suggestion is to place the 'scaryman2Eaten' field either with in the class of the actor that shoots the bullets or in your world class. You will then need to be able to access that field. This can be done in several ways, however (1) passing the actor that shoots the bullet to the bullet when created; (2) using the world to find the actor that shoots the bullet, provided only one kind of actor shoots this kind of bullet; or (3) getting the world that contains the field. No matter which object it is in, it must be typed as its particular class (whatever subclass of World or Actor it might be -- at least to the class that the field is located in). For example, if your world is called MyWorld and you placed the field there, then:
( (MyWorld)getWorld() ).scaryman2Eaten++;
With the above line, the 'scaryman2Eaten' field must be public. If it is to remain a private field, then you can add the following method:
public void scaryman2Ate()
{
    scaryman2Eaten++;
    if (scaryman2Eaten == 3) Greenfoot.setWorld(new Town2());
}
and you would call it with this:
( (MyWorld)getWorld() ).scaryman2Ate();
bmblevins98 bmblevins98

2014/11/13

#
Okay, im kind of on your track you set. how would you explain it to a beginner? I put it in my Hero class now and deleted it from my bullet and here is my code now. thanks.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private static final int gunReloadTime = 5;
    private int reloadDelayCount;
    private int shotsfired;
    private int bulletspeed = 7;
    private int Scaryman2Eaten;
    
    
   /**
     * Return the number of shots fired from this Hero.
     */
    public int getShotsfired()
    {
        return shotsfired;
    }
  
    public Hero()
    {
        reloadDelayCount = 5;
        image1 = new GreenfootImage("Hero.png");
        image2 = new GreenfootImage("Hero2.png");
        setImage(image1);
    }
   
    public void act() 
    {
        reloadDelayCount++;
        checkKeyPress();
        if (Scaryman2Eaten == 3)
        {
            Greenfoot.setWorld(new Town2());
        }
        
    }  
    public void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            move(-3);
            setImage(image2);
            bulletspeed = -5;
        }
        if (Greenfoot.isKeyDown("right"))
        {
            move(3);
            setImage(image1);
            bulletspeed = 5;
        }
        if(Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
    }
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            bullet bullet = new bullet (bulletspeed);
            getWorld().addObject (bullet, getX(), getY());
//             bullet.move (bulletspeed);
            reloadDelayCount = 0;
        }
    }
    public void changeImage()
    {
        if ( getImage() == image1 )
        {
            setImage(image1);
        }
        else
        {
            setImage(image2);
        }
    }
danpost danpost

2014/11/13

#
I does not look like you moved everything pertaining to the 'scaryman2Eaten' field into the Hero class (I do not see the 'scaryman2Ate' method in it for one thing). You probably only have one Hero, so you should be able to use this:
((Hero)getWorld().getObjects(Hero.class).get(0)).scareyman2Ate();
in the bullet class when a ScareyMan2 is seen and eaten.
You need to login to post a reply.