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

2015/11/22

Remove actor after 1 hit

sanderbakker sanderbakker

2015/11/22

#
import greenfoot.*;
import java.awt.*;
import java.util.List;

/**
 * Write a description of class DarthMaul here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DarthMaul extends Animate
{
    /**
     * Act - do whatever the DarthMaul wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    Block block;
    Bird3 bird3;
    private int speed = 4;
    private int vSpeed = 0;
    private int acceleration = 2;
    float velocityY = 0;
    float velocityX = 0;
    int rSpeed = 0;
    int size = 20;
    boolean second = false;
    private int hitcounter = 3;
    public void act() 
    {
        turnAtEdge();
         
        
        setRotation(getRotation()+rSpeed);
        setLocation(getX()+(int)Math.round(velocityX),getY());
        // bepaald de locatie van de blokken 
        velocityX = velocityX*getFriction();
        // friction & velocity samen bepalen de snelheid, zie onderstaande comments - Sander
        if(velocityY == 0 && getRotation() > 0)
        {
            rSpeed = 0;
            setRotation(0);
            
        }
        if(velocityY == 0)
        {
            setRotation(Math.abs(getRotation())-1);
        }
        impartForce();
        {
            velocityY += .1;
            // de snelheid van de verschuifing wordt bepaald door velocity + 0.1, friction bepaald ook nog de snelheid van de verschuifing  - Sander
        }
        if(getY() <= 0-(10*Math.sin(Math.abs(Math.toRadians(getRotation())))))
            setLocation(getX(),getY()+(int)Math.round(velocityY));
        else{
            if(velocityY>5){
                getWorld().removeObject(this);
                return;
            }
            velocityY = 0;
            
        }
        DarthMaul darthmaul = (DarthMaul)getOneIntersectingObject(DarthMaul.class);  
        if (darthmaul != null)  
        {  
            darthmaul.takeDamage();  
            getWorld().removeObject(this);  
            return;  
        }
        if (hitcounter == 0)
        {
            getWorld().removeObject(this);
        }
    }    
    public void turnAtEdge()
    {
        if ( isAtEdge() ) 
        {
            turn(17);
        }
    }
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) 
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }
    public void randomMove()
    {
        
            move(Greenfoot.getRandomNumber(14)  );
       
    }
    public void takeDamage()
    {
        hitcounter = 0;
    }
 public void transferVelocity(float x, float y)
    {
        velocityX += x/1.4;
        velocityY += y/1.4;
        stop();
        // bepaald hoever de blokken bewegen, hoe hoger de waard achter het = teken, hoe langzamer het gaat - Sander
    }
    public void stop()
    {
        if(Math.pow(Math.pow(velocityY,2)+Math.pow(velocityX,2),10)>2){
            if(second == false){
                velocityX = 0;
                velocityY = 0;
                second = true;
            }
        }
        // zorgt er voor dat het object stopt met bewegen - Sander
    }
    public float getFriction()
    {
        float friction = -1;
        if(getY() >= 700)
            friction += .7;
        return 0-friction;
        // bepaald hoeveel ruimte het blok verschuift, hoe hoger de waarde hoe sneller het verschuift en hoe meer het verschuift  - Sander
    }
    public void impartForce()
    {
        List<Animate> targets = getIntersectingObjects(Animate.class);
        float direction = velocityX/Math.abs(velocityX);
        for(int i = 0;i<targets.size();i++){
            if(targets.get(i).getX()*direction>getX()){
                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, velocityY/2);
                velocityY = velocityY/2;//-newY;
                velocityX = velocityX/2;//2-newX;
            }
            // zorgt dat de blokken niet oneindig blijven schuifen, en dat ze niet te ver doorschuif zodra een object het blok raakt - Sander 
            {}
        }
    }
}
I've made this code but it won't remove the DarthMaul after I hited it once with a bird
danpost danpost

2015/11/22

#
The only thing that the code is showing a collision interest in, at the moment, is when a DarthMaul object hits another DarthMaul object. There is nothing showing one hitting a bird (presuming it is a different class of object).
sanderbakker sanderbakker

2015/11/22

#
So I need to change intersecting DarthMaul to Bird ?
sanderbakker sanderbakker

2015/11/22

#
import greenfoot.*;
import java.awt.*;
import java.util.List;

/**
 * Write a description of class DarthMaul here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DarthMaul extends Animate
{
    /**
     * Act - do whatever the DarthMaul wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    Block block;
    Bird3 bird3;
    DarthMaul darthmaul;
    private int speed = 4;
    private int vSpeed = 0;
    private int acceleration = 2;
    float velocityY = 0;
    float velocityX = 0;
    int rSpeed = 0;
    int size = 20;
    boolean second = false;
    private int hitcounter = 3;
    public void act() 
    {
        turnAtEdge();
         
        
        setRotation(getRotation()+rSpeed);
        setLocation(getX()+(int)Math.round(velocityX),getY());
        // bepaald de locatie van de blokken 
        velocityX = velocityX*getFriction();
        // friction & velocity samen bepalen de snelheid, zie onderstaande comments - Sander
        if(velocityY == 0 && getRotation() > 0)
        {
            rSpeed = 0;
            setRotation(0);
            
        }
        if(velocityY == 0)
        {
            setRotation(Math.abs(getRotation())-1);
        }
        impartForce();
        {
            velocityY += .1;
            // de snelheid van de verschuifing wordt bepaald door velocity + 0.1, friction bepaald ook nog de snelheid van de verschuifing  - Sander
        }
        if(getY() <= 0-(10*Math.sin(Math.abs(Math.toRadians(getRotation())))))
            setLocation(getX(),getY()+(int)Math.round(velocityY));
        else{
            if(velocityY>5){
                getWorld().removeObject(this);
                return;
            }
            velocityY = 0;
            
        }
        Bird3 bird3 = (Bird3)getOneIntersectingObject(DarthMaul.class);  
        if (bird3 != null)  
        {  
            darthmaul.takeDamage();  
            getWorld().removeObject(this);  
            return;  
        }
        if (hitcounter == 0)
        {
            getWorld().removeObject(this);
        }
    }    
    public void turnAtEdge()
    {
        if ( isAtEdge() ) 
        {
            turn(17);
        }
    }
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) 
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }
    public void randomMove()
    {
        
            move(Greenfoot.getRandomNumber(14)  );
       
    }
    public void takeDamage()
    {
        hitcounter = 0;
    }
 public void transferVelocity(float x, float y)
    {
        velocityX += x/1.4;
        velocityY += y/1.4;
        stop();
        // bepaald hoever de blokken bewegen, hoe hoger de waard achter het = teken, hoe langzamer het gaat - Sander
    }
    public void stop()
    {
        if(Math.pow(Math.pow(velocityY,2)+Math.pow(velocityX,2),10)>2){
            if(second == false){
                velocityX = 0;
                velocityY = 0;
                second = true;
            }
        }
        // zorgt er voor dat het object stopt met bewegen - Sander
    }
    public float getFriction()
    {
        float friction = -1;
        if(getY() >= 700)
            friction += .7;
        return 0-friction;
        // bepaald hoeveel ruimte het blok verschuift, hoe hoger de waarde hoe sneller het verschuift en hoe meer het verschuift  - Sander
    }
    public void impartForce()
    {
        List<Animate> targets = getIntersectingObjects(Animate.class);
        float direction = velocityX/Math.abs(velocityX);
        for(int i = 0;i<targets.size();i++){
            if(targets.get(i).getX()*direction>getX()){
                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, velocityY/2);
                velocityY = velocityY/2;//-newY;
                velocityX = velocityX/2;//2-newX;
            }
            // zorgt dat de blokken niet oneindig blijven schuifen, en dat ze niet te ver doorschuif zodra een object het blok raakt - Sander 
            {}
        }
    }
}

Now I've made this but DarthMaul won't be removed after 1 hit
danpost danpost

2015/11/22

#
sanderbakker wrote...
So I need to change intersecting DarthMaul to Bird ?
If the intersecting of a DarthMaul object kills a Bird object.
sanderbakker sanderbakker

2015/11/22

#
I want the bird to kill the Darthmaul that is the whole point and this still doens't happen when I shoot the bird on the darthmaul
danpost danpost

2015/11/22

#
You still need to change the class name at the end of line 64.
sanderbakker sanderbakker

2015/11/22

#
I've changed that and when I shoot the bird on the Darthmaul I got an greenfoot error java.lang.NullPointerException at DarthMaul.act(DarthMaul.java:67) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2015/11/22

#
Change 'darthmaul' on line 67 at DarthMaul.act(DarthMaul.java:67) to 'bird3'. No -- do not do that. The bird is being removed -- 'this', the DarthMaul object hitting the bird is taking damage. Change the line to:
this.takeDamage();
// or to this
takeDamage();
sanderbakker sanderbakker

2015/11/22

#
Thanks this helped me very much it is fixed now
You need to login to post a reply.