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

2014/3/31

Wrong and Right code

1
2
SullyFish SullyFish

2014/3/31

#
working on a project where Im trying to get a random generator for two types of chests the wrong chests and the right one. I need 9 chests to open but 8 have to be wrong and only one can be right. I dont know if I need two different actors and I have no clue what the code is. Please help!
danpost danpost

2014/3/31

#
Just add a boolean field to the Chest class and call it 'specialChest' or 'rightChest' or just 'special'. Add a method to the Chest class to set the value of the field:
public void setSpecial(boolean specialState)
{
    special = specialState;
}
// you might also need this later
public boolean isSpecial()
{
    return special;
}
After creating all the chests in the world class, if you want to randomly have one picked to be special, use:
((Chest)getObjects(Chest.class).get(Greenfoot.getRandomNumber(getObjects(Chest.class).size()))).setSpecial(true);
SullyFish SullyFish

2014/4/1

#
this is my current code, how would I change it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Chest here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chest extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private boolean special;
    
    public Chest()
     {  
        }
    public void act() 
    {
       checkOpen(); // Add your action code here.
    }   
    public void checkOpen()
    {
        Actor a = getOneIntersectingObject(Lime.class);
        if (a != null) 
        if (Greenfoot.getRandomNumber(100) < 17) {
            Sandbox sandbox = (Sandbox) getWorld();
            sandbox.addObject(new ChestLime(), getX(), getY());
            getWorld().removeObject(this);
            Greenfoot.playSound("ZeldaChest.wav");
            Greenfoot.stop();
            }   
          
        else  {Sandbox sandbox = (Sandbox) getWorld();
            sandbox.addObject(new ChestOpen(), getX(), getY());
            getWorld().removeObject(this); }
        }
    }
danpost danpost

2014/4/1

#
What are the two images for? and why is there a 17 percent change of new ChestLine instead of ChestOpen being created? By understanding what you are trying to do, I can help you better. Also, I do not see the methods I included in my last post in this class :/
SullyFish SullyFish

2014/4/2

#
This was my code before the code you gave me and Im wondering what I would change. It's at 17% because that was the only way I could figure out a decent ratio to spawn in the right chest. The 2 images are meaningless now. And I have two actors for rightchest and wrongchest and Im wondering what changes I have to make to use this code properly!
danpost danpost

2014/4/2

#
I would stick with one Chest class using two images (one for opened; one for closed). Boolean fields can be used for both open/close state and special/non-special state.
SullyFish SullyFish

2014/4/3

#
I'm really sorry Im fairly confused where do I put in the
((Chest)getObjects(Chest.class).get(Greenfoot.getRandomNumber(getObjects(Chest.class).size()))).setSpecial(true);
This is what Im doing right now and it doesnt make much sense to me
import greenfoot.*; 
public class Chest extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private boolean special;
    public void setSpecial(boolean specialState)  
        {  
            special = specialState;  
        }  
    public boolean isSpecial()  
        {  
        return special;  
        } 
    public Chest()
     {  
         image1 = new GreenfootImage("chestX.png");
         image2 = new GreenfootImage("chestLimes.png");
        }
    public void act() 
    {
        checkOpen();
    }   
    public void checkOpen()
    {
        ((Chest)getObjects(Chest.class).get(Greenfoot.getRandomNumber(getObjects(Chest.class).size()))).setSpecial(true);

        }

    }

danpost danpost

2014/4/3

#
I will go ahead and presume that 'image1' and 'image2' are the images of the two kinds of opened chests
import greenfoot.*;  // makes the greenfoot package available to the class

public class Chest extends Actor // declares the class as subclass of Actor
{
    // the image fields can be set when declared
    private  GreenfootImage image1 = new GreenfootImage("chestX,png");
    private  GreenfootImage image2 = new GreenfootImage("chestLimes.png");
    private boolean special; // default/initial value is 'false'

    // used to set one chest as special -- called from the world class after chests are created
    public void setSpecial(boolean specialState)  
    {  
        special = specialState;  
    }  

    // used to return the special status of a chest
    public boolean isSpecial()  
    {  
        return special;  
    } 

    // constructor for chest objects (empty)
    public Chest()
    {
    }

    // used to check if chest needs to be opened
    public void act() 
    {
        checkOpen();
    }

    // opens chest if needed
    public void checkOpen()
    {
        // if not intersecting player, return
        // open chest
        // if special, do special else do not-so-special
    }
}
// the following line should be in your world class constructor after you create all the chests
((Chest)getObjects(Chest.class).get(Greenfoot.getRandomNumber(getObjects(Chest.class).size()))).setSpecial(true);
SullyFish SullyFish

2014/4/4

#
Thank you so much! It works great! And one last question then Ill stop being so needy, I'm using a counter script right now for every time you touch a chest but it counts infinitely and I was wondering how I could make it so that it only counts by one for each individual chest you touch this is my counter code
     public void act()
    {
        checkKeypress();
        
            {    Actor open;
        open = getOneObjectAtOffset(0,0, Chest.class);
        if (open != null)
        {counter++;
         System.out.println(counter);

    }   
    }
    }
SullyFish SullyFish

2014/4/4

#
this is in the character you control just to be clear
danpost danpost

2014/4/4

#
Line 6 in your last post (in the 'act' method) only checks to see if a chest is there. It also needs to ensure that the chest is not opened yet. If you need help with that, you will need to post your Chest class code as it currently is.
SullyFish SullyFish

2014/4/14

#
import greenfoot.*;  // makes the greenfoot package available to the class  
  
public class Chest extends Actor // declares the class as subclass of Actor  
{  
    // the image fields can be set when declared  
    private  GreenfootImage image1 = new GreenfootImage("chestX.png");  
    private  GreenfootImage image2 = new GreenfootImage("chestLimes.png");  
    private boolean special; 
    private int counter;
    Actor ppen;// default/initial value is 'false' 
    
  
    // used to set one chest as special -- called from the world class after chests are created  
    public void setSpecial(boolean specialState)    
    {    
        special = specialState;    
    }    
  
    // used to return the special status of a chest  
    public boolean isSpecial()    
    {    
        return special;    
    }   
  
    // constructor for chest objects (empty)  
    public Chest()  
    {  
    }  
  
    // used to check if chest needs to be opened  
    public void act()   
    {  
        checkOpen();  
    }  
  
    // opens chest if needed  
    public void checkOpen()  
    {  ppen = getOneObjectAtOffset(0,0, Lime.class);
        if (ppen != null){
            if (isSpecial())
            {
            setImage(image2);
            Greenfoot.playSound("ZeldaChest.wav");
            Greenfoot.stop();

            } 
            else {
                setImage(image1);
                counter++;
                System.out.println(counter);
                Greenfoot.delay(5);
                if (counter == 10){
                    Greenfoot.stop();
            }
        
        // if not intersecting player, return  
        // open chest  
        // if special, do special else do not-so-special  
    } 
   }
} 
}
SullyFish SullyFish

2014/4/24

#
:(
danpost danpost

2014/4/24

#
This should do it -- change your 'checkOpen' method to this:
public void checkOpen()  
{
    ppen = getOneObjectAtOffset(0,0, Lime.class);
    if (ppen != null)
    {
        if (isSpecial() && getImage() != image2)
        {
            setImage(image2);
            Greenfoot.playSound("ZeldaChest.wav");
            Greenfoot.stop();
         } 
        if (!isSpecial && getImage() != image1)
        {
            setImage(image1);
            counter++;
            System.out.println(counter);
            Greenfoot.delay(5);
            if (counter == 10) Greenfoot.stop();
        }
    } 
}
SullyFish SullyFish

2014/4/29

#
Thank you again, but I dont know why but the counter resets every time it touches a new chest. The coding doesnt tell it to reset anywhere and so I was wondering how I could change that. Please and thank you AGAIN. The counter just posts 1,1,1,1,1 for everytime
There are more replies on the next page.
1
2