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

2016/5/25

How can I stop a counter from counting once it has reached a certain number?

Sonarwave Sonarwave

2016/5/25

#
Hey everyone, I am trying to make an object change images after getting touched by a different object. I have 7 images, and each time the object is touched the object changes from image1.png to image2.png until it reaches image7.png. The only problem is that if it is touched again, the greenfoot game comes up with an error saying it could not find an 8th image, but I want it to stop at the 7th image. Is it possible to get the counter to stop counting after it has reached the 7th image? ~sonarwave
danpost danpost

2016/5/25

#
You will need to show the code of the class with this counter and the animation.
Sonarwave Sonarwave

2016/5/25

#
No problem, here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boundry1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boundry1 extends Actor
{
    int iHurt = 1;
    /**
     * Act - do whatever the Boundry1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage("boundry"+iHurt+".png");
        if(isTouching(Bullet.class)){
         removeTouching(Bullet.class);
         iHurt++;
        }
    }    
}
Sonarwave Sonarwave

2016/5/25

#
The object is named "boundry", which is just a building object. It has a set of 7 images, and each time a bullet hits it ticks over to the next image. If it reaches the 7th image and gets hit by a bullet again, greenfoot stops and there is an error saying "cannot find boundry8.png" (the 8th image greenfoot is looking for).
SPower SPower

2016/5/25

#
You need to simply add one little thing after you increase iHurt by one, though what depends on what you want it to do once it has reached image7. If you want it to go back to image1, add this after iHurt++:
if (iHurt > 7) {
    iHurt = 1;
}
If you want it to remain at image7, simply say:
if (iHurt > 7) {
    iHurt = 7;
}
Alternatively, if you want it to stop at image7 and not go back, you could also do:
if (isTouching(Bullet.class) && iHurt < 7) {
    removeTouching(Bullet.class);
    iHurt++;
}
danpost danpost

2016/5/25

#
I would simply just not update the image if the value of 'iHurt' was greater than seven. Also, I would do this only when the value of 'iHurt' was changed. It is CPU wasteful to set the image every act cycle. Add the following constructor (unless the "boundry1.png" image is set as the default image for objects of the class):
public Boundry1()
{
    setImage("boundry1.png");
}
Then, the touching code would be:
if (isTouching(Bullet.class)
{
    removeTouching(Bullet.class);
    iHurt++;
    if (iHurt < 8) setImage("boundry"+iHurt+".png");
}
Sonarwave Sonarwave

2016/5/26

#
Hey guys, I'm sorry to say that the code here didn't work :( but on the bright side, a friend of mine helped out and it is all fixed. thanks for everyone's efforts in helping :D
You need to login to post a reply.