Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Basic ActionScript 3 help please! (Displaying an integer value)

Explorer ,
Sep 21, 2015 Sep 21, 2015

Hi all,

I've never used Flash Pro or coded in ActionScript before so please forgive me for this hopelessly basic question! I have previous coding experience in other languages but I can't seem to figure this one out!

I am trying to code a quiz in Adobe Flash Pro, when you click on a button I want the integer variable 'quizScore' to increase by 1, 2 or 3 or decrease by 1, 2 or 3 depending on which button you press. I want a 'score counter' (in the form of text?) at the bottom of the quiz which displays the current value of 'quizScore'.

Below is my code. When I run it at the moment nothing happens.

var quizScore = 0; //Set the score of the quiz to 0

//Add event listeners to the buttons on the quiz

//Each button has a different function to add the correct number of points

p01.addEventListener(MouseEvent.CLICK, addOnePoint);

p02.addEventListener(MouseEvent.CLICK, addTwoPoints);

p03.addEventListener(MouseEvent.CLICK, addThreePoints);

p001.addEventListener(MouseEvent.CLICK, minusOnePoint);

p002.addEventListener(MouseEvent.CLICK, minusTwoPoints);

p003.addEventListener(MouseEvent.CLICK, minusThreePoints);

submit_button.addEventListener(MouseEvent.CLICK, submitFunction);

//Functions for the points buttons

function addOnePoint(event:MouseEvent):void {

  quizScore = quizScore+1;

}

function addTwoPoints(event:MouseEvent):void {

  quizScore = quizScore+2;

}

function addThreePoints(event:MouseEvent):void {

  quizScore = quizScore+3;

}

function minusOnePoint(event:MouseEvent):void {

  quizScore = quizScore-1;

}

function minusTwoPoints(event:MouseEvent):void {

  quizScore = quizScore-2;

}

function minusThreePoints(event:MouseEvent):void {

  quizScore = quizScore-3;

}

//Submit button function

function submitFunction(event:MouseEvent):void {

  score.text=quizScore;

}

p01 = button that adds 1 to the quizScore

p02 = button that adds 2 to the quizScore

p03 = button that adds 3 to the quizScore

p001 = button that subtracts 1 from the quizScore

p002 = button that subtracts 2 from the quizScore

p003 = button that subtracts 3 from the quizScore

quizScore = the score the user has scored in the quiz

score = text that displays the score at the bottom of the quiz

If you need anything else please let me know!

Thanks so much!!

Jason

TOPICS
ActionScript
790
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2015 Sep 21, 2015

I don't see anything readily wrong with your code except for a few minor things.

You should specify the variable type when you declare it, as in...

     var quizScore:int = 0;

When assign a numeric value as a String you should cast it as such...

    score.text = String(quizScore);

You can shorten the scoring code

    quizScore += 1;

    quizScore -= 1;  etc

As far as nothing happening goes, I would expect a possible warning regarding the second item I mention, which makes me think that maybe you do not have the publishing set for Actionscript 3.  Check to be sure that you have AS3 selected in the Publish Settings.

Beyond that, how is the quiz set up along the timeline?  It is possible that the code is assigned to objects that are no longer present if you are walking down the timeline.  That could explain why you get no error messages.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

Many many thanks for your reply! I will make those additions and see what happens!

Presently I only have one frame on the timeline which has all of the questions on it. Eventually once the user has answered all of the questions and have pressed 'submit' they will be taken to the appropriate frame with their result. At the moment I just want the score to be displayed so I can be sure the variables are all working ok and that the right number is being produced.

Jason

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

Still not working or doing anything. I have lots of buttons with the same variable names, could that be it? I have ensured that all buttons are in fact buttons and the text is dynamic, but still nothing.

Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2015 Sep 21, 2015

That will definitely be a problem.  Each button has to be uniquely named, using the names you used in the code.  And, if you do have 'variable' names associated with the buttons you are likely not working in AS3.  That is a feature of AS2 and earlier.  Check your Publish Settings.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

In the publish settings only AS3 is available, so I think I created the document as an AS3 document when I made it. Instance names is what I meant.

I was thinking that I have 40 questions in this quiz and therefore 240 buttons (40x6 buttons, 6 options for each question). Each question has a button that adds 1, 2, 3 -1, -2 or -3 points to the quiz score, so I thought I could use the same instance name for all of the buttons that add 3 points, all of the buttons that add 2 points, all of the buttons that add 1 point etc... or do all the buttons need to have unique instance names?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2015 Sep 21, 2015

Nope.  Imagine you are the code and you have to decide which button to assign yourself to (only one can get the assignment).   I would guess that the last button found is the one that gets the code. 

You likely do not need to have more than 7 buttons total, unless they do different things at different times, but then still you can assign the functionality as needed using the same7 buttons. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

OK I'm going to give this a go making a new document and using unique instance names for the buttons. Hopefully it will work!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

OK 3 questions, each with 6 buttons - each now has a different instance name, here is my code:

var quizScore:int = 0; //Set the score of the quiz to 0

//Add event listeners to the buttons on the quiz

//Each button has a different function to add the correct number of points

q0101.addEventListener(MouseEvent.CLICK, addOnePoint);

q0102.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0103.addEventListener(MouseEvent.CLICK, addThreePoints);

q0104.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0105.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0106.addEventListener(MouseEvent.CLICK, minusThreePoints);

q0201.addEventListener(MouseEvent.CLICK, addOnePoint);

q0202.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0203.addEventListener(MouseEvent.CLICK, addThreePoints);

q0204.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0205.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0206.addEventListener(MouseEvent.CLICK, minusThreePoints);

q0301.addEventListener(MouseEvent.CLICK, addOnePoint);

q0302.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0303.addEventListener(MouseEvent.CLICK, addThreePoints);

q0304.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0305.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0306.addEventListener(MouseEvent.CLICK, minusThreePoints);

submitButton.addEventListener(MouseEvent.CLICK, submitFunction);

//submit_button.addEventListener(MouseEvent.CLICK, submitFunction);

//Functions for the points buttons

function addOnePoint(event:MouseEvent):void {

  quizScore +=1;

}

function addTwoPoints(event:MouseEvent):void {

  quizScore +=2;

}

function addThreePoints(event:MouseEvent):void {

  quizScore +=3;

}

function minusOnePoint(event:MouseEvent):void {

  quizScore -=1;

}

function minusTwoPoints(event:MouseEvent):void {

  quizScore -=2;

}

function minusThreePoints(event:MouseEvent):void {

  quizScore -=3;

}

//Submit button function

function submitFunction(event:MouseEvent):void {

  score.text=String(quizScore);

}

By default the text where the score should appear when you press Submit should say 'Score'. When I press the Submit button the text which says 'Score' just disappears and the score is not shown.

Publish settings AS3 and Flash Player 17.

Here's my fla document in case that helps: link

Many thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

Fixed it! I had something commented out, deleted it and it seemed to work. Thanks for all of your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2015 Sep 21, 2015

Try putting a trace statement inside the submitFunction to help you see what is being processed.

  1. function submitFunction(event:MouseEvent):void { 
  2.     trace(quizScore);
  3.     score.text=String(quizScore); 
  4.     trace(score.text)
  5. }

One other thing you might try as well is to embed the font for the textfield and be sure to include numbers for it.  If you happen to be manipulating that textfield in any way, such as animating it or masking it, then embedding the font will also help with that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

OK thanks, will do! One issue is that it works but it seems to be buggy, doesn't seem to be adding the values to quizScore correctly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015

OK so there seems to be a problem with adding to the score itself.

Each big green should add 3 to the score, each medium-sized green button should add 2, each small green button should add 1, each small red button should subtract 1, each medium-sized red button should subtract 2 and each big red button should subtract 3.

I altered my code to show the score as you press the buttons, and sometimes no score is displayed, other times the wrong score is displayed etc. Must be a logic error with my code somewhere that I am missing.

Code below:

var quizScore:int = 0; //Set the score of the quiz to 0

//Add event listeners to the buttons on the quiz

//Each button has a different function to add the correct number of points

q0101.addEventListener(MouseEvent.CLICK, addThreePoints);

q0102.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0103.addEventListener(MouseEvent.CLICK, addOnePoint);

q0104.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0105.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0106.addEventListener(MouseEvent.CLICK, minusThreePoints);

q0201.addEventListener(MouseEvent.CLICK, addThreePoints);

q0202.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0203.addEventListener(MouseEvent.CLICK, addOnePoint);

q0204.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0205.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0206.addEventListener(MouseEvent.CLICK, minusThreePoints);

q0301.addEventListener(MouseEvent.CLICK, addThreePoints);

q0302.addEventListener(MouseEvent.CLICK, addTwoPoints);

q0303.addEventListener(MouseEvent.CLICK, addOnePoint);

q0304.addEventListener(MouseEvent.CLICK, minusOnePoint);

q0305.addEventListener(MouseEvent.CLICK, minusTwoPoints);

q0306.addEventListener(MouseEvent.CLICK, minusThreePoints);

submitButton.addEventListener(MouseEvent.CLICK, submitFunction);

//Functions for the points buttons

function addOnePoint(event:MouseEvent):void {

  quizScore +=1;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

function addTwoPoints(event:MouseEvent):void {

  quizScore +=2;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

function addThreePoints(event:MouseEvent):void {

  quizScore +=3;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

function minusOnePoint(event:MouseEvent):void {

  quizScore -=1;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

function minusTwoPoints(event:MouseEvent):void {

  quizScore -=2;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

function minusThreePoints(event:MouseEvent):void {

  quizScore -=3;

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

//Submit button function

function submitFunction(event:MouseEvent):void {

  trace(quizScore);

    score.text=String(quizScore);

    trace(score.text)

}

I also have the fla and swf files so you can see what is going on : FLA SWF

Again, many many thanks for any assistance! Hugely appreciated! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2015 Sep 21, 2015
LATEST

OK so I have been doing more testing tonight. Basically the output panel in Flash Pro shows the correct values of quizScore whilst I debug the program (although every time I press a button the score is outputted in the output window three times, not sure if it's normal or not), but the score text box is showing all kinds of weird scores - sometimes the correct scores as shown in the output window but mostly random numbers. That's the next thing to look at.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines