Put Delay Before 'return' in Function
Copy link to clipboard
Copied
When I create function that allow flash to detect object's velocity, a problem happen. I can't put delay before 'return' executed. It's cause result of function is null or 0. There's the script:
function SSvelocity(Target:Object,target_property:String,delay_ms:int=1000):Number
{
if ((target_property!="x")&&(target_property!="y")&&(target_property!="z")&&(target_property!="rotation"))
{
trace("ShortenScript: err(SSvelocity[1]): SSvelocity("+Target+","+target_property+","+delay_ms+")");
trace(" SSvelocity(the_object,target_property,delay_millisecond)");
trace(" position should filled with \"x\" or \"y\" or \"z\" or \"rotation\"");
trace(" and don\'t forget about quotes(\")");
DisplayError = true;
return null;
}
var detecta:Timer = new Timer(1000);
var detectb:Timer = new Timer(1000);
var resulta:Number;
var resultb:Number;
detecta.delay = delay_ms;
detectb.delay = delay_ms;
detecta.start();
detecta.addEventListener(TimerEvent.TIMER, handlera);
detectb.addEventListener(TimerEvent.TIMER, handlerb);
function handlera(event:TimerEvent)
{
detecta.stop();
if (target_property == "x")
{
resulta = Target.x;
}
else if (target_property=="y")
{
resulta = Target.y;
}
else if (target_property=="z")
{
resulta = Target.z;
}
else if (target_property=="rotation")
{
resulta = Target.rotation;
}
detectb.start();
}
function handlerb(event:TimerEvent)
{
detectb.stop();
if (target_property == "x")
{
resultb = Target.x;
}
else if (target_property=="y")
{
resultb = Target.y;
}
else if (target_property=="z")
{
resultb = Target.z;
}
else if (target_property=="rotation")
{
resultb = Target.rotation;
}
}
// I want put delay before return, because it's resulting NaN and often 0
return (resultb-resulta);
}
Copy link to clipboard
Copied
You should not nest named functions in other functions. Keep all functions independent. You should rethink the approach to this and have the functions work sequentially, the last function in the sequence assigning the value. So you call the first function that sets things up and calls the first function to assign resulta, tat same function calls the next function after whatever delay to determine resultb and then assign the final result of resultb - resulta.
Copy link to clipboard
Copied
Actually, that function is a part of ShortenScript class. But uniting whole function in a class could be bad idea. Well, I should waste my script that have 766 line and creating new one and let them independent. It's gonna be hard work
Copy link to clipboard
Copied
Yes, that's exactly why "god classes" are a bad idea. When you decide to break things out properly, it's hard to do--especially if you use a lot of global state.
refactoring - How do you refactor a God class? - Stack Overflow
Copy link to clipboard
Copied
I agree with Ned that you should rethink your approach. Objects A and B are your Information Experts (GRASP (object-oriented design) - Wikipedia, the free encyclopedia) on the problem at hand, so they should be responsible for storing their own previous positions and calculating velocity.
Let's assume that it's possible for you to apply the same Base Class to both A and B. That Base Class might look something like this:
public class VelocityClip extends MovieClip {
//each history will contain the past 10 values for that property
protected var propertiesHistory:Dictionary = new Dictionary();
//by adding or removing property names to this Array, you can change what properties are being tracked
protected var trackedProperties:Array = ['x', 'y', 'rotation'];
//capture values every 100 ms (10 times a sec)
protected var timer:Timer = new Timer(100);
public function VelocityClip() {
super();
for each (var property:String in trackedProperties) {
//create a spot to store the history of each tracked property
propertiesHistory[property] = new Vector.<Number>();
}
timer.addEventListener(TimerEvent.TIMER, storeValues);
timer.start();
}
protected function storeValues(e:TimerEvent):void {
for each (var property:String in properties) {
var history:Vector.<Number> = propertiesHistory[property] as Vector<Number>;
if (history.length > 9) {
//stay at 10 total indices by removing first item
history.shift();
}
history.push(Number(this[property]));
}
}
public function getVelocity(property:String):Number {
var history:Vector.<Number> = propertiesHistory[property] as Vector<Number>;
if (!history) return NaN;//no history for this property
if (history.length < 10) return 0; //haven't been moving for a full sec.//history[9] was recorded sometime in the last 99 ms, and history[9] and history[0] are 1 sec apart
//subject to the limitations of the Timer Class
return history[9] - history[0];
}
}

