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

2017/5/15

Actor speed when touch

skitlesas skitlesas

2017/5/15

#
Hello. I want add different speed when ball touch block, block2, block3 Ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Kamuoliukas extends Actor
{
    private int deltaX;         
    private int deltaY;         
    
    private boolean stuck = true;   
    private int counter;
    private GreenfootSound fonas = new GreenfootSound("fail.mp3");
    public Kamuoliukas()
    {
        counter = 0;
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            checkBlock();
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        Actor block2 = getOneIntersectingObject(Block2.class);
        Actor block3 = getOneIntersectingObject(Block3.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            Greenfoot.playSound("click.wav");
            deltaY=-deltaY;
            if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(1);
            else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(1);
            else if (getWorld() instanceof Lvl3) ((Lvl3)getWorld()).getCounter().bumpCount(1);
        }
        if(block2 != null)
        {
            getWorld().removeObject(block2);
            Greenfoot.playSound("click.wav");
            deltaY=-deltaY;
            if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(2);
            else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(2);
            else if (getWorld() instanceof Lvl3) ((Lvl3)getWorld()).getCounter().bumpCount(2);
        }
        if(block3 != null)
        {
            getWorld().removeObject(block3);
            Greenfoot.playSound("click.wav");
            deltaY=-deltaY;
            if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(3);
            else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(3);
            else if (getWorld() instanceof Lvl3) ((Lvl3)getWorld()).getCounter().bumpCount(3);
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if(getY() > 598) //you lose
            {
                getWorld().addObject(new Restart(), 344, 297);
                fonas.play();
                Greenfoot.stop();
            }
    }
    
    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            Greenfoot.playSound("click2.wav");
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) {
                deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
You need to login to post a reply.