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

2016/6/17

Basically trying to get a ball to be hit by a shooter and into a goal. Works once, would like to keep score and replace shooter and ball in world. Have two different versions, commented out lots.

MNastasi MNastasi

2016/6/17

#
import greenfoot.*; public class Ball extends Animal { Counter count; GreenfootImage img; int xSpeed; int ySpeed ; private int life ;; boolean shooterContact = false; boolean goalContact = false; boolean goalieContact = false; public Ball(Counter c) { img = getImage(); //scale image to proper size before running program img.scale(40,40); count = c; xSpeed = Greenfoot.getRandomNumber(20) - 10; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; } public void act() { //removeAtEdge(); //isTouchingGoal(); life--; if(life!=0) { setLocation(getX() + xSpeed, getY() - ySpeed); } if(life == 0) { Greenfoot.delay(10); getWorld().removeObject(this); if (Greenfoot.isKeyDown("space")) { xSpeed = Greenfoot.getRandomNumber(20) - 10; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; getWorld().addObject(new Ball(count), 311, 365); } //resets the program by adding all objects back in getWorld().addObject(new Ball(count), 311, 365); getWorld().addObject(new Shooter(), 540, 321); } Actor goal = getOneIntersectingObject(Goal.class); if(goal != null) { count.add(1); getWorld().removeObject(this); //getWorld().removeObject(Shooter.class); if ("space".equals(Greenfoot.getKey())) { xSpeed = Greenfoot.getRandomNumber(20) - 10; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; getWorld().addObject(new Ball(count), 311, 365); getWorld().addObject(new Shooter(), 540, 321); } //isTouchingShooter(); //isTouchingGoalie(); /*if(atWorldEdge()) { //removes ball when at the edge of the world getWorld().removeObject(this); if (Greenfoot.isKeyDown("space")) { xSpeed = Greenfoot.getRandomNumber(20) - 10; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; getWorld().addObject(new Ball(count), 311, 365); } }*/ } /*public boolean isTouchingGoal() { Actor goal = getOneIntersectingObject(Goal.class); if(goal != null) { count.add(-1); //getWorld().removeObject(this); goalContact = true; Greenfoot.playSound("boo.wav"); if(count.getValue() == -5) { Greenfoot.playSound("youLose.wav"); Greenfoot.stop(); } } return goalContact; }*/ /*public boolean isTouchingGoalie() { Actor goalie = getOneIntersectingObject(Goalie.class); Actor flippedGoalie = getOneIntersectingObject(FlippedGoalie.class); Actor singleGoalie = getOneIntersectingObject(SingleGoalie.class); if(goalie != null || flippedGoalie != null || singleGoalie != null) { count.add(1); getWorld().removeObject(this); goalieContact = true; Greenfoot.playSound("applause.wav"); if(count.getValue() == 5) { Greenfoot.playSound("celebration.wav"); Greenfoot.stop(); } } return goalieContact; } public boolean isTouchingShooter() { Actor shooter = getOneIntersectingObject(Shooter.class); if(shooter != null) { //ball moves in random direction shooterContact = true; } return shooterContact; } public void removeAtEdge() { if(atWorldEdge()) { //removes ball when at the edge of the world //getWorld().removeObject(this); getWorld().addObject(new Ball(count), 311, 365); } }*/ }}
danpost danpost

2016/6/17

#
Try, instead of removing and replacing the ball with a new one, to just reset the location of the current ball back to (311, 365) using:
setLocation(311, 365);
.
You need to login to post a reply.