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

2013/5/18

Counter Problems

1
2
danpost danpost

2013/5/18

#
Then just remove '.getValue()'.
Hawx_ Hawx_

2013/5/19

#
//This is the code in the car at the moment:
if(((Road) getWorld()).timesTouched == 2);
        ((Road) getWorld()).addObject(this, Greenfoot.getRandomNumber(600), 1);

//Any reason why it's not working?
Hawx_ Hawx_

2013/5/19

#
This is the code in the World , but it's still not working,
if(timesTouched == 2) {   
        addObject(new Car(), Greenfoot.getRandomNumber(600), 1); 
}
        
It adds the car when the counter is at zero , when I put this:
if(timesTouched == 2) ;   
        addObject(new Car(), Greenfoot.getRandomNumber(600), 1); 
But when I want it so that , the car gets added when the counter is equal to 2 it doesn't work.
danpost danpost

2013/5/19

#
Do you have any code that changes the value of 'timesTouched'? Have you stopped the scenario at a point where you believe that a new car should have been created and inspected the 'timesTouched' value to see if it actually has a value of two?
Hawx_ Hawx_

2013/5/19

#
if(getOneIntersectingObject(Biker.class) != null){ 
            
            {
                ((Road) getWorld()).timesTouched+=1;
                setLocation(Greenfoot.getRandomNumber(400),1);


            }
    }  
danpost danpost

2013/5/19

#
Are you actually wanting to place a second car object in the world, or are you trying to move the one car object back to the top edge of the world? That last code snippet you provided (the intersecting biker code), was that in your Car class, also?
Hawx_ Hawx_

2013/5/19

#
Add the Car to the world when the counter gets to a certain value.
danpost danpost

2013/5/19

#
Hawx_ wrote...
Add the Car to the world when the counter gets to a certain value.
That gave me no additional information and did not answer either of my questions.
Hawx_ Hawx_

2013/5/19

#
Last snippet was in an actor that added to the timesTouched int and'm trying to place a car object in the world that's not already there.
danpost danpost

2013/5/19

#
Your responses seem quite cryptic; there must some type of language barrier at work here. I do think I know what you are trying to convey, however. "Last snippet was in an actor (not the Car class) that added to the timesTouched int (which I already knew) and'm trying to place a car object in the world that's not already there (the world IS there, the car is not yet there)." You should put the code to add the car directly after incrementing the counter, as follows:
if (getOneIntersectingObject(Biker.class) != null) { 
    ((Road)getWorld()).timesTouched += 1;
    if (((Road)getWorld()).timesTouched == 2)
        addObject(new Car(), Greenfoot.getRandomNumber(600), 1);
    setLocation(Greenfoot.getRandomNumber(400), 1);
}
Hawx_ Hawx_

2013/5/19

#
Thanks, works perfectly. Sorry I didn't really know how to convey what I was saying. But it seems you knew what you were doing anyway. So thanks.
You need to login to post a reply.
1
2