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

2016/4/13

SCORE COUNTER

LYNX LYNX

2016/4/13

#
How i i make my code so if i touch an actor by my controlled actor my score goes up by 1
DogeOverlord DogeOverlord

2016/4/14

#
if(isTouching(OtherActor.class))
{
score++;
}
You'll have to replace OtherActor with the actor you are touching and score with whatever the variable is that holds the score.
limingbogf limingbogf

2016/4/14

#
define an variable ,int score. if(isTouching(Actor.class)){ score++; } //the Actor is your controlled actor's name
danpost danpost

2016/4/14

#
@DogeOverlord, the code you supplied will only work if you also use 'removeTouching' on the actor. If the touched actor is not removed, then the score will increment repeatedly until the actors are no longer touching each other. @limingbogf, you might be confused as to where the snippet of code is located. The original question was put in a way to infer that the code was in the class of the controlled Actor -- not in the one the it will be touching.
danpost danpost

2016/4/14

#
If the touched actor is not to be removed from the world, then you will a field to track the state of touching. Cannot explain immediately. Will have to edit later with details.
danpost danpost

2016/4/14

#
If not removing the touched actor, an instance field to track the touching state is needed:
// instance field
private boolean touchingScorer;

// in act or method it calls
if (!touchingScorer && isTouching(Scorer.class)
{
    touchingScorer = true;
    score++;
}
if (touchingScorer && !isTouching(Scorer.class)) touchingScorer = false;
Adding the tracking field allows you to detect when the state changes instead of just knowing the current state. Of course, your touched actor is not of the Scorer class and you should adjust the name to suit your class.
You need to login to post a reply.