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

2020/4/13

I want to add score when my ball touch left edge but the code doesnot run when i add if(getX() == 0) { score++; } . if i donot write, it counts. Can anyone help me?

JohnBis JohnBis

2020/4/13

#
public class Counter extends Actor { int score =0; public Counter() { setImage(new GreenfootImage("Team A Score :" +score, 24, Color.BLUE, Color.BLACK)); } public void act() { setImage(new GreenfootImage("Team A Score :" +score, 24, Color.BLUE, Color.BLACK)); } public void addScore() { if(getX() == 0) { score++; } } }
danpost danpost

2020/4/13

#
JohnBis wrote...
when my ball touch left edge
I doubt the counter will ever touch the left edge. Using getX in the Counter class gets the x-coordinate of the counter. Check for ball hitting an edge needs to be done in the class of the ball.
JohnBis JohnBis

2020/4/13

#
Thank you very much for the help. Now, it is working perfectly.
You need to login to post a reply.