ForumsProgramming Forum

How do you make an as2 score counter

Posted Dec 21, '11 at 5:05pm



19 posts

I am working on a platform game and i need to make it that when my character hits a coin it adds +1 to the score i only have a dynamic text box with the instance name of score please help.


Posted Dec 24, '11 at 10:46pm



130 posts

Here's a simple way to do it:

(To be organized, all code should be placed on the main stage actions panel)

Assuming your player to have the instance name "player", (change it to fit what you need)
I also suggest you change the name of the text box to "score_text", because we are going to use a variable called score.

Create a new moveclip called "coin". In the library panel right-click the symbol and click "linkage". Check "Export for actionscript". (We are doing this to allow flash to reference this symbol in code). Close the linkage window.

On the main stage actions panel, type the following:

score = 0;
coinNum = 0;
function spawnCoin(xposition:Number, yposition:Number, value:Number):Void {
var coin:MovieClip = _root.attachMovie("coin", "coin"+_root.getNextHighestDepth(), 100+_root.getNextHighestDepth());
coin._x = xposition;
coin._y = yposition;
coin.value = value;
coin.onEnterFrame = function() {
if (player.hitTest(this)) {
_root.score += this.value;
_root.score_text.text = _root.score

Now, you can easily spawn coins by executing the spawnCoin function as follows:


The first parameter sets the coin's x-coordinate (275)
The second parameter sets the coin's y-coordinate (200)
The third value sets how many points the coin is worth (10)

You can use this function to create as many coins as you wish simply by altering those three parameters.

Happy programming!


Posted Jun 4, '13 at 4:34pm



369 posts

What if I don't want to go through the irritating process of setting X and Y positions? How can I just drag coins onto the stage and it work?


Posted Jun 4, '13 at 6:02pm



1,292 posts


this thread is over 2 years old...

I think your answer would require an instance name for each coin to make it work.

Reply to How do you make an as2 score counter

You must be logged in to post a reply!