Reputation: 2285
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.
Upvotes: 0
Views: 5575
Reputation: 10325
@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.
Upvotes: 1
Reputation: 5571
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.
Upvotes: 1
Reputation: 17237
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.
Upvotes: 0
Reputation: 3395
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.
Upvotes: 3
Reputation: 4434
try adding
if(!balls){
trace('balls missing');
return;
}
before loop declaration
Upvotes: 0
Reputation: 15570
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++)
Upvotes: 0