Flash Score Counter

Changing From Actionscript 2 to 3.0

Page 1 of 1

8 Replies - 27393 Views - Last Post: 29 March 2009 - 03:42 PM

#1 whitecrossnathan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 09

Flash Score Counter

Posted 24 March 2009 - 06:00 AM

I've scripted a score counter in AS2 but need to change it to AS3.

The code I have so far is...
on (release) {
	score+=5;
}



This code is present on a button in AS2 and then includes the score in a dynamic text, but when I try to script in AS3 on the 1st Layer it won't recognize it. I know that this is because there needs to be a function or the inclusion of the name but I can't find out how to do this.
Is This A Good Question/Topic? 0
  • +

Replies To: Flash Score Counter

#2 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Flash Score Counter

Posted 24 March 2009 - 07:40 AM

In actionscript 3, user interactions are run by events. You must listen for an event on an object, and have a function to execute when the event occurs.

This code goes on the same timeline as your button, not on the button itself. myButton is the instance name of the button.
myButton.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():Void {
	score += 5;
}


Was This Post Helpful? 0
  • +
  • -

#3 whitecrossnathan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 09

Re: Flash Score Counter

Posted 24 March 2009 - 02:10 PM

I entered the code you said:
score5_btn.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():Void {
	score += 5;
}



With this code to display the score count:
score_txt.text = String(score);



But these errors occurred:

1046: Type was not found or was not a compile-time constant: Void.

[quote name='whitecrossnathan' date='24 Mar, 2009 - 01:06 PM' post='584128']
I entered the code you said:
score5_btn.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():Void {
	score += 5;
}


With this code to display the score count:
score_txt.text = String(score);


But these errors occurred:
1046: Type was not found or was not a compile-time constant: Void.
5000: The class 'fl.controls.Button' must subclass 'flash.display.SimpleButton' since it is linked to a library symbol of that type.

[quote name='whitecrossnathan' date='24 Mar, 2009 - 01:08 PM' post='584128']
I entered the code you said:
score5_btn.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():Void {
	score += 5;
}



With this code to display the score count:
score_txt.text = String(score);



But these errors occurred:

1046: Type was not found or was not a compile-time constant: Void.

View Postwhitecrossnathan, on 24 Mar, 2009 - 01:06 PM, said:

I entered the code you said:
score5_btn.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():Void {
	score += 5;
}


With this code to display the score count:
score_txt.text = String(score);


But these errors occurred:
1046: Type was not found or was not a compile-time constant: Void.
5000: The class 'fl.controls.Button' must subclass 'flash.display.SimpleButton' since it is linked to a library symbol of that type.



Sorry I don't know why it replied 3 times!
Was This Post Helpful? 0
  • +
  • -

#4 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Flash Score Counter

Posted 25 March 2009 - 10:18 AM

Sorry, my bad. In my code, the word Void should have a lower case v:

score5_btn.addEventListener(MouseEvent.MOUSE_DOWN, onButtonclicked);

function onButtonclicked():void {
	score += 5;
}


Was This Post Helpful? 0
  • +
  • -

#5 whitecrossnathan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 09

Re: Flash Score Counter

Posted 26 March 2009 - 04:28 PM

It still reports these errors:

Frame 1 line 4: 1120: Access of undefined property score.

and

ComponentShim (Compiled Clip), Line 1 5000: The class 'fl.controls.Button' must subclass 'flash.display.SimpleButton' since it is linked to a library symbol of that type.
Was This Post Helpful? 0
  • +
  • -

#6 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Flash Score Counter

Posted 27 March 2009 - 05:00 AM

Righto, two things. The first is that the buttonclicked function needs to have an argument declared for the event object:
function onButtonclicked(evt:MouseEvent):void {



Second, you will have to declare your score variable and set it's initial value outside of the function before it can be used. Before you create the button listener, declare score like this:
var score:Number = 0;



As for your button error, have you tried to create a custom button class?
Was This Post Helpful? 0
  • +
  • -

#7 whitecrossnathan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 09

Re: Flash Score Counter

Posted 28 March 2009 - 05:01 PM

This is my final script and is displays everything with no errors but the only problem is that when the button is clicked the dynamic text box displays 0 and doesn't count up.
stop();
var score:Number = 0;
score_btn.addEventListener(MouseEvent.CLICK, onButtonclicked);



function onButtonclicked(e:MouseEvent):void {
	score ++;
}

score_txt.text = String (score);


Was This Post Helpful? 0
  • +
  • -

#8 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1630
  • View blog
  • Posts: 8,492
  • Joined: 07-September 06

Re: Flash Score Counter

Posted 29 March 2009 - 10:05 AM

In your function you need to update the text box's text:

stop();
var score:Number = 0;
score_btn.addEventListener(MouseEvent.CLICK, onButtonclicked);



function onButtonclicked(e:MouseEvent):void {
	score ++;
	score_txt.text = String (score);
}

score_txt.text = String (score);


HTH
Was This Post Helpful? 0
  • +
  • -

#9 whitecrossnathan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 09

Re: Flash Score Counter

Posted 29 March 2009 - 03:42 PM

Thanks for all your help it all works now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1