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

2020/5/3

How do I complete eatAppleOnCollision()?

Ahmed780 Ahmed780

2020/5/3

#
I am having trouble implementing appleColor attribute in bowl class. If a green apple is caught (appleColor attribute of Apple class is 1), you must increase the attribute greenApplesCounter by 1, remove the apple from Game. If a yellow apple is caught (appleColor attribute of Apple class is 2), you must increase the attribute yellowApplesCounter by 1, remove the apple from Game. Apple class ***************************************************************************************************** public class Apple extends SimulationActor { public int appleColor = 0; public Apple(int v_appleColor) { setRotation(Greenfoot.getRandomNumber(360)); if (v_appleColor == 1) { setImage("greenApple.png"); appleColor = 1; } else if (v_appleColor == 2) { setImage("yellowApple.png"); appleColor = 2; } } public void act() { super.act(); if (getY() > 630) { position.setY(position.getY() + 300.0); } } } Bowl class ***************************************************************************************************** public class Bowl extends SimulationActor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ public int greenApplesCounter = 0; public int yellowApplesCounter = 0; /** * */ public Bowl() { super(); alignWithVector( new Vector2D(0.0, 0.0)); acceleration = new Vector2D(0.0, 0.4); } /** * */ public void act() { super.act(); MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null) { Point2D mouseWindowPos = new Point2D(mouse.getX(), mouse.getY()); Point2D mouseWorldPos = windowToWorld(mouseWindowPos); position.setX(mouseWorldPos.getX()); } eatAppleOnCollision(); } /** * */ public void eatAppleOnCollision() { Actor Apple = getOneIntersectingObject(Apple.class); SimulationWorld world = (SimulationWorld)getWorld(); List<Apple> apples = world.getObjects(Apple.class); if (Apple != null) { getWorld().removeObject(Apple); Greenfoot.playSound(" pop1.wav"); } } }
danpost danpost

2020/5/3

#
In your eatAppleOnCollision method, remove the lines beginning with "SimulationWorld" and "List<Apple>". In the if code block, add the following lines:
Apple apple = (Apple)Apple;
if (apple.appleColor == 1) greenAppleCounter++;
else if (apple.appleColor == 2) yellowAppleCounter++;
The :Apple apple: variable is require because the :int appleColor: field is in the Apple class and the:Actor Apple: variable will not look past the Actor class to find that field.
Ahmed780 Ahmed780

2020/5/3

#
Thank you so much!!! It works but when I play different sounds the game crashes. Is this correct? public void eatAppleOnCollision() { Actor Apple = getOneIntersectingObject(Apple.class); if (Apple != null) { Apple apple = (Apple)Apple; if (apple.appleColor == 1) greenApplesCounter++; getWorld().removeObject(Apple); Greenfoot.playSound("pop1.wav"); if (apple.appleColor == 2) yellowApplesCounter++; getWorld().removeObject(Apple); Greenfoot.playSound("pop2.wav"); } }
danpost danpost

2020/5/3

#
Ahmed780 wrote...
It works but when I play different sounds the game crashes.
There may be formatting issues with your sound files.
Is this correct? << Code Omitted >>
public void eatAppleOnCollision()
{
    Apple apple = (Apple)getOneIntersectingObject(Apple.class);
    if (apple == null) return;
    getWorld().removeObject(apple);
    int colorValue = apple.appleColor;
    if (colorValue == 1) greenApplesCounter++;
    if (colorValue == 2) yellowApplesCounter++;
    Greenfoot.playSound("pop"+colorValue+".wav");
}
Ahmed780 Ahmed780

2020/5/3

#
Thanks, It works now
Ahmed780 Ahmed780

2020/5/3

#
I need help with this part update act() method (of Score class) so that the score holds the total number of points earned (1 point if a green apple is caught, 3 points if a yellow apple is caught). public class Score extends Actor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ private double score = 0; public int greenApplesCounter = 0; public int yellowApplesCounter = 0; /** * Act - do whatever the Score wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor Apple = getOneIntersectingObject(Apple.class); Apple apple = (Apple)Apple; if (apple.appleColor == 1) { greenApplesCounter = greenApplesCounter + 1; score = greenApplesCounter; } if (apple.appleColor == 2) { yellowApplesCounter = yellowApplesCounter + 3; score = greenApplesCounter; } setImage( new GreenfootImage("Time(sec.): " + CatchWorld.currentGameTime + "\n\n Score: " + (int)score + " ", 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW)); } /** * */ public double getScore() { return score; } }
danpost danpost

2020/5/3

#
Having a score field here is a bit redundant as you could use for your getScore method:
public int getScore()
{
    return greenAppleCounter + 3*yellowAppleCounter;
}
and replace "(int)score" in the setImage line with "getScore()".
Ahmed780 Ahmed780

2020/5/3

#
It's not working, getScore method says <identifier> expected Do I remove anything else? public class Score extends Actor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ //private double score = 0; public int greenApplesCounter = 0; public int yellowApplesCounter = 0; /** * Act - do whatever the Score wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor Apple = getOneIntersectingObject(Apple.class); Apple apple = (Apple)Apple; if (Apple != null) { if (apple.appleColor == 1) { greenApplesCounter++; score = greenApplesCounter; return; } if (apple.appleColor == 2) { yellowApplesCounter = yellowApplesCounter + 3; //score = yellowApplesCounter; //return; } } setImage( new GreenfootImage("Time(sec.): " + CatchWorld.currentGameTime + "\n\n Score: " + getScore() + " ", 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW)); } /** * */ public double ()getScore { return score; return greenAppleCounter + 3*yellowAppleCounter; } }
danpost danpost

2020/5/3

#
Replace the entire last method (starting with 'public double ()getScore') with the method I gave above.
Ahmed780 Ahmed780

2020/5/3

#
It still doesn't work. The score changes automatically without eating apples. import lang.stride.*; import greenfoot.*; import java.util.*; /** * Write a description of class Score here. * @author (your name) @version (a version number or a date) */ public class Score extends Actor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ //private double score = 0; public int greenApplesCounter = 0; public int yellowApplesCounter = 0; /** * Act - do whatever the Score wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor Apple = getOneIntersectingObject(Apple.class); Apple apple = (Apple)Apple; if (Apple != null) { if (apple.appleColor == 1) { greenApplesCounter++; //score = greenApplesCounter; return; } if (apple.appleColor == 2) { yellowApplesCounter = yellowApplesCounter + 3; //score = yellowApplesCounter; return; } } setImage( new GreenfootImage("Time(sec.): " + CatchWorld.currentGameTime + "\n\n Score: " + getScore() + " ", 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW)); } /** * */ public int getScore(){ return greenApplesCounter + 3*yellowApplesCounter; } }
danpost danpost

2020/5/4

#
I did not really take a close look at the act method. More than that, I did not know you were adding 3 to the yellow counter there; or that you were using an intersection of apple and score counter (which is totally not right). Complicating matter is that of displaying game time along with the scores. Another main issue is having counters for the same thing in multiple classes. I would replace the entire Score class with a simple actor class. Like:
public class SimpleActor extends greenfoot.Actor {}
Have your world create and keep a reference to an instance of this class:
private Actor panel = new SimpleActor();
Add a method to the world class to update its image:
public void updatePanel()
{
    String txt = "Time (sec.): "+currentGameTime+"\n\nScore: "+bowl.getScore();
    panel.setImage(new GreenfootImage(txtt, 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW);
}
Then, have the prepare method (or the world constructor, itself) add the panel into the world and update its image. Finally, the act method in your world should also update the panel each second (when game time changes seconds). In Bowl class, at end of eatAppleOnCollision method, add the following line:
((CatchWorld)getWorld()).updatePanel();
Ahmed780 Ahmed780

2020/5/4

#
I added "scoregreen,int scoreyellow and score" and it worked. thanks for your help. Do you mind if I ask more questions? public class Score extends Actor { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ private double score = 0; /** * Act - do whatever the Score 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.*/ SimulationWorld world = (SimulationWorld)getWorld(); List<Bowl> bowls = world.getObjects(Bowl.class); if (bowls.size() <= 0) { return; } Bowl bowl = world.getObjects(Bowl.class).get(0); if (bowl != null) { /* score is set to current real position of the bowl (in space)*/ int scoregreen = bowl.greenApplesCounter * 1; int scoreyellow = bowl.yellowApplesCounter * 3; score = scoregreen + scoreyellow; } setImage( new GreenfootImage("Time(sec.): " + CatchWorld.currentGameTime + "\n\n Score: " + (int)score + " ", 22, Color.WHITE, Color.DARK_GRAY, Color.YELLOW)); } /** * */ public double getScore() { return score; } }
You need to login to post a reply.