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

2019/6/13

I NEED HELP!

1
2
coder123 coder123

2019/6/13

#
I wanted to make my ball bounce off the walls, bricks, and the paddle, but I have no idea how to do it.I have only used Greenfoot for 3 days and I don't know how. this is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
{
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int delaytime = 150;
    public void act() 
    {    
        turn(-45);
        move(1);
        turn(45);
    } 
    
}
Super_Hippo Super_Hippo

2019/6/13

#
Basically the same as this one: https://www.greenfoot.org/topics/62420/0 (I replied there first even though your message was before that one.) I am pretty sure this was also asked quite often in the past already. Maybe you could find your answer with the search function as well.
coder123 coder123

2019/6/13

#
Can you’ll a explain this post: To make it use a random direction at the beginning, set the rotation randomly in the constructor of the class. Then in its act method, just move straight, check if it is touching a wall and if so, change the rotation. This is usually done by splitting the X and Y movement, so you can decide where to turn after the intersection with a wall.
coder123 coder123

2019/6/13

#
I meant: can you pls explain the post
coder123 coder123

2019/6/13

#
here's my new code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
{
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public ball() {
        setRotation(-45);
    }
    public void act() 
    {    
        move (1);
    } 
    
}
coder123 coder123

2019/6/13

#
Now what I did was that I made a method in the paddle class to get the x coordinates of it, and i want to call it in the ball class. how would you do that?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
{
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public ball() {
        setRotation(-45);
    }
    public void act() 
    {    
        move (1);
        if (isAtEdge() == true  || isTouching(bricks.class) == true) {
            setRotation(getRotation() - 90);
        }
        if (isTouching(paddle.class) == true) {
            setRotation(getRotation() - 90);
        }
        if (isTouching(paddle.class)) {
            paddle a = new paddle();
            int x = a.getXOfPaddle();
            getWorld().removeObject(paddle());
            getWorld().addObject( new paddle(), x, 570);
        }
    } 
    
}
coder123 coder123

2019/6/13

#
this code doesn't compile
coder123 coder123

2019/6/13

#
here's the error message: 2019-06-13 10:38:55.533 java unrecognized type is 4294967295 2019-06-13 10:38:55.534 java *** Assertion failure in -, /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1671.50.111/AppKit.subproj/NSEvent.m:1977
Super_Hippo Super_Hippo

2019/6/13

#
I have no clue what that error message is. Maybe you want to get the coordinates of the paddle object which you are touching (for some reason) and not from a new paddle you just created (which doesn't have a location because it isn't in a world). A class doesn't have a location. The Actor class has the "getX" method so there shouldn't be the need of a "getXOfPaddle" method in the paddle class. A good start would be to import the SmoothMover class and let the Ball extend it. (Doesn't matter if it will always move in 45°, but if it will change direction, you will notice it.) Then move x and y separately.
private double[] direction;

public Ball() //classes should start with an uppercase letter
{
    direction = new double[] {1, -1}; //-45°
}

public void act()
{
    setLocation(getExactX()+direction[0], getExactY());
    if (isTouching(Paddle.class))
    {
        direction[0] *= -1;
    }
    setLocation(getExactX(), getExactY()+direction[1]);
    if (isTouching(Paddle.class))
    {
        direction[1] *= -1;
    }
}
This is a very easy example and will probably need some improvements.
coder123 coder123

2019/6/13

#
thanks i will try that
coder123 coder123

2019/6/13

#
that code doesn't compile either
Super_Hippo Super_Hippo

2019/6/13

#
Did you import the SmoothMover class and does the Ball extend it? If not, remove all "Exact" from the code and use "int" instead of "double".
coder123 coder123

2019/6/13

#
yes, I did
coder123 coder123

2019/6/13

#
never mind
coder123 coder123

2019/6/13

#
now i did
There are more replies on the next page.
1
2