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

Discussions

You need to login to take part
Rss

Current Discussions

Tomc84

Random # and chance problems

By Tomc84, with no replies.
I am suppose to pick a # between 1-4 for my vertical speed at random with a 50% chance to go up or down. Same with my left and right and I have no clue what I'm doing wrong... PLZ I need help :( 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 { private int ySpeed; private int xSpeed; private int wait; private int random; public Ball() { ySpeed = 3; //do not really need constructor call xSpeed = 3; wait = 80; random = Greenfoot.getRandomNumber(3)+1; } /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //ballMove(); //bounceOffPaddle(); bouncOffWalls(); if (wait == 0) { ballMove(); } else { wait --; } } // public void ballMove() // { // setLocation(getX() +xSpeed, getY() +ySpeed); // } /** * Move the ball */ public void ballMove() // NEW MOVE METHOD { if (getY() == 0 || getY() == 500-1) { ((PongWorld)getWorld()).updateScore(this); setLocation( 350, 250 ); } else { //setLocation (getX(Greenfoot.getRandomNumber(3)+1), getY(Greenfoot.getRandomNumber(3)+1)); //Greenfoot.getRandomNumber(random)<2); RandomMove(); setLocation(getX()+xSpeed,getY()+ySpeed); //movement bounceOffPaddle(); // new call for bounceOffPaddle } } public void RandomMove() { if (Greenfoot.getRandomNumber(random)<2); { setRotation(Greenfoot.getRandomNumber(360)); } } public void bounceOffPaddle() { Paddle paddle = (Paddle) (getOneObjectAtOffset(0, 0, Paddle.class)); // tells ball paddle is here if (paddle != null) { ySpeed = ySpeed * (-1); } } public void bouncOffWalls() { if(getX() == 0 || getX() == 700-1) // use of logic or to condense two if statements { xSpeed = xSpeed * (-1); } // else ...........WHY NOT USE AN ELSE???? // { // xSpeed = xSpeed * (-1); // } } // public void bouncOffWalls() CAN BE CONDENSED A SINGLE IF STATEMENT...... HOW THOUGH?????? // { // if(getX() == 0) // { // xSpeed = xSpeed +1; // } // if (getX() == 700-1) // { // xSpeed = xSpeed * (-1); // } // } }