I have genuinely, genuinely looked ALL over the web for a basic scoring system and can't find one that actually works. Maybe it's just me/ my game but nothing works. I'm trying to do it with a dynamic text box. So i've got a starting score. But when i put: "Score = Score + 10;"
Nothing happens. So yeah, any help please. I'm gonna put up most of the game code, to see if it's something else, and there's only ~ 30 lines...I want it so when the two objects collide the score goes up. THANKS Oh and I embedded the text, after flash told me to. Hence the first line ↓.import flash.text.*;
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.text.TextField;
var pressedKeys:Object = {};
// BASIC KEYBOARD MOVEMENT - Took it out as it is a开发者_运维技巧lmost defs irrelevant.
var Score = 10
var myText:TextField = new TextField();
addChild(myText);
myText.text = ("Score:"+ Score);
myText.textColor = 0xFF0000;
myText.border = true;
myText.height = 20;
myText.x = 4;
myText.y = 4;
addEventListener(Event.ENTER_FRAME, hitTest)
function hitTest(e:Event):void
{
if(Hero_Mc.hitTestObject(Enemy_mc)&& (Enemy_mc.width<Hero_Mc.width))
{
Hero_Mc.width=Hero_Mc.width +4;
Hero_Mc.height=Hero_Mc.height +4;
Enemy_mc.stop();
removeChild(Enemy_mc);
addChild(Enemy_mc);
Enemy_mc.x= Math.floor(Math.random()*500);
Enemy_mc.y= Math.floor(Math.random()*350);
Score = Score + 10;
}
}
Nothing changes because myText.text
only receives the value of Score
, not the actual reference. You can either use BindingUtils
. Or, you can change the following line:
var Score = 10;
to
private var _score = 10;
protected function set Score(value:Number):void {
_score = value;
//Now, whenever you set Score to anything, it also sets the text on myText.
myText.text = ("Score:"+ Score);
}
protected function get Score():Number {
return _score;
}
Also, I agree with citizen. ActionScript convention is to use lowercase variables and _prefixed
for private vars.
This is a little better syntax:
var score:Number = 10;
score += 10;
Is that if statement evaluating true?
精彩评论