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

2016/10/19

ball

Nosson1459 Nosson1459

2016/10/19

#
y doesnt this work
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Obstacle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Obstacle extends Actor
{
    private Ball ball;
    public boolean bottom=false;
    public boolean top=false;
    public boolean rightSide=false;
    public boolean leftSide=false;
    /**
     * Act - do whatever the Obstacle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Board board=(Board)getWorld();
        checkBall();
        booleans();
    }  
    private void checkBall()
    {
        Board board=(Board)getWorld();
        if(bottom==true||top==true)
        {
            if(board.bounce==false)
            {
               board.deltaX=-board.deltaX;
            }   
        }
        if(rightSide==true||leftSide==true)
        {
            if(board.bounce==false)
            {
               board.deltaY=-board.deltaY;
            }   
        }
    }
    private void booleans()
    {
        bottom1();
        bottom2();
        bottom3();
        bottom4();
        bottom5();
        top1();
        top2();
        top3();
        top4();
        top5();
    }
    private boolean bottom1()
    {
        Object under=getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean bottom2()
    {
        Object under=getOneObjectAtOffset(-getImage().getWidth()/2+20,getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean bottom3()
    {
        Object under=getOneObjectAtOffset(0,getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean bottom4()
    {
        Object under=getOneObjectAtOffset(getImage().getWidth()/2-20,getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean bottom5()
    {
        Object under=getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private void bottom()
    {
        if(bottom1())
        {
            bottom=true;
        }
        
        
    }
    private boolean top1()
    {
        Object under=getOneObjectAtOffset(-getImage().getWidth()/2+1,-getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean top2()
    {
        Object under=getOneObjectAtOffset(-getImage().getWidth()/2+20,-getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean top3()
    {
        Object under=getOneObjectAtOffset(0,-getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean top4()
    {
        Object under=getOneObjectAtOffset(getImage().getWidth()/2-20,-getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private boolean top5()
    {
        Object under=getOneObjectAtOffset(getImage().getWidth()/2-1,-getImage().getWidth()/2,Ball.class);
        return under!=null;
    }
    private void top()
    {
        if(top1()||top2()||top3()||top4()||top5())
        {
            top=true;
        }
        else
        {
            top=false;
        }
    }
Super_Hippo Super_Hippo

2016/10/19

#
... Be specific. What should work, what is happening, does it compile?
danpost danpost

2016/10/19

#
It seems strange to have 'getWidth' being called after the comma in the 'getOneObjectAtOffset' parameter lists. Also, the 'booleans' method is useless. It is like saying 'true; false; false; false; true; false;'. The returned values from all the methods called within it are not doing anything -- they are not being saved for later use and not being used immediately (as a condition to do something). Again, seems like a lot of work for naught. It is bulky, clumsy, not well written and evidently, does not work. Collision detecting is not that hard. You just need to hold the movement offsets in variables and keep vertical and horizontal movement separate so you know which movement caused collision; the direction of that movement tells you which side was hit.
Nosson1459 Nosson1459

2016/10/20

#
even if something is moving in a certain direction it still doesnt tell which side of something its gonna hit
danpost danpost

2016/10/20

#
Nosson1459 wrote...
even if something is moving in a certain direction it still doesnt tell which side of something its gonna hit
Not just something -- but the actor that is being hit. After it moves, check for intersecting object(s); if the actor moved right and an intersection occurred, then the object hitting it was on the right and hit the right side; same for the other directions.
Nosson1459 Nosson1459

2016/10/20

#
wat about going at an angle of up 2 da right it cud b da bottom or da left
danpost danpost

2016/10/20

#
Nosson1459 wrote...
wat about going at an angle of up 2 da right it cud b da bottom or da left
Oh -- wait; you have the collision checking in the Obstacle class. You should have it in your Ball class, looking for obstacles. But, I am curious as to why the 'deltaX' and 'deltaY' fields are in your world class. Are not those values belonging to the Ball objects?
Nosson1459 Nosson1459

2016/10/20

#
if i hit the side of an obstacle i want to reverse the x but not the y if i hit the bottom i want to reverse the y but not the x if i put it in the ball i wont b able to check if i hit the side or the bottom/top so i put the deltaX and deltaY in the world so i could call them in both ball and obstacle
danpost danpost

2016/10/20

#
Nosson1459 wrote...
if i hit the side of an obstacle i want to reverse the x but not the y if i hit the bottom i want to reverse the y but not the x if i put it in the ball i wont b able to check if i hit the side or the bottom/top so i put the deltaX and deltaY in the world so i could call them in both ball and obstacle
Put everything in the Ball class; the fields and the collision checking. Basically, it would be like this:
// with fields
private int deltaX = 1+Greenfoot.getRandomNumber(3); // horizontal speed
private int deltaY = Greenfoot.getRandomNumber(3);  // vertical speed

// first move horizontally
setLocation(getX()+deltaX, getY());
if (!getIntersectingObjects(Obstacle.class).isEmpty())
{
    deltaX = -deltaX;
    setLocation(getX()+deltaX, getY());

}
// then move vertically
setLocation(getX(), getY()+deltaY);
if (!getIntersectingObjects(Obstacle.class).isEmpty())
{
    deltaY = -deltaY;
    setLocation(getX(), getY()+deltaY);
}
Nosson1459 Nosson1459

2016/10/20

#
wats isEmpty()
danpost danpost

2016/10/20

#
Nosson1459 wrote...
wats isEmpty()
Same as 'size() == 0'. The list exists, but there is nothing in it (no intersecting objects, in this case).
You need to login to post a reply.