Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-22-2012, 04:36 PM   #1
loewenstern
Registered User
 
Join Date: Dec 2011
Location: Norway.
Posts: 9
Default [AS3] High score system with a dynamic text field

Hey there. I'm stuck. This was quite easy in AS 2, but I can't seem to figure out how to solve it in AS 3.

I want to make a high score system. Everytime "helten" hits "smilefjes" I want the text field called "hiscore" that starts with 0 to go 10 points up. My first attempt was the following:

Code:
addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void {
	if (helten.hitTestObject(smilefjes)) {
	  hiscore.text += 10;
    }
}
What happens when I run this is that the text field just adds a zero next to the zero that already is there.

I have 2 questions. First, why doesn't it add the 10 instead of just a zero? Second, how can I make it work so that the 0 is replaced with the 10 when "helten" hits "smilefjes", and the second time the 10 is replaced with 20 etc.
loewenstern is offline   Reply With Quote
Old 07-23-2012, 01:21 AM   #2
menachemt
Member
 
Join Date: Jun 2012
Location: Brooklyn, New York
Posts: 75
Default

I am no professional in as3 but I like helping people so I did some googling and found something. First of all, try adding
Code:
var hiscore:int=0;
hiscore.text=String(hiscore);
to the top of your code, then change
Code:
hiscore.text += 10;
to
Code:
hiscore+=10;
I purposely didn't put any spaces in the new piece of code, cause thats how I found it. I have to give credit to this thread: actionscript.org/forums/showthread.php3?t=234059 Hope I helped.
menachemt is offline   Reply With Quote
Old 07-23-2012, 02:08 AM   #3
loewenstern
Registered User
 
Join Date: Dec 2011
Location: Norway.
Posts: 9
Default

Quote:
Originally Posted by menachemt View Post
I am no professional in as3 but I like helping people so I did some googling and found something.
Thanks, you are gentleman and a scholar. It didn't work as it was, but when I edited it a bit it worked.

Code:
var highscore:int=0;

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void {
	if (helten.hitTestObject(smilefjes)) {
	  highscore+=10;
	  hiscore.text = String(highscore);
	}
}
was what I ended up with. If you see in the thread you linked you can see that "myScore" and "myScore_txt" is not the same. "myScore" is the name of the textfield while "myScore" is just the name of the variable.

My textfield is called hiscore and my variable is called highscore.
loewenstern is offline   Reply With Quote
Old 07-23-2012, 03:27 AM   #4
Prid
Prid - Outing
 
Prid's Avatar
 
Join Date: Oct 2009
Location: Halden, Norway
Posts: 1,191
Send a message via MSN to Prid Send a message via Skype™ to Prid
Default

Hi fellow Norwegian,

it only added it at the end of the textfield, because a textfield is a String, it's not a Number, hence it doesn't have any mathematical function, such as adding and subtracting. Declaring an integer variable and incrementing it, and displaying that variable in your textfield, is indeed the correct way of doing it. Also, I am assuming you typed 0 in your dynamic textfield, like manually, right? Because, that's why it was only adding 0 at the end and not 10, because you haven't embedded the font to your dynamic textfield, making it only display the font characters already typed in the textfield!
__________________
I have returned, and am now 18
Prid is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:07 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.