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/10/27

#
I need to make angry birds for a school project but every time I hit run. The bird drops to the ground. This is my bird code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.util.List;
/**
 * Write a description of class RedBird[s][/s] here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RedBird extends Animate
{
    
    float velocityY = 0;
    float velocityX = 0;
    float speed = 0;
    boolean roll = false;
    RedBird redbird; 
    int counter = 0;
    /**
     * 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() 
    {
        {
            velocityY += .1;
            speed += .1;
        }
        if(getY() >= 500)
            velocityX = velocityX*.9f;
            // bepaald wanneer de vogel wordt afgeremd en naar de grond valt 
        if(getY() <= 470 -(Math.sin(Math.abs(Math.toRadians(getRotation()))))){
            
            setLocation(getX(),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();
        counter++;
        removeRedBird();
       
       
       
    }  
    public RedBird()
    {
        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;//Zorgt ervoor dat de constructie niet helemaal weg gevaagd wordt - Sander
                velocityX = velocityX/2;//zorgt ervoor dat de constructie niet in een keer helemaal weg gevaagd wordt door de vogel - Sander
            }
        }
        // zorgt dat de blokken als target gezien worden, waardoor de vogel niet letterlijk door de blokken heen vliegt - Sander 
    }
    
    public void removeRedBird()
    {
        if (counter == 170)
        {   
            getWorld().removeObject(this);
        }
    }
}
This is how the game looks: So everytime it hit run the bird drops from the catapult to the ground
danpost danpost

2015/10/28

#
It is hard to say why the bird drops like you say. However, I do not believe that the 'impartForce' method does what you had intended; and this may have something to do with the behavior you are getting. As it is, you could remove lines 99, 100, 101 and 103 without changing what the method does. From what I can tell, 'velocityY' always increases and there are no restrictions on the accelerated fall until a y-coordinate' value of 470 is reached. Extra: lines 85 through 89 is quite round-about as the one line 'roll = verdict;' by itself would do the same thing.
sanderbakker sanderbakker

2015/10/28

#
So it is impossible to stop the bird from falling from it's possition, because it is very anoying. I tried to remove the lines you said but it didin't fix the problem @danpost
sanderbakker sanderbakker

2015/10/28

#
danpost wrote...
It is hard to say why the bird drops like you say. However, I do not believe that the 'impartForce' method does what you had intended; and this may have something to do with the behavior you are getting. As it is, you could remove lines 99, 100, 101 and 103 without changing what the method does. From what I can tell, 'velocityY' always increases and there are no restrictions on the accelerated fall until a y-coordinate' value of 470 is reached. Extra: lines 85 through 89 is quite round-about as the one line 'roll = verdict;' by itself would do the same thing.
btw if I change the values on line 32 from 470 to 300 or something the bird will stop falling but when I shoot it from it's position it will not get below 300 on the whole game.
danpost danpost

2015/10/28

#
sanderbakker wrote...
So it is impossible to stop the bird from falling from it's possition, because it is very anoying. I tried to remove the lines you said but it didin't fix the problem @danpost
I did not say that removing those lines would fix anything -- just that they do not do anything within your code. One cannot, for sure, say why the actor is falling straight down. It may be because the velocity is a float -- but, the location coordinates are int values (the position of the actor is not being tracked very precisely). Your actor could be told to move all day at a rate of 0.1 pixels per act cycle; but, the actor would never move -- not after 10, 100, or 1000000 cycles). Unless the positional coordinates are tracked more precisely using decimal or fractional values, using float velocity values loses a lot of its fractional importance.
sanderbakker sanderbakker

2015/10/29

#
So there is no way to fix it ?
danpost danpost

2015/10/29

#
sanderbakker wrote...
So there is no way to fix it ?
I did not say that. You need to track the location coordinates of the bird more exactly (using decimal or fractional values). Something else -- if the bird is to rise before falling, then the value of 'velocityY' should start off as a negative value.
sanderbakker sanderbakker

2015/10/29

#
Okay thanks Can you help me with another problem? I want to shoot the redbird on the construction. after that it will disapear within 10 seconds. After that I want to replace the redbird with the purple one All of these parts are working in the code except the part of replacing the bird with another. How can I replace it? This is my world code
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    RedBird redbird;
    Block block;
    Pig pig; 
    Ground1 ground1;
    
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(950, 600, 1); 
        
        addObject(new TNT(),170,464);
        addObject(new Katapult(),161,406);
        addObject(new BlueBird(), 20,469);
        addObject(new YellowBird(), 73,471);
        redbird = new RedBird();
        addObject(redbird, 125,474);
        block = new Block();
        addObject(block, 600,475);
        block = new Block();
        addObject(block, 627,475);
        block = new Block();
        addObject(block, 654,475);
        block = new Block();
        addObject(block, 681,475);
        block = new Block();
        addObject(block, 708,475);
        block = new Block();
        addObject(block, 735,475);
        block = new Block();
        addObject(block, 762,475);
        block = new Block();
        addObject(block, 789,475);
        block = new Block();
        addObject(block, 816,475);
        block = new Block();
        addObject(block, 843,475);
        block = new Block();
        addObject(block, 870,475);
        block = new Block();
        addObject(block, 627,448);
        block = new Block();
        addObject(block, 654,448);
        block = new Block();
        addObject(block, 681,448);
        block = new Block();
        addObject(block, 708,448);
        block = new Block();
        addObject(block, 735,448);
        block = new Block();
        addObject(block, 762,448);
        block = new Block();
        addObject(block, 789,448);
        block = new Block();
        addObject(block, 816,448);
        block = new Block();
        addObject(block, 843,448);
        block = new Block();
        addObject(block, 654,421);
        block = new Block();
        addObject(block, 681,421);
        block = new Block();
        addObject(block, 708,421);
        block = new Block();
        addObject(block, 735,421);
        block = new Block();
        addObject(block, 762,421);
        block = new Block();
        addObject(block, 789,421);
        block = new Block();
        addObject(block, 816,421);
        block = new Block();
        addObject(block, 681,394);
        block = new Block();
        addObject(block, 708,394);
        block = new Block();
        addObject(block, 735,394);
        block = new Block();
        addObject(block, 762,394);
        block = new Block();
        addObject(block, 789,394);
        
        block = new Block();
        addObject(block, 708,367);
        block = new Block();
        addObject(block, 735,367);
        block = new Block();
        addObject(block, 762,367);
        block = new Block();
        addObject(block, 735,340);
        pig = new Pig();
        addObject(pig, 735,301); 
        ground1 = new Ground1();
        addObject(ground1, 729,495);
       
    }
    
    public void act ()
    {
    if(Greenfoot.mouseClicked(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse.getButton() <2)
            {
                removeObject(redbird);
                redbird = new RedBird();
                addObject(redbird,157,365);
                if(365-mouse.getY() > -150)
                    redbird.velocityY = (365-mouse.getY())/10;
                else
                    redbird.velocityY = -5;
                if(157-mouse.getX() < 125)
                    redbird.velocityX = (157-mouse.getX())/10;
                else
                    redbird.velocityX = 10;
            }
        }
        // Laat de vogel vanaf een bepaald x en y punt starten, en laat de vogel tot een bepaald punt vliegen, hoe lager de waarde hoe minder ver de vogel komt - Sander
    
    }
}
Thanks for helping me
danpost danpost

2015/10/29

#
You should have one class for all birds that you shoot. You can subclass that class with the individual bird classes, if you want; but, the only real gain from doing that is applying the proper image to each bird. This, however, can be done in the one class while tracking the order of the images:
import greenfoot.*;

public class Bird extends Actor
{
    private static GreenfootImage[] images =
    {
        new GreenfootImage("redBird.png"),
        new GreenfootImage("purpleBird.png"),
        new GreenfootImage("blueBird.png"),
        new GreenfootImage("yellowbird.png")
    };
    private static int birdNumber;

    public Bird()
    {
        setImage(images[birdNumber]);
        birdNumber = (birdNumber+1)%images.length;
    }
I noticed that you are adding several birds into the world when the world is created (a YellowBird, BlueBird and a RedBird). Are you lining up the birds in a queue for the catapult? If so, you need a boolean field to track their active state. Insert the following at line 13 above:
private boolean active;
The first lines in the act method can check to set active and exit the method if still not active. Something like this:
if (!active && getX() == 157 && getY() == 365) active = true;
if (!active) return;
which says to set active if currently not active and set at catapult and then if still not active, exit the method (do not process any further code in the act method). If you are to have multiple Level# classes, you may want to have an intermediate class between World and these level worlds for all the levels.
sanderbakker sanderbakker

2015/10/29

#
So from all my birds actors I need to make one bird class. So just bird and not RedBird, YellowBird, Bluebird etc.?
sanderbakker sanderbakker

2015/10/29

#
If I apply this, it will shoot a bird one time and then give me a error. It won't shoot a bird for the second time. I want it to shoot 3 birds and then move to the next level (level2)
danpost danpost

2015/10/29

#
sanderbakker wrote...
If I apply this, it will shoot a bird one time and then give me a error. It won't shoot a bird for the second time. I want it to shoot 3 birds and then move to the next level (level2)
What error message on what code??? cannot help without pertinent data.
danpost danpost

2015/10/29

#
sanderbakker wrote...
So from all my birds actors I need to make one bird class. So just bird and not RedBird, YellowBird, Bluebird etc.?
I do not see a need for multiple bird classes.
sanderbakker sanderbakker

2015/11/2

#
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.
sanderbakker sanderbakker

2015/11/2

#
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:189) at greenfoot.GreenfootImage.scale(GreenfootImage.java:388) at RedBird.<init>(RedBird.java:67) at Level1.<init>(Level1.java:29) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at greenfoot.core.Simulation.newInstance(Simulation.java:607) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:441) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) I got this error after the second time I try to shoot a bird
There are more replies on the next page.
1
2
3
4