Hello,
I am trying to fix some bugs that are happening in my game and was wondering if there is anyway to only change the ball once; so you can't change it again if it was already changed by player1 or player2.
----------------------------The code for the Player is-----------------------------------------------
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
enum PlayerMode {PLAYER1, PLAYER2 };
PlayerMode mode;
public Player(){
mode = PlayerMode.PLAYER1;
setImage("ant-with-food.png");
}
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if (Greenfoot.mouseMoved(null)){
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
}
if (Greenfoot.mouseClicked(null)){
GameBall ball = (GameBall)getOneIntersectingObject(GameBall.class);
if(ball!=null){
if (mode == PlayerMode.PLAYER1 ){
ball.setSteel();
setPlayer2();
}
else if (mode == PlayerMode.PLAYER2){
ball.setGold();
setPlayer1();
}
}
}
}
public void setPlayer1(){
mode = PlayerMode.PLAYER1;
setImage("ant-with-food.png");
}
public void setPlayer2(){
mode = PlayerMode.PLAYER2;
setImage("ant.png");
}
}
-----------------------------------AND THE CODE FOR THE GameBall-----------------------------
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class GameBall here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GameBall extends Actor
{
enum BallState { UNCLICKED, GOLD, STEEL };
BallState state = BallState.UNCLICKED;
GameBall(){
setImage("cell.jpg");
}
/**
* Act - do whatever the GameBall wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void setGold(){
setImage("gold-ball.png");
state=BallState.GOLD;
}
public void setSteel(){
setImage("steel-ball.png");
state = BallState.STEEL;
}
public void reset(){
setImage("cell.jpg");
state = BallState.UNCLICKED;
}
public boolean isUnclicked(){
return (state == BallState.UNCLICKED);
}
public boolean isGold(){
return (state==BallState.GOLD);
}
public boolean isSteel(){
return (state==BallState.STEEL);
}
public void setWinIfGold(){
if (state == BallState.GOLD){
setImage("ball.png");
}
}
public void setWinIfSteel(){
if ( state == BallState.STEEL){
setImage("ball.png");
}
}
}