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.
(To be organized, all code should be placed on the main stage actions panel)
Assuming your player to have the instance name "layer", (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.