I had a problem writing a game for As 3.0. How to make, that after a collision of an object with another object, the inscription 0 at the top increases each time by 1, and the object itself changes its position? The object changes position, but I do not quite understand how to make the number written at the top of the program increase by 1? Tell me please. I'm new, so don't kick it, just explain how to do it right.

    1 answer 1

    For a text field with "0", specify a name, for example: scores

    In the code, in the collision (as I understand it, you already track it through hitTest), we enter:

    scores.text = String(int(scores.text) + 1); 

    This is the fastest, but not the most reasonable method. What happens: scores.text - take the text of the field, turn it into a number (int (scores.text)), add 1 (int (scores.text) + 1), translate into a string (String (int (scores.text) + 1)), the resulting record in the field (scores.text = String (int (scores.text) + 1)).

    If you want to do better: At the very beginning of the code we get a variable

     var points:int = 0; 

    In the event of a collision, add 1, and write it in the field:

     points++; scores.text = String(points); 

    ("points ++" is an analogue of "points = points + 1" - adding one)

    Advantages of the second method: collision points are stored separately, and there is no chance of a glitch when changing the contents of the text field. Plus, these glasses can be used further.

    Let me remind you again: scores is the name of the text field. You may have it different.