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

2016/3/8

Help me for my table football game

fearDnine fearDnine

2016/3/8

#
i'm making a table football,i want make the ball bounce to the player, but the ball always stuck on all player, help mee please
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class ball1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball1 extends Actor
{
    /**
     * Act - do whatever the ball1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    private int speedX;
    private int speedY; 
         
    /**
     * make the ball move randomly
     */
    public ball1()
    {
        speedX =  Greenfoot.getRandomNumber(11) - 5;
        speedY =  Greenfoot.getRandomNumber(11) - 5;
    
     GreenfootImage image = new GreenfootImage("ball1.png");
     image.scale( 40,40);
     setImage(image);


    }
    
    public void act() 
    {
        move();
           turn(12);
        bounce ();
            
        // scoring ();
    }  
    
    /**
     * move the ball and see if the ball have hit an edge.
     */
    private void move()
    {
    
        if (getX() == 0 || getX() == getWorld().getWidth()-1)
        {
            speedX = -speedX;
            
        }
        if (getY() == 0)
        {
            speedY= -speedY;
            
        }
        
        if (getY() == getWorld().getHeight()-1)
        {
            speedY= -speedY;
           
        }
        if (isAtEdge()) {
            turn(180);
        }
     
        setLocation(getX()+ speedX,getY() + speedY);
    }
    
        private void bounce ()
        {
            Actor Blue1 = getOneIntersectingObject(Blue1.class);
            if (Blue1  != null) 
            {
                speedY=-speedY;
                speedX=-speedX;
            }
            Actor Blue2 = getOneIntersectingObject(Blue2.class);
            if (Blue2  != null) 
            {
                speedY=-speedY;
                speedX=-speedX;
            }
            Actor Blue3 = getOneIntersectingObject(Blue3.class);
            if (Blue3  != null) 
            {
                speedY=-speedY;
                speedX=-speedX;
            }
            Actor Green1 = getOneIntersectingObject(Green1.class);
            if (Green1  != null) 
            {
                speedY=-speedY;
                speedX=-speedX;
            }
            Actor Green2 = getOneIntersectingObject(Green2.class);
            if (Green2 != null) 
            {
                speedY=-speedY;
                speedX=-speedX;
            }
            Actor Green3 = getOneIntersectingObject(Green3.class);
            if (Green3 != null) 
            {
                speedY=-speedY;
                speedX=-speedX;         
            }
        }
        public boolean canMove(){
        
        World Field=getWorld();
        int x=getX();
        int y=getY();
        
        if(x>= Field.getWidth()|| y>= Field.getHeight()){return false;}
        else if (x<0 || y<0){return false;}
        List bounary= Field.getObjectsAt(x,y,bounary.class);
        if(bounary.isEmpty()) {
                return true;
            }
            else {

                return false; 

            }
        
        }
         
    
    // private void scoring ()
    // {
        // Actor brick = getOneIntersectingObject(Brick.class);
        // if (brick != null)
        // {
            // speedY= -speedY;
            // getWorld().removeObject(brick);
            // Greenfoot.playSound ("boo.wav");
            // Box myBox = (Box) getWorld();
            // myBox.score();
        // }
    // }
    
    
}

     

Super_Hippo Super_Hippo

2016/3/8

#
Call the 'move' method at the end of the 'bounce' method. Maybe that helps. Why do you have a different class for each player?
You need to login to post a reply.