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

2012/3/5

Image Tracking...

Omniscience Omniscience

2012/3/5

#
Basically, I need to keep track of what image my Lives.class is, to induce some effect (a temporary decrease in world speed, in case you wanted to know). There is only ever one Lives.class inserted into the world at any given point in time. Below is the code (called within the World's Act statement) is syntactically correct but has some logic error in it that I can't see, because there is no induced effect:
public void damageControl() {
        damageCount = worldCount + 2;
        Lives lives = (Lives) (getObjects(Lives.class).get(0));
        GreenfootImage twoLives = new GreenfootImage("2 Lives.png");
        GreenfootImage oneLife = new GreenfootImage("Last Life.png");
        GreenfootImage Health = new GreenfootImage(lives.getImage());
        if (Health ==  twoLives  || Health == oneLife && worldCount == damageCount) {
            originSpeed = speedIncrease; 
            slowDownRevert = worldCount + 20;
            Greenfoot.setSpeed(40);
        }
        if (worldCount == slowDownRevert) { 
            Greenfoot.setSpeed(45+originSpeed);
            chance -= 5;
            addObstacle -= 2;
            addLimit += 2;//when hit, obstacles become more frequent
        }
    }
As usual, I'm extremely grateful for any and all help on the matter. Thanks in advance!
davmac davmac

2012/3/5

#
Ok, first, a correction in terminology: You want to keep track of what image your Live instance has, not the Live class itself. There are a few issues with the code itself. One problem is that you're creating new instances of the images (twoLives and oneLife) each time the damageControl() method is called, and you're using reference equality (the '==' operator) to check that the image instance (Health) is either of the new objects. It can't be the same, because you have just created the objects referred to by oneLine and twoLives. Even though the images would look the same, they are not the same image - they are new objects. You could change line 07 to use the equals(...) method instead: if (Health.equals(twoLives) || Health.equals(oneLife) && worldCount == damageCount) { This might work, but it relies on equality being defined for images, and it's likely to be inefficient (to compare two images, it'd basically have to compare every pixel in one image with its counterpart in the other image). What would be better would be to maintain a separate instance variable in the Lives instance, perhaps an int, which determines which image the Lives is currently displaying. You possibly do this already. For instance, if the number of lives left is equal to 2, then the image is the "2 Lives.png" image, whereas if it's 1, then the image is the "Last life.png" image. I.e. replace 06 with: int health = lives.getLivesCount(); Line 07 becomes: if (health == 2 || health == 1 && worldCount == damageCount) { You could then remove lines 4 and 5, but you'd need to add an appropriate 'getLivesCount' method (an instance method) to the Lives class.
Omniscience Omniscience

2012/3/5

#
Thanks davmac, again incredibly helpful. I do appreciate the corrections in the terminology; it definitely will help me in an exam :) ... On careful thought, I think I will opt for your first possible solution, seeing as the images are very small, so the inefficiency itself, for me, will be near to negligible. EDIT: ah, it didn't work! Well, lets try the seemingly long-winded alternative...
You need to login to post a reply.