Forums

ForumsProgramming Forum

How do I make a As2 score counter?

Posted Jun 4, '13 at 6:25pm

mightybob

mightybob

351 posts

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!

 

Posted Jun 4, '13 at 7:36pm

weirdlike

weirdlike

470 posts

Knight

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);
    }
}

 

Posted Jun 4, '13 at 8:09pm

mightybob

mightybob

351 posts

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?

 

Posted Jun 4, '13 at 8:35pm

weirdlike

weirdlike

470 posts

Knight

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);
    }
}

 

Posted Jun 4, '13 at 11:14pm

weirdlike

weirdlike

470 posts

Knight

I made an example here

hope it works :)

 
Reply to How do I make a As2 score counter?

You must be logged in to post a reply!