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

2014/4/3

Help! Homework World

adam0314 adam0314

2014/4/3

#
I have been working on this for three days and can't figure out what I'm doing wrong. Any advice is much appreciated! Homework World Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HomeworkWorld here.
 * 
 * @author (Your name)
 * @version (a version number or a date)
 */
public class HomeworkWorld extends World
{
    /**
     * INSERT CODE BEOLW
     * Add two instance variables that are of type String[]
     *        one will be an array of images
     *        one will be an array of sounds
     *        DO NOT INCLUDE THE FILE SUFFIX - add that through code later
     *        
     *        Note - each image is 55x55
     */
    private String[] image = {"smiley1", "smiley2", "smiley3", "smiley4", "smiley5"};;
    private String[] soundfile = {"hello", "happy", "crying", "ohno", "raspberry"};
    
    /**
     * Constructor for objects of class HomeworkWorld.
     * 
     */
    public HomeworkWorld()
    {   
        
        // Create a new world with 400x100 cells with a cell size of 1x1 pixels.
        super(400, 100, 1);
        
    }
        
        /**
         * INSERT CODE BELOW
         * Write a "for" loop
         *      It should loop 5 times  (you can either hard code 5 or use code)
         *      It should use the method addObject( Actor object, int x, int y) to add new Emoticons to your world.
         *      Note:  concatinate the ".png" for images  and ".wav" for the sounds
         *      
         *      Determine the x value using a mathematical calculation
         *      The y value can be either hard coded or calculated
         *      
         */
        private void makeImages()
                   {
                       //make the Emoticon Images
                       for(int i = 0; i<6; i++)
                       {
                            Emoticon emoticon = new Emoticon();
                            addObject(emoticon, 10*(i+60), 50);
                                   }
                }
            }

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

/**
 * Write a description of class Emoticon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Emoticon extends Actor
{
    /**
     *INSERT CODE BELOW
     *    Create two instance varaibles of type String
     *         one named sound
     *         one named image
     */
   private void emoticon(String image[i], String soundFile[i])
    
           
    
    /**
     * INSERT CODE BELOW
     *   Write a construtor method
     *         The method should have two parameters
     *                type:  String     name:   newImage
     *                type:  String     name:   newSound
     *    
     *         The method should
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter
     *               
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image.
     *               
     */
    public Emoticon(String image, String soundFile)
    {
        setImage(image + ".png"); 
        sound = soundFile;
    }    /**
     * Act - do whatever the Emoticon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        // When the mouse is click on this object, play the sound.
         if(Greenfoot.mouseClicked(this))
         {
             /**
              * INSERT CODE BELOW
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound
              *     
              */
             Greenfoot.playSound(sound + ".wav");
         }
            
    }    
}
bourne bourne

2014/4/3

#
Line 17 of the Emoticon class looks like a constructor header. I guess it should be two instance field declarations. Like what you did on lines 20 and 21 in the HomeworkWorld class. Though it wants
Create two instance variables of type String one named sound one named image
whereas lines 20 and 21 from HomeworkWorld were arrays.
adam0314 adam0314

2014/4/4

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

/**
 * Write a description of class Emoticon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Emoticon extends Actor
{
    /**
     *INSERT CODE BELOW
     *    Create two instance varaibles of type String
     *         one named sound
     *         one named image
     */
   private String [] sound;
   private String [] image;
 
    {
        
           
    
    /**
     * INSERT CODE BELOW
     *   Write a construtor method
     *         The method should have two parameters
     *                type:  String     name:   newImage
     *                type:  String     name:   newSound
     *    
     *         The method should
     *               1.   set the instance variable  image   to the value passed by the corresponding parameter
     *               2.   set the instance variable  sound   to the value passed by the corresponding parameter
     *               
     *               3.   Use the method   setImage( java.lang.String filename ) to set the image.
     *               
     */
  
    public Emoticon(String newImage, String newSound)
    { 
        image = newImage;
        sound = newSound;
        setImage(image);
    }
    /**
     * Act - do whatever the Emoticon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        // When the mouse is click on this object, play the sound.
         if(Greenfoot.mouseClicked(this))
         {
             /**
              * INSERT CODE BELOW
              *     Use the method  Greenfoot.playSound( java.lang.String filename ) to play the sound
              *     
              */
             Greenfoot.playSound(sound + ".wav");
         }
            
    }    
}
bourne bourne

2014/4/4

#
You have an extra curly brace at line 20. And the fields declared at lines 17 and 18 are declared as arrays (the brackets make them arrays) but it does not seem to be what you want.
danpost danpost

2014/4/4

#
I see this line as your Emoticon constructor declaration statement:
// code block A
public Emoticon(String newImage, String newSound)
but I do not see this line
// code block B
public Emoticon()
From your world class at line 51 above, you have this:
// code block C
Emoticon emoticon = new Emoticon();
which has the system looking for the line in block B in your Emoticon class to construct the object with. Look at block A above. When creating a new Emoticon object, you must supply the String image name and String sound name. That means the correct way to create an Emoticon object would be:
Emoticon emoticon = new Emoticon(image[i], soundfile[i]);
A couple other things I had noticed: (a) in your Emoticon constructor block, your 'setImage' call uses 'image' as the filename; but, that is incomplete; (b) your 'for' loop in the 'makeImages' method in your world class will execute 6 times
You need to login to post a reply.