ForumsProgramming ForumHow do I make a As2 score counter?

4 2715
mightybob
offline
mightybob
363 posts
1,050

I need to know how to make a As2 score counter for my platformer game i'm programming, and I don't want to go through setting X and Y positions in code. I just want to drag and drop coins onto stage. Any help is appreciated!

  • 4 Replies
weirdlike
offline
weirdlike
1,309 posts
28,720

assuming you have a "hero_MC", "coin_MC" and a "score_MC" on the timeline

maybe something like this

// Add collision event to detect player
addEventListener(detectPlayer);

function detectPlayer(event){
if(coin.hitTestObject(root["heroMC"])){ // Test for collision between this MovieClip and the player

// Update the score box on the main timeline
root["score_mc"].score_txt.text = String(Number(root["score_mc"].score_txt.text)+100);

// Make the MovieClip invisible and remove the collision event
coin.visible = false;
removeEventListener("enterFrame", detectPlayer);
}
}

mightybob
offline
mightybob
363 posts
1,050

How do I use this...? You say that I need a score_MC, but in the code it sometimes refers to it as score_txt... So I'm confused. Plus why are there quotes around the instance names? Am I supposed to remove those?

weirdlike
offline
weirdlike
1,309 posts
28,720

I am trying to adapt from as3

root is referring to the main timeline if you are coding on the object itself.

the question on score_MC would be if you had a movie clip with both static and dynamic text
like the part that says money which never changes it would be static, the part that gets updated would be numerical and would be dynamic

so in essence score_MC has a box inside it that has the score_txt box

http://imageshack.us/a/img163/8462/58787930.jpg

this might look a little cleaner, again I don't have your files and I'm trying to adapt from as3

addEventListener(detectPlayer);
function detectPlayer(event){
if(this.hitTestObject(root["heroMC"])){

root["score_mc"].score_txt.text = String(Number(root["score_mc"].score_txt.text)+100);

this.visible = false;
removeEventListener(detectPlayer);
}
}

weirdlike
offline
weirdlike
1,309 posts
28,720

I made an example here

hope it works

Showing 1-4 of 4