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

2013/5/18

Counter Problems

1
2
Hawx_ Hawx_

2013/5/18

#
How do you make it so that once a counter gets to a certain number , a actor(that's not already in the scene) gets added to the world. I've been playing around but I can't seem to get the code right. Thanks, Hawx_
Hawx_ Hawx_

2013/5/18

#
Any help?
danpost danpost

2013/5/18

#
Usually a counter class will have a method to retrieve the value of the counter called 'getValue'. Just add a check to see if the counter is that certain number and add the object when it is.
if(counter.getValue() == targetNumber) getWorld().addObject(new ActorA(), locationX, locationY);
If you do not have a reference to the counter use '((Counter)getWorld().getObjects(Counter.class).get(0)).getValue()'.
Hawx_ Hawx_

2013/5/18

#
Thanks Danpost
Hawx_ Hawx_

2013/5/18

#
It's saying at the "getvalue()" , "int cannot be dereferenced". Any reason why?
danpost danpost

2013/5/18

#
Please show the code as you are trying to use it.
Hawx_ Hawx_

2013/5/18

#
The Counter isn't it's own class, it's set up in Road( World). So I've put the code into the world, script like this:
if(timesTouched.getValue() == 2) 
        addObject(new Car(), Greenfoot.getRandomNumber(600), 1);  
Hawx_ Hawx_

2013/5/18

#
inside the prepare method
danpost danpost

2013/5/18

#
Hawx_ wrote...
inside the prepare method
It will not work in the prepare method. You need it in the 'act' method; but, you will also need another Boolean to regulate the creation of new Car objects (or reset the value of 'timesTouched' back to zero).
Hawx_ Hawx_

2013/5/18

#
Thanks
Hawx_ Hawx_

2013/5/18

#
I've put it in the World act method and it's still saying "int cannot be dereferenced"
Hawx_ Hawx_

2013/5/18

#
is there any other way to script "getValue()"?, looking through the API I can't see it.
danpost danpost

2013/5/18

#
What type of object (or primitive) does the variable 'timesTouched' hold?
Hawx_ Hawx_

2013/5/18

#
It's an int , if that's what your asking .
Hawx_ Hawx_

2013/5/18

#
int timesTouched = 0;
There are more replies on the next page.
1
2