ForumsProgramming ForumHow do you make an as2 score counter

3 7917
fiester
offline
fiester
18 posts
Nomad

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.

  • 3 Replies
arobegamr
offline
arobegamr
130 posts
Nomad

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 &quotlayer", (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());
coinNum++;
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
this.removeMovieClip();
}
};
}


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

spawnCoin(275,200,10)

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!

mightybob
offline
mightybob
360 posts
Peasant

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?

weirdlike
offline
weirdlike
1,299 posts
Prince

this thread is over 2 years old...

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

Showing 1-3 of 3