问题
After compiling and running my program, i run into runtime error 1010:
TypeError: Error #1010: A term is undefined and has no properties.
at DC/updateScore()[DC::frame74:36]
This is the piece of code:
function updateScore(e:MouseEvent) {
var i:uint=0;
for(;i<balls.length;i++)
if(balls[i]==e.target)
break;
if(balls[i].isCorrect) {
score++;
timeField.text = new String(score);
}
else {
score--;
timeField.text = new String(score);
}
}
What's the problem? I'm using updateScore function for MouseEvent listener, as you can see.
回答1:
Please put { and } to your for loop!
function updateScore(e:MouseEvent) {
var i:uint=0;
for(;i<balls.length;i++) {
if(balls[i]==e.target)
break;
if(balls[i].isCorrect) {
score++;
timeField.text = new String(score);
}
else {
score--;
timeField.text = new String(score);
}
}
}
Its allowed not to use { and } with a loop but then only the first statement will be excuted.
回答2:
The error means that you are trying to manipulate properties of a undefined variable. You are probably trying to access balls[i] when there is no element on the i-th position. Or possibly there is another object without the property you want to access.
At what line do you get the error. Are you sure balls[i].isCorrect is a valid property.
回答3:
@Jens answer has it, but his explanation is confusing.
When your loop exits,
i = balls.length
On the next line, you're referencing
balls[i].isCorrect.
On that particular line, i is 1 greater than the greatest index in balls. Hence, exception.
While you may not want each of those lines to execute for each repetition of the loop as Jens thought, you've got to address this.
回答4:
You've missed the first rule in your for loop:
for(;i<balls.length;i++)
I think you meant
for(i;i<balls.length;i++)
回答5:
try adding
if(!balls){
trace('balls missing');
return;
}
before loop declaration
回答6:
based on the little code you've provided, you've either not instantiated the balls vector or the timeFiled textField.
var balls:Vector<Ball>;
balls.push(new Ball(whatever), new Ball(whatever), new Ball(whatever));
trace(balls);
//ERROR
//you must initialize the vector: var vector:Vector<Ball> = new <Ball>;
which part of the code is on line 36?
the problem may also be with the Ball object's isCorrect property.
来源:https://stackoverflow.com/questions/6910233/error-1010-in-actionscript