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

2013/1/16

need help

myheartwillgoon myheartwillgoon

2013/1/16

#
if any friend can help me with that Project, pls. I will be very grateful
Dytanoth Dytanoth

2013/1/16

#
This is your code:
if ( getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
This method is called every act. Your exercise tells you to make it less hyperactive, by using a counter. So they want you to call this method once, every 3 acts. By adding a counter, you can accomplish this:
if(counter==2){
  switchimage();
  counter=0;
}else{
  counter++;
}
myheartwillgoon myheartwillgoon

2013/1/19

#
Hi thanks a lot. it works fine. Pls I posted my another exercise that i get stuck with. Its about the piano game. If u can have a look at that.
vonmeth vonmeth

2013/1/19

#
    public void makeKeys()
    {
        int i = 0;

        Key key = new Key("g", "3a.wav");
        int keyWidth = key.getImage().getWidth();
        int keyHeight = key.getImage().getHeight();
        while (i<12)
        {
            addObject (new Key ( "g", "3a.wav"), i*keyWidth+54, keyHeight/2 );
            i = i + 1;
        }
    }
danpost danpost

2013/1/19

#
@vonmeth, I think the '54' needs to be figured out programmatically; because if you change the width of the keys, that value is no longer valid for the location of the leftmost key if the keyboard is to be centered in the world.
vonmeth vonmeth

2013/1/19

#
Erm, true.
    public void makeKeys()
    {
        int i = 0;

        Key key = new Key("g", "3a.wav");
        int keyWidth = key.getImage().getWidth();
        int keyHeight = key.getImage().getHeight();
        int spacer = (getWidth()-(keyWidth*11))/2;
        
        while (i<12)
        {
            addObject (new Key ( "g", "3a.wav"), i*keyWidth+spacer, keyHeight/2 );
            i = i + 1;
        }
    }
myheartwillgoon myheartwillgoon

2013/1/19

#
Hi thanks! The codes work fine! but may be I need be cleared about the line int spacer = (getWidth()-(keyWidth*11))/2; of course I know what the line does but for some reasons or no reason codes look difficult to me to understand. ohh and "addObject (new Key ( "g", "3a.wav"), i*keyWidth+spacer, keyHeight/2 ); "- here why do we need to write keyHeight/2?
vonmeth vonmeth

2013/1/20

#
I made a slight mistake. int spacer = (getWidth()-(keyWidth*11))/2; should be int spacer = ((getWidth()-(keyWidth*12))/2)+(keyWidth/2); First we are getting the width of the world (in cells, but since the cell size is one pixel, this isn't a problem). The width of the world is 800 (super(800, 340, 1) or super(width, height, cell size in pixels)) We then want to get the width of all the keys we are placing down. We are placing down 12 keys. So we multiply the number of keys (12) by the width of the keys (63). We get 756. (keyWidth*11) We then want to know what space would be left over once all the keys are placed down, so we subtract the width of the world (800) from the width of all the keys (756) . 800 - 756 = 44. getWidth()-(keyWidth*11) We would have 44 pixels left over. We want the keys to be centered, so we want half of 44 on one side, and half of 44 on the other side, so we divide that 44 by 2. That leaves us with 22. ((getWidth()-(keyWidth*12))/2) Now, for the last bit, that I forgot to add in my original post. When an object is placed, its image is centered on that point. So if we placed a key at x = 0, y = 0, half the image would be showing. Try the following out if you don't understand what I mean.
         addObject (new Key ( "g", "3a.wav"), 0,0);
You will see that half image is outside of the world. So to compensate for this, we will get half the width of the key, which is 31. (keyWidth/2) With our other calculations of 22, we add it to 31, and we get 53. ((getWidth()-(keyWidth*12))/2)+(keyWidth/2) or ((800-(63*12))/2)+(63/2)=spacer. We now have our spacer width. The reason we put "keyHeight/2", is the same reason we did "keyWidth/2". The image is centered on its coordinates, so we get half its height so that the top of the image is touching the top of the world. If it was 0, the top half of the keys would not be showing, and if it was keyHeight, they would be to far down. Try doing this, put 0 instead of "keyHeight/2", and then try it with just "keyHeight".
danpost danpost

2013/1/20

#
Basic information needed: The width of the world is: getWidth() (no matter what) The width of the key is: key.getImage().getWidth() (no matter what) The number of keys is assigned to: keyCount (no matter what) The width of the entire keyboard is: keyCount * key.getImage().getWidth() (always the number of keys time the width of one key) Since the keyboard is to centered horizontally in the world, we need: The total horizontal length not occupied by the keyboard: getWidth() - keyCount * key.getImage().getWidth() (always the width of the world minus the width of the keyboard) The unoccupied length on one side of the keyboard will be half of the above. ( getWidth() - keyCount * key.getImage().getWidth() )/2 (always half the total horizontal length not occupied by the keyboard) This is the same as the x-coordinate of the left edge of the keyboard. The x-coordinate for the leftmost key will be half the width of one key more than the above. ( getWidth() - keyCount * key.getImage().getWidth() )/2 + key.getImage().getWidth()/2 (always the location of the first key along the x-axis) For the x-coordinate of the rest of the keys, just continuously add the width of one key. The resulting equation for the x-coordinate of the leftmost key can be simplifyed as follows: Factoring out the division by 2: ( getWidth() - keyCount * key.getImage().getWidth() + key.getImage().getWidth() ) /2 Factoring out the width of one key from the second and third terms within the parenthesis: ( getWidth() + ( - keyCount + 1 ) * key.getImage().getWidth() ) /2 which is equivalent to ( getWidth() - ( keyCount - 1 )*key.getImage().getWidth() ) /2 which is the same thing as you were questioning. As far as the 'keyHeight/2', I think that was just arbitrarily chosen because they did not know the exact y-location to use. Closer might be something like 2*getHeight()/3 or getHeight()-key.getWidth()/2-spacer2. But it is not something to worry about; as these are all just guesswork possibilities.
danpost danpost

2013/1/20

#
@vonmeth, you did not make a mistake with the formula (both are equivalent). However, where you say 'We get 756. (keyWidth*11)', I think you meant 'We get 756. (keyWidth*12)'. One the next step, you meant to say we subtract the width of all the keys from the width of the world; and where you say '800 - 756 = 44. getWidth()-(keyWidth*11)', you meant '800 - 756 = 44. getWidth()-(keyWidth*12)'. Back to the formula: lets plug in the values in the other formula and see what we get. (getWidth()-(keyWidth*11) ) / 2 ( 800 -( 63 *11) ) / 2 ( 800 - 693 ) / 2 107 / 2 53 (your 'spacer' value).
vonmeth vonmeth

2013/1/20

#
@danpost, you're right, I did mean for it to be (keyWidth*12) and getWidth()-(keyWidth*12). Not sure why I reverted back to 11 there. Thanks! =) Ah, they are equivalent. I thought it was just 'luck' that 11 worked out, but looking at it again, they would be equivalent as per your explanation in the factoring.
You need to login to post a reply.