6

I am very new to action script 3, and I am trying to make a very basic game right now. However, no matter how many pages I look at I can't find a working way to get my game to keep score :/.

What I am trying to do is make it so that every 10 seconds, 10 points is added to the score (right now I have it replaces with a key, to see if I could get that to work, but it didn't). This is the code I am trying to use right now:

    var playerScore:int = 0

    stage.addEventListener(MouseEvent.CLICK,onclick);

    function updateTextFields():void{
         playerScoreText.text = ("Player Score: " + playerScore);
    }

    if(Key.isDown(Key.G)){
          playerScore++; //increase playerScore by 1
          updateTextFields();
    }

playerScoreText is the name of the dynamic text any help will be greatly appreciated :)

This code was all added in Timeline

I am thinking the problem is most likely something with the creation of the dynamic text, but I am not sure.

jackbtroop62
  • 63
  • 1
  • 5
  • It doesn't look like you have anything listening for key presses. Where does the code that you posted exist? Is it just sitting in the timeline or in a class somewhere? Is there anything encapsulating this code sample? (what's before and after it?) – Foggzie Jan 07 '13 at 21:52
  • Sorry, I forgot to add the event listener in the code i put up there, but I do have it: XD stage.addEventListener(MouseEvent.CLICK,onclick); it is inside the actions section in the timeline – jackbtroop62 Jan 07 '13 at 21:54

2 Answers2

2

It sounds like you want to do something like this with the timer class. Your key code isn't written properly.

var playerScore:int = 0;
var score_timer:Timer = new Timer(10000,0);
score_timer.addEventListener(TimerEvent.TIMER,updateTextFields);
score_timer.start();
function updateTextFields(e:TimerEvent):void
{
    playerScore+=10
    playerScoreText.text = ("Player Score: " + playerScore);
}
lostPixels
  • 1,303
  • 9
  • 23
  • That doesn't seem to be working, the playerscore text just stays at 0, and sometime glitches for a second and adds another 0. To make sure it is not just something in that project, I pasted this code in a brand new project and still nothing :/. Is there anything else I have to add to this to get it to work? – jackbtroop62 Jan 07 '13 at 22:03
  • I've also had troubles with dynamic text and it only showing some of the numbers. Try bitmapping the font from its properties. – Jono Jan 07 '13 at 22:13
  • hate to me this annoying person...but how do i bitmap the font? XD I am sorry....I am just really new to this. Trying to finish this game before moving on to full tutorials – jackbtroop62 Jan 07 '13 at 22:15
  • Make sure you're also embedding your font. It wont show the text if you're not storing the characters in the SWF. – lostPixels Jan 07 '13 at 22:37
2

Make sure the fonts are embedded properly and that the color of the dynamic text field is not same as the background.

also instead of writing

playerScoreText.text = ("Player Score: " + playerScore);

try this

playerScoreText.text = "Player Score: " + String(playerScore);
Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • hmm still getting the same problem, And I know the colors and embedding is fine, Can you think of any common mistakes that are made with creating the dynamic text? Because I am pretty sure that is where the problem is, since the coding seems fine :l – jackbtroop62 Jan 07 '13 at 22:12
  • 'playerScore+=10' is missing a semi-colon at the end. Also make sure that the textfield is not below any other object. Make sure it is big enough for the text to fit in. – Joe Slater Jan 07 '13 at 22:15
  • also, try to change the anti alias properties. change it to use device fonts or no anti alias and see what that does. also can somebody answer mine - http://stackoverflow.com/questions/14204769/what-is-the-null-object-error-with-this-as3-code-for-loading-external-swf – Joe Slater Jan 07 '13 at 22:16
  • It does constantly revert back to Player Score: 0, even if I change it to initially say Player Score: 10. So the problem has something to do with the number changing – jackbtroop62 Jan 07 '13 at 22:27
  • can i get your fla file and test it? – Joe Slater Jan 07 '13 at 22:28
  • erm I would be happy to send you it, but It might be a bit confusing XD. I didn't organize it well – jackbtroop62 Jan 07 '13 at 22:29
  • @jackbtroop62 no problem. i am glad to help. your font embedding was not proper as I found out. - http://i.imgur.com/F5ufM.png you had to click the all button. – Joe Slater Jan 07 '13 at 22:51