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

2018/6/12

isTouching not really working

fabipfolix fabipfolix

2018/6/12

#
I'm programming a little Basketball Game. If the Ball is hitting the Hoop he should be thrown back, but nothing happens. Here is my Ball class:
import greenfoot.*;

public class Ball extends Actor
{
    public int deltaX = (Greenfoot.getRandomNumber(5) + 3);
    public int deltaY = (Greenfoot.getRandomNumber(5) + 3);
           
    public void act() 
    {
        setLocation(getX() + deltaX, getY() + deltaY);  
        wallCollision();
        touchingHoop();
 
    }    
    
    public void wallCollision()
    {
        if (getX() < 10 || getX() > 1823)
        { 
            deltaX = -deltaX;      
            
           
        }
        
        if (getY() < 10 || getY() > 1090)
        { 
            deltaY = -deltaY;   
            
            
        }
        
        
    }
    
    public void touchingHoop()
    {
        if (isTouching(Backboard.class)) 
        {
           
            deltaX = -deltaX;  
            
            
        }
    }   
}

danpost danpost

2018/6/12

#
Is the Backboard object considered to be the Hoop? Side note: you should probably use direction conditions along with edge checking (only reverse direction if close to edge and currently moving toward that edge).
fabipfolix fabipfolix

2018/6/12

#
danpost wrote...
Is the Backboard object considered to be the Hoop? Side note: you should probably use direction conditions along with edge checking (only reverse direction if close to edge and currently moving toward that edge).
The Backboard is one of 4 subclasses of the hoop. But it didn't worked when i tried
if (isTouching(Hoop.class)) 
But with Backboard.class it didn't worked as well. Edit: Now it's working, although I haven't changed anything.
You need to login to post a reply.