Copy link to clipboard
Copied
I'm trying to make a object be worth points. Here is the code im using, but it doesnt make the score increase, it just makes the 0 vanish after clicking on the item. Then you click on the item again and it says 00, then nothing, then 0 and repeat. Any ideas?
----------------------------------------------------
var currentScore:int;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
currentScore = 0; //start with 0 score
MoneyBtn.addEventListener(MouseEvent.CLICK, addScore ); // clicking on +1
}
function updateScoreText():void
{
scoretxt.text = (""+ currentScore); // set the text property of the txtScore
trace("Score text updated");
}
updateScoreText(); // finally, update the text field
function addScore(e:MouseEvent):void{
currentScore += 25; // add points to the score
updateScoreText(); // update the textfield
}
Copy link to clipboard
Copied
there's no problem with the code you showed. in particular, that code would not cause the problem you are seeing.
Copy link to clipboard
Copied
then what is the problem. instead of adding 25 points, it just changes the score to blank, then 00, then blank.
Copy link to clipboard
Copied
Fixed it. the text wasnt embeded. FML lol
Copy link to clipboard
Copied
New problem sorry, if i was to make multiple objects worth points. How would i change the value on each one? For example. MoneyBtn is worth 25, and im trying to make ShirtBtn worth 50, how exactly would i change it so they dont use the same properties = 25.
---------------------------------------
var currentScore:int;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
currentScore = 0; //start with 0 score
MoneyBtn.addEventListener(MouseEvent.CLICK, addScore );
ShirtBtn.addEventListener(MouseEvent.CLICK, addScore );
}
function updateScoreText():void
{
ScoreTxt.text = ("Score: "+ currentScore); // set the text property of the txtScore
trace("Score text updated");
}
updateScoreText(); // finally, update the text field
function addScore(e:MouseEvent):void{
currentScore += 25; // add points to the score
updateScoreText(); // update the textfield
}
Copy link to clipboard
Copied
either use movieclip button's:
var currentScore:int;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
currentScore = 0; //start with 0 score
ShirtBtn.points=50;
MoneyBtn.points=25;
MoneyBtn.addEventListener(MouseEvent.CLICK, addScore );
ShirtBtn.addEventListener(MouseEvent.CLICK, addScore );
}
function updateScoreText():void
{
ScoreTxt.text = ("Score: "+ currentScore); // set the text property of the txtScore
trace("Score text updated");
}
updateScoreText(); // finally, update the text field
function addScore(e:MouseEvent):void{
currentScore += e.currentTarget.points; // add points to the score
updateScoreText(); // update the textfield
}
or, use a dictionary instance:
var currentScore:int;
var dictionary:Dictionary;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
dictionary=new Dictionary(true);
dictionary[MoneyBtn]=25;
dictionary[ShirtBtn]=50;
currentScore = 0; //start with 0 score
MoneyBtn.addEventListener(MouseEvent.CLICK, addScore );
ShirtBtn.addEventListener(MouseEvent.CLICK, addScore );
}
function updateScoreText():void
{
ScoreTxt.text = ("Score: "+ currentScore); // set the text property of the txtScore
trace("Score text updated");
}
updateScoreText(); // finally, update the text field
function addScore(e:MouseEvent):void{
currentScore += dictionary[e.currentTarget]; // add points to the score
updateScoreText(); // update the textfield
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now