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

2013/7/4

event happening once

davemib123 davemib123

2013/7/4

#
Hi, a little stuck here. I want a particular event to happen only once. When my hero touches a flag I want 100 points given only once and not multiple times if the user passes through it. i've tried oneintersectingobject and oneobjectatoffset but they work on multiple tries. Any ideas?
Have a boolean that determines if the player has touched it this level. Then, when the player touches the flag, turn it to true, and prevent the player from getting any points if the boolean is true. Something a little like this:
//Instance variables
private boolean hasTouchedFlag
...

//method for getting points from flag
{
    if (getOneIntersectingObject(Flag.class) != null)
    {
        if (!hasTouchedFlag)
        methodForGainingPoints(100); //Just a guess
        hasTouchedFlag = true;
    }
}
Or better:
//method for getting points from flag
if (getOneIntersectingObject(Flag.class != null) && !hasTouchedFlag)
    methodForGainingPoints(100);
hasTouchedFlag = true;
davemib123 davemib123

2013/7/4

#
great, i'll give it a try :)
davemib123 davemib123

2013/7/5

#
FlyingRabidUnicornPig it worked great. I'm trying to get the flag to come down once the hero touches the flagpole. How do I call the Flag when I added it in my World class as:
 addObject(new BgItem(13), 461, 170); //flag
addObject(new BgItem(12), 489, 289); //pole
For reference these are the snippets relating to the flag and pole in the BgItem class:
    
private boolean hasTouchedFlag = false; 
private final GreenfootImage Flag1 = new GreenfootImage("flag1.gif");
    private final GreenfootImage Flag2 = new GreenfootImage("flag2.gif");

 public BgItem(int selection)
    {
        this.selection = selection; 

        if (selection == 12) {  
            setImage(Flag1);       
            Flag1.scale(28,350);
        } 
        else if (selection == 13) {  
            setImage(Flag2);       
            Flag2.scale(50,50);
        } 
    } 


 public void act()
    {
        if (selection == 12) {  
            touchedFlag();
        }
    }

 public void touchedFlag()
    {
        if (getOneIntersectingObject(Mario.class) != null && !hasTouchedFlag)  
        {  
            Levels L = (Levels)getWorld();
            Counter scoreCounter = L.getScoreCounter();
            scoreCounter.add(100);
            hasTouchedFlag = true; 
        } 
    }
danpost danpost

2013/7/5

#
Why would you need to 'call the Flag'? Just add the code in the act method of the BgItem class.
// inserted at line 25 above
if (selection == 13 && hasTouchedFlag && getY() < 264) {
    setLocation(getX(), getY()+1);
}
davemib123 davemib123

2013/7/5

#
danpost I tried your suggestion the flag doesnt move.
danpost danpost

2013/7/5

#
I think I had the line number wrong. The act method should be like this:
public void act()
{
    if (selection == 12) {  
        touchedFlag();
    }
    if (selection == 13 && hasTouchedFlag && getY() < 264) {
        setLocation(getX(), getY()+1);
    }
}
davemib123 davemib123

2013/7/5

#
that's what i tried. Source code is available to look at: http://www.greenfoot.org/scenarios/8881
danpost danpost

2013/7/5

#
I see what is happening. Setting 'hasTouchedFlag' for the pole object does not set the flag object value of 'hasTouchedFlag'. You can however, add the 'static' keyword to the 'hasTouchedFlag' field declaration statement so that all BgItem objects share that value (instead of each having its own value for the field). You may want to make sure it is set to 'false' in the constructor of a BgItem object (either of selection 12 or 13).
danpost danpost

2013/7/5

#
Change the limit of the falling flag to 438.
davemib123 davemib123

2013/7/5

#
ace that works, just as expected.
You need to login to post a reply.