0
votes

In a function i'm trying to delay the execution of the code through:

var thinkTimer = setInterval(Actions(opp), C.THINK_TIME);

where Actions is a simple (tested working) function, opp is a small integer and C.THINK_TIME is a constant = 3000 (3 seconds).

but using setInterval I got this problem: The function Actions immediatly runs (without waiting the interval) and every 3 seconds I got this message in the output:

TypeError: Error #1009: Impossibile accedere a una proprietà o a un metodo di un riferimento oggetto null. at SetIntervalTimer/onTimer() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

What is this about? I'm not trying to accede a property or a function with a refering object null! :-/

2

2 Answers

0
votes

You have to wrap your Actions(opp) call into a nested function, and supply the name of that into your setInterval call, like this:

private var opp:int; // place this outside if you ever want to change opp at runtime
// without changing the setInterval statement
function actOpp():void { Actions(opp); } // wrapper
var thinkTimer:uint = setInterval(actOpp, C.THINK_TIME);

The actual error is that setInterval wants a Function object, and you are trying to provide it a function output of Actions(opp). Most likely your Actions function returns void, this causes your errors to appear.

In case you don't want to change your opp at runtime, say you want your AI to check for a certain opp once in a set period of time, use the extended syntax of setInterval like this:

var thinkTimer:uint = setInterval(Actions, C.THINK_TIME, opp);

Then you retrieve the passed opp value in your function using arguments[0] syntax, or, if you declared a parameter in that function, use its name as usual. Note that with this syntax your opp value passed to Actions will not change if you change the value of the variable elsewhere. To override, you can use a wrapper function for opp, pass its name (or reference) to the setInterval as a parameter, then accept that reference inside Actions, call the function and receive the current opp value.

Hope this helps, if you have questions, please comment below.

0
votes

Not everyone speaks your language, but from the Error code, something is referencing a null object, one of the objects is null, trace the objects before this line and see which one is "null".