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

2017/10/25

Way does my score increses whit more than one??

vbh vbh

2017/10/25

#
i have a gun that shoots balls whit bullets. When a bullet hits the ball the image changes, and the point score should go up whit one, showing in a scoreboard, but goes op whit a larger number, hope someone can see what im doing wrong. my scoreboard class public class ScoreBoard extends Actor { private static final String scoreLable = "Score: "; private GreenfootImage box; public ScoreBoard() { box = new GreenfootImage(60,30); box.setColor(new Color(255,255,255)); box.fillRect(0,0,100,30); updateStatus(0); } public void updateStatus(int pinkScore) { GreenfootImage img = new GreenfootImage(box); img.setColor(Color.YELLOW); img.drawString(scoreLable + pinkScore, 5,12); setImage(img); } my world class public class MyWorld extends World { private int pinkScore = 0; private ScoreBoard pinkScoreBoard; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); addGun(); pinkScoreBoard = new ScoreBoard(); addObject(pinkScoreBoard,30,385); pinkScoreBoard.updateStatus(pinkScore); } public void increasePinkScore() { pinkScore = pinkScore+1; pinkScoreBoard.updateStatus(pinkScore); } public int getPinkScore() { return pinkScore; } } my ball class public void hitPinkBall() { Actor actor = getOneIntersectingObject (Bullet.class); if (actor != null) { MyWorld myWorld1 = (MyWorld)getWorld(); myWorld1.increasePinkScore(); setImage("pinkSplach.png"); setRotation(90); }
danpost danpost

2017/10/25

#
Since both the ball and the bullet are not removed upon intersecting, they remain intersecting for multiple act steps. That is why the score is "jumping" (or increasing by one, multiple times, rapidly).
vbh vbh

2017/10/27

#
Thanks for reply i see what you mean, im still trying to find a way to solve the problem, but im getting more greay hair than solutions :-).
danpost danpost

2017/10/27

#
vbh wrote...
im still trying to find a way to solve the problem, but im getting more greay hair than solutions :-).
Ask yourself -- what is different about the act step when intersection first occurs that is different from the rest of the steps that intersection occurs? The answer is hiding in plain site -- right within the question ;) the fact that it is the first act step when intersection occurs. What makes it the first? -- the fact that the objects were not intersecting on the last act step. So, if you track the intersecting state, you can determine when the first act step that intersection occurs on (by comparing the last intersecting state with the next determined one).
vbh vbh

2017/10/30

#
thanks for reply im still getting more and more greay hair,:-D i have tryed to remove the bullet after intersecting, but i did't do the trick, and im not sure how to get the state of the first intersecting and how to save it. I dont like to give up, but if you know the code that can do the trick, it will be much appreciated
danpost danpost

2017/10/30

#
vbh wrote...
have tryed to remove the bullet after intersecting, but i did't do the trick, and im not sure how to get the state of the first intersecting and how to save it.
It would be something like this: Add an instance field (outside method):
1
private boolean touchingBullet;
Then when checking for bullet (inside method):
1
2
3
4
5
6
7
8
Actor actor = getIntersectingObject(Bullet.class); // get any bullet intersector
boolean bulletTouching = (actor != null); // get new intersecting state
if (touchingBullet != bulletTouching) // is there a change in state of touching a bullet
{
    touchingBullet = bulletTouching; // state changed
    if (touchingBullet) // is state changed to touching (from not touching)
    {
        etc.
vbh vbh

2017/10/31

#
ty very much its working :-) i dont think i would have figured that out myself
You need to login to post a reply.