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

2015/10/27

How can I stop the bird from falling

1
2
3
4
sanderbakker sanderbakker

2015/11/2

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.util.List;
/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RedBird extends Animate
{
    private static GreenfootImage[] images =
    {
        new GreenfootImage("redBird.png"),
        new GreenfootImage("redBird.png"),
    };
    private static int birdNumber;
    private boolean active;
    
    float velocityY = 0;
    float velocityX = 0;
    float speed = 0;
    boolean roll = false;
    Bird bird;
    RedBird redbird;
    /**
     * Act - do whatever the RedBird wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (!active && getX() == 157 && getY() == 365) active = true;
        if (!active) return;
        {
            velocityY += .1;
            speed += .1;
        }
        if(getY() >= 377)
            velocityX = velocityX*.9f;
        if(getY() <= 470&&roll == false){
            
            setLocation(getX()+(int)Math.round(velocityX),getY()+(int)Math.round(velocityY));
            impartForce();
        }
        // bepaald de start positie van het afschieten en tot hoe laag de vogel kan gaan op de Y-as - Sander
        if(roll = true){
            setLocation(getX() + (int)Math.round(velocityX),getY());
            impartForce();
            
        }
        // bepaald tot hoelaag de vogel kan zakken op de Y-as , 470 in dit geval, dit is ook de start positie van de vogel ( geen idee waarom) - Sander 
        
        checkBottom();
       
       
       
    }  
    public RedBird()
    {
        setImage(images[birdNumber]);
        birdNumber = (birdNumber+1)%images.length;
        GreenfootImage  myImage = getImage ();
        int myNewHeight = (int)myImage.getHeight()/75;
        int myNewWidth = (int)myImage.getWidth()/75;
        myImage.scale(myNewWidth, myNewHeight);
   
        // maakt de afbeelding van de rode vogel 75 keer zo klein - Sander
        
        
        turn(-12);
        
        // draait de vogel met -12 graden - Sander
        
       
       
      
    }
    public void checkBottom()
    {
        List<Block> targets = getIntersectingObjects(Block.class);
        boolean verdict = false;
        for(int i = 0;i<targets.size();i++){
            if(getY() < targets.get(i).getY()-9&&getX() > targets.get(i).getX()+9)
            {
                verdict = true;
            }
        // bepaald hoelang de vogel in de lucht blijft & bepaald wat het doel is van de vogel - Sander
        }
        if(verdict == true)
        {
            roll = true;
        }
        else roll = false;
    }
    public void impartForce()
    {
        List<Animate> targets = getIntersectingObjects(Animate.class);
        for(int i = 0;i<targets.size();i++){
           
            {
                float whatever =  (targets.get(i)).getX()/(targets.get(i)).getY();
                float newX = velocityX*Math.abs(1-whatever/(velocityX/velocityY));
                float newY = velocityY*Math.abs(1-whatever/(velocityX/velocityY));
                (targets.get(i)).transferVelocity(velocityX/2, 0);
                velocityY = velocityY;//-newY;
                velocityX = velocityX/2;//2-newX;
            }
        }
        // zorgt dat de blokken als target gezien worden, waardoor de vogel niet letterlijk door de blokken heen vliegt - Sander 
    }
}
This is the code for now
danpost danpost

2015/11/2

#
sanderbakker wrote...
How will my class look then? because every image I use has a diffrent size so if I make them smaller some of them become to small and others to big, this is very frustrating.
You can adjust the size of the bird images depending on the value of 'birdNumber'. The adjusting should be done in the Bird class constructor ( 'public Bird()' ) before setting that initial image to the bird being constructed. The IllegalArgumentException is because you are trying to set the height or width of the scaled image to zero. An image must have positive width and height values or there would be no image to speak of.
danpost danpost

2015/11/2

#
sanderbakker wrote...
< Code Omitted > This is the code for now
I say again, you should only have one class for all the birds -- the only difference between one bird and another is just the image. All other states are the same for each one as well a their behavior. You are still not tracking the location of the bird actors using float or fractional values. Moving or checking the coordinates of the actor using 'getX' and 'getY' will cause your bird actors to not move smoothly or obtain mis-information.
sanderbakker sanderbakker

2015/11/3

#
Yes one class I understand but how can I make them line up behinde the catapult because if I have only one class?
sanderbakker sanderbakker

2015/11/3

#
And what should I change in my scaling code to avoid the error?
danpost danpost

2015/11/3

#
sanderbakker wrote...
Yes one class I understand but how can I make them line up behinde the catapult because if I have only one class?
If you start the Bird class as I have instructed earlier and execute it, you will see that the different images for the birds will be applied to the separate birds.
And what should I change in my scaling code to avoid the error?
On lines 63 and 64, you are dividing the width and height of the original image by 75 and then scaling the image to these new values. This is a drastic reduction in size of the image. If the original width or height was less than 75 to begin with (which one of the two appears to be), then the new value will be zero. You cannot have an image of zero width or height. What size is the original image and what size do you want it to be?
sanderbakker sanderbakker

2015/11/3

#
Okay, thanks I finally fixed the problem with shooting the birds. Now I want to make a line up for shooting the birds. So if I put a bird next to the catapult. A black one a yellow one and a red one. I want them to shoot one by one and dissapear from the line up. Is this possible? Also how can I make a scoreboard on the left top of the screen and make the game go to the next level when the pig is removed from the world by falling below 470 and after 5 seconds of waiting after the pig felt?
danpost danpost

2015/11/3

#
For the scoreboard, you can refer to my Value Display Tutorial. You can probably just have the world act method check to see if the pig is not in the world and count down a timer when it is not (the timer can be zero until the pig is added, when you can set it to a positive value to count down from when the pig is removed.
sanderbakker sanderbakker

2015/11/3

#
And how do I do that?
danpost danpost

2015/11/3

#
sanderbakker wrote...
And how do I do that?
Which part are you referring to? did you try anything? how do you think you would do it?
sanderbakker sanderbakker

2015/11/4

#
The part of the pig, yes I tried it I would do it like
if (counter==400&&getY() >= 470)
{
getWorld().removeObject(this);
Greenfoot.setWorld(new Level1());
}
But this won't work for some reason
danpost danpost

2015/11/4

#
sanderbakker wrote...
The part of the pig, yes I tried it I would do it like < Code Omitted > But this won't work for some reason
By "this won't work", I presume you mean that the transition to a new world is not being made (whether to a different world or to a new world of the same type). Presuming that the actor does go below 470, the only other thing that could be an issue is the value of the 'counter' field. Unfortunately, you did not include all the code related to that field.
sanderbakker sanderbakker

2015/11/4

#
This won't make the pig disapear when it gets below 470 but it will go to the next level. I don't know how I can make the pig disapear and move to the next level after it disapeared. So I want it to move on when the pig is removed from the world.
danpost danpost

2015/11/4

#
The constructor of your Level1 world calls a 'prepare' method that adds a Pig object into the world. If you want to restart the world without the pig, you need to remove the Pig object after the world is created:
World newWorld = new Level1();
newWorld.removeObjects(newWorld.getObjects(Pig.class));
Greenfoot.setWorld(newWorld);
or
Level1 level1 = new Level1();
level1.removeObject(level1.pig);
Greenfoot.setWorld(level1);
sanderbakker sanderbakker

2015/11/4

#
if(getY() > 470)
        {
            getWorld().removeObject(this);
            counter --;
        }
        if (counter --){
            Greenfoot.setWorld(Level2.class);
        }
i've made this to move to the next level but it says that int can not be converted to bolean. What can I do about this
There are more replies on the next page.
1
2
3
4