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

2015/6/19

Speed question

1
2
Gingervitis Gingervitis

2015/6/21

#
danpost wrote...
Change '16' on line 7 to '10'. Also, change line 8 to this:
if(timer == 0)
You may also want to set attacking to false when taking damage within the 'if' block of line 8.
Could I please post my scenario again? I think the issue may lie elsewhere once the user clicks "New Game". I notice that the images animate faster and everything regarding the speed changes normally before the user actually starts playing the game.
danpost danpost

2015/6/21

#
Gingervitis wrote...
Could I please post my scenario again? I think the issue may lie elsewhere once the user clicks "New Game". I notice that the images animate faster and everything regarding the speed changes normally before the user actually starts playing the game.
Make sure you include the source so I can download and look at the code.
Gingervitis Gingervitis

2015/6/21

#
Having a problem publishing the scenario.... May take a bit
danpost danpost

2015/6/21

#
Gingervitis wrote...
Having a problem publishing the scenario.... May take a bit
Too large (over 20 MB)?
Gingervitis Gingervitis

2015/6/21

#
danpost wrote...
Too large (over 20 MB)?
Over 190 MB according to the folder information
danpost danpost

2015/6/21

#
Convert large sound files to '.mp3' format and remove any not used. Remove excess images -- those not used at all and large images not immediately needed (background images for other worlds -- and this world if needed). Whatever you can do to reduce the size without taking away from the animation issue. If you have to remove all sound files to help reduce it to an uploadable size, then do so.
danpost danpost

2015/6/21

#
Okay, I got it. Will look at it shortly. You can delete the scenario if you wish, now.
danpost danpost

2015/6/21

#
I am finding it quite difficult to work with. I keep getting Java Heap Space or Out of Memory errors during compilations.
Gingervitis Gingervitis

2015/6/21

#
danpost wrote...
I am finding it quite difficult to work with. I keep getting Java Heap Space or Out of Memory errors during compilations.
When did you start getting those errors? I'm sorry if its my coding. I have been doing my best to make it as efficient as my limited knowledge allows me..
danpost danpost

2015/6/21

#
Gingervitis wrote...
When did you start getting those errors? I'm sorry if its my coding. I have been doing my best to make it as efficient as my limited knowledge allows me..
Pretty much immediately. Never the first time, when initially loading the project; but, on subsequent compilings. I have found a lot of little things that could be improved on. These include the use and setting of 'static' fields; the class structure; the excessive use of fields; and the lack of coherent structures for your codes (among other things). Also, I was wondering why you hid objects in the world by making them transparent as opposed to just removing them. I will take your Menu class and its subclasses and combine them into a single class (making use of 'inner classes'). Then I will post it here as an example of a better structure. This could also be done without using inner classes (using a Menu class and one Button class), but we can treat the combination as one unit by using inner classes.
Gingervitis Gingervitis

2015/6/21

#
Why do you get those errors and I don't? I never got a single error you described. Also I used 'static' fields hoping it would work.... What would be an example of 'lack of coherent structures of codes'?
danpost danpost

2015/6/22

#
Gingervitis wrote...
Why do you get those errors and I don't? I never got a single error you described. Also I used 'static' fields hoping it would work.... What would be an example of 'lack of coherent structures of codes'?
I do not know why I get them and you do not. Maybe you increased your available java heap space or your system affords you more. I just do not know. You need to reset your static fields to initial values in your world constructor or they will remain at their current values during reset. There were several places where the code either did not really do anything or was lacking logic. Examples of not doing anything: * creating a MouseInfo object in a couple classes where it was not later made use of; * calling methods that only return a value, but that is all you do, call it (you do not use that returned value for anything) * adding 'getter' and 'setter' methods that are only used within the class, where you could just set the field directly; * asking if a returned List object is 'null', where they are never 'null' (at least for the methods you are using) -- they could be empty, or have no elements, but are never 'null' Example of lacking logic: * using conditions in 'if' statements where the state is already known; Other noticables: * using an external counter with a loop to hold a number that is always one more than the loop counter; * leaving the first element in a List empty (see your actor image arrays where element 0 is never set)
danpost danpost

2015/6/22

#
I came up with the following code for a world constructor to create a menu similar to yours:
// with the following field declared in the world class
private InfoPanel info;
// in the constructor of that world class
TextImage.setDefaultName("Copperplate Gothic Bold");
TextImage.setDefaultSize(32);
String[][] data =
{
    { "New Game", "" },
    { "Setting", "Speed:  50\nWidth:  "+getWidth()+"\nHeight:  "+getHeight() },
    { "Controls", "Left:  move left\nRight:  move right\nSpace:  shoot" }
};
addObject(info = new InfoPanel(data), getWidth()/2, getHeight()/2);
and this for the InfoPanel class (similar to your Menu class with its subclasses):
import greenfoot.*;
import java.awt.Color;
import java.util.Arrays;

public class InfoPanel extends Actor
{
    private Actor[] buttons;

    /**
     * creates an InfoPanel object using the captions and texts given
     * 
     * param buttonData an array of paired Strings; captions and their displayed texts
     */
    public InfoPanel(String[][] buttonData)
    {
        buttons = new Actor[buttonData.length]; // initialize the button array
        for (int i=0; i<buttonData.length; i++) // for each button to add
        {
            buttons[i] = new Button(buttonData[i][0]); // assign new button with assigned caption to array
            ((Button)buttons[i]).setClickImage(createClickImage(buttonData[i][1])); // save the click image to button
        }
        setImage((GreenfootImage)null); // the infopanel will show click images when needed
    }
    
    /**
     * creates and returns the image with given text
     * 
     * param text the text for the image to display
     */
    private GreenfootImage createClickImage(String text)
    {
        GreenfootImage txtImg = new TextImage(text);
        GreenfootImage image = new GreenfootImage(txtImg.getWidth()+20, txtImg.getHeight()+20);
        image.setColor(new Color(93, 93, 93));
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(5, 5, image.getWidth()-11, image.getHeight()-11);
        image.drawImage(txtImg, 10, 10);
        return image;
    }
    
    /**
     * adds the buttons into the world when the infopanel is placed into a world (or when clicked on -- see 'act' method)
     */
    protected void addedToWorld(World world)
    {
        for (int i=0; i<buttons.length; i++)
        {
            world.addObject(buttons[i], getX(), getY()+((i*2+1-buttons.length)*buttons[i].getImage().getHeight()/2));
        }
    }

    /**
     * returns to unclicked state when infopanel is clicked (button clicks go to clicked state)
     */
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            setImage((GreenfootImage)null); // hides infopanel object
            addedToWorld(getWorld()); // adds buttons back into world
        }
    }            

    /**
     * returns the array of buttons for this infopanel
     */
    public Actor[] getButtons()
    {
        return buttons;
    }

    /** ********************************************************************************* */
    
    /**
     * The Button class creates clickable objects for an infopanel object
     */
    private class Button extends Actor
    {
        private GreenfootImage clickImage; // the image to set to the infopanel owner when button is clicked
        
        /**
         * creates the button with the given text
         */
        private Button(String text)
        {
            GreenfootImage txtImg = new TextImage(text);
            GreenfootImage image = new GreenfootImage(400, 120);
            image.setColor(new Color(93, 93, 93));
            image.fill();
            image.setColor(Color.lightGray);
            image.fillRect(25, 20, 349, 79);
            image.drawImage(txtImg, 200-txtImg.getWidth()/2, 60-txtImg.getHeight()/2);
            setImage(image);
        }

        /**
         * removes self if owner is removed from world;
         * sets image of owner and removes all owner buttons when clicked
         */
        public void act()
        {
            if (InfoPanel.this.getWorld() == null)
            {
                getWorld().removeObject(this);
                return;
            }
            if (Greenfoot.mouseClicked(this))
            {
                InfoPanel.this.setImage(clickImage);
                getWorld().removeObjects(Arrays.asList(buttons));
            }
        }
        
        /**
         * helper method to set the image to give owner when this button is clicked
         */
        public void setClickImage(GreenfootImage image)
        {
            clickImage = image;
        }
    }
}
Create a new scenario, call it 'InfoPanel test'. Add the field and code for the constructor in a new subclass of World. Add a subclass of Actor for the InfoPanel class to be copied into and import the TextImage class into the scenario. Then test it out.
Gingervitis Gingervitis

2015/6/22

#
....I feel like such a bad coder when I see everything you provide....I will test your code shortly
You need to login to post a reply.
1
2