i used rocketseaten
but it no longer works
because is used to have 1 ball now i have more balls
i probaly need to transfer the code to a higher level but how ??
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class balltest here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends SmoothMover
{
// First we set up our velocity variables - these basically control horizontal and vertical speed
// Horizontal velocity is set to random, and vertical starts at zero.
private int xVel=Greenfoot.getRandomNumber(10)-5;
private int yVel=0;
private int RocketsEaten;
private int life = 30;
private int damage = 16;
private int stability;
private int stNumber;
// this variable is used to control whether or not we actually check for collisions (see below
private int collisionTimer=0;
/**
* This is the main game loop for the ball
* Basically, it handles acceleration (up to a maximum speed), and then checks for collisions
* before moving the ball. Technically the screen bounce code should really be in the checkCollisions()
* method.
*/
public void act()
{
stuiter();
lookForRocket();
lookForRocket2();
}
private void stuiter()
{
// First of allm, increase vertical speed by 1. This is similar to the effect of gravity
// (constand downward acceleration)
yVel++;
// if our vertical speed hits 25, it can't go any higher
if (yVel>25)
{
yVel=25;
}
// Next up is the collision checking. It's set so that after every collision, there are ten
// game 'ticks' during which time the ball does not check for collisions. This helps to
// eliminate situations with actors getting stuck in a continual bouncing cycle
if (collisionTimer==0){
checkCollisions();
/**
checkCollisions2();
*/
}
// if a collision has occurred within the last 10 ticks, we just increment the timer.
// if the timer reaches 10, we reset it to 0 to show that the ball's ready toc heck collisions again.
else {
collisionTimer++;
if(collisionTimer>10){
collisionTimer=0;
}
}
//call the move method with given x and y velocity
move(xVel, yVel);
// This basically checks to see if we've hit the floor or the sides of the screen and bounces accordingly.
if (getY()>getWorld().getHeight()-2){
yVel=-yVel-6;
}
if (getX()>getWorld().getWidth()-2 || getX()<2 ){
xVel=-xVel;
}
}
/**
* The move method is very simple, it merely sets the actor's location according to xVel and yVel.
*/
private void move(int xVel, int yVel)
{
setLocation(getX()+xVel,getY()+yVel);
}
/**
* This method handles the collisions with other balls
*/
private void checkCollisions()
{
//we start by getting a reference to the first colliding object
Actor collidingActor = getOneIntersectingObject(Obstacle.class);
// next we check to see if a collision is actually taking place
if (collidingActor != null){
// the next condition checks a couple of things. Firstly, it checks if the ball we're colliding with
// is to the left of us and we are travelling to the left OR if it's to the right of us and
// we're travelling to the right. If we are, then chances are we've got a horizontal collision.
if ((collidingActor.getX()<getX() && xVel <0) || (collidingActor.getX()>getX() && xVel > 0)){
//so we simply invert the x velocity and set the collision timer going so we don't check
// for collisions in the next 10 ticks
xVel=-xVel;
collisionTimer++;
}
// the next condition is similar to the first, but it checks if the ball we're colliding with
// is to the above us and we are travelling up OR if it's below us and we're travelling down.
// If we are, then chances are we've got a vertical collision.
if ((collidingActor.getY()<getY() && yVel <0) || (collidingActor.getY()>getY() && yVel >0)){
// so we invert the Y velocity, and set the timer going.
yVel=-yVel;
collisionTimer++;
}
}
}
/**
* This method handles the collisions with other balls
private void checkCollisions2()
{
//we start by getting a reference to the first colliding object
Actor collidingActor = getOneIntersectingObject(Obstacle.class);
// next we check to see if a collision is actually taking place
if (collidingActor != null){
// the next condition checks a couple of things. Firstly, it checks if the ball we're colliding with
// is to the left of us and we are travelling to the left OR if it's to the right of us and
// we're travelling to the right. If we are, then chances are we've got a horizontal collision.
if ((collidingActor.getX()<getX() && xVel <0) || (collidingActor.getX()>getX() && xVel > 0)){
//so we simply invert the x velocity and set the collision timer going so we don't check
// for collisions in the next 10 ticks
xVel=-xVel;
collisionTimer++;
}
// the next condition is similar to the first, but it checks if the ball we're colliding with
// is to the above us and we are travelling up OR if it's below us and we're travelling down.
// If we are, then chances are we've got a vertical collision.
if ((collidingActor.getY()<getY() && yVel <0) || (collidingActor.getY()>getY() && yVel >0)){
// so we invert the Y velocity, and set the timer going.
yVel=-yVel;
collisionTimer++;
}
}
}
*/
/** hierna komt code die zelf is ingebracht dit moet zeker blijven staan
*/
private void lookForRocket()
{
if ( canSee(Rocket.class) )
{
eat(Rocket.class);
/**
Greenfoot.playSound("slurp.wav");
*/
RocketsEaten = RocketsEaten + 1;
if (RocketsEaten == 2)
{
/**
Greenfoot.playSound("fanfare.wav");
*/
setLocation(400, 300);
getWorld().addObject(new Gameover(), getX(), getY());
}
}
}
private void lookForRocket2()
{
if ( canSee(Rocket2.class) )
{
eat(Rocket2.class);
/**
Greenfoot.playSound("slurp.wav");
*/
RocketsEaten = RocketsEaten + 1;
if (RocketsEaten == 2)
{
/**
Greenfoot.playSound("fanfare.wav");
*/
setLocation(400, 300);
getWorld().addObject(new Gameover(), getX(), getY());
}
}
}
}