Specifically if I try to use it to call a pre-existing function, then this.ship is not defined within that function (presumably as addFrameCallback is a method of Global?). This of course makes it rather difficult to actually use to animate a ship when the called function doesn't know what ship called it?
It passes down to the moveWings function fine and gives me access to the parameter delta, but this.ship is no longer defined. I tried to use a function definition as in the wiki example to call the moveWings function, but that failed too as it said this.moveWings was not a function?
What am I doing wrong here? Trying it with a modified version of the butterflies OXP. The FPS stuff etc may also not be right, but as I can't get the thing to run at all I can't test that yet...
Code: Select all
Exception: TypeError: this.ship is undefined
butterflies_butterfly.js, line 97:
if(this.ship.AI == "dockingAI.plist" && this.ship.position.distanceTo(this.ship.target.position) < 1500)
Code: Select all
this.name = "butterflies_butterfly";
this.author = "Thargoid";
this.copyright = "Creative Commons: attribution, non-commercial, sharealike.";
this.description = "Butterfly wing sub-ent motion";
this.version = "1.0";
this.shipSpawnedAsEscort = function()
{
this.ship.setAI("butterflies_butterflySwarmAI.plist");
this.shipSpawned();
}
this.shipSpawned = function()
{
this.wingCounter = 0;
this.Q0 = new Quaternion([1,0,0,0]);
this.Q1 = new Quaternion([0,0,0,1]);
this.setColours(Math.floor(Math.random() * 8));
this.callbackID = addFrameCallback(this.moveWings);
}
this.setColours = function(selection)
{
if(!selection || selection.isNaN || selection < 0 || selection > 8) { this.colorChoice = 0; }
else { this.colorChoice = Math.floor(selection);}
switch(this.colorChoice)
{// if colorChoice is 0, revert to default colouration.
case 0:
{
this.overrideMaterials(false);
this.ship.displayName = this.ship.name;
break;
}
case 1:
{
this.overrideMaterials("redColor");
this.ship.displayName = "Red " + this.ship.name;
break;
}
case 2:
{
this.overrideMaterials("cyanColor");
this.ship.displayName = "Cyan " + this.ship.name;
break;
}
case 3:
{
this.overrideMaterials ("grayColor");
this.ship.displayName = "Gray " + this.ship.name;
break;
}
case 4:
{
this.overrideMaterials("lightGrayColor");
this.ship.displayName = "Light Gray " + this.ship.name;
break;
}
case 5:
{
this.overrideMaterials("greenColor");
this.ship.displayName = "Green " + this.ship.name;
break;
}
case 6:
{
this.overrideMaterials("yellowColor");
this.ship.displayName = "Yellow " + this.ship.name;
break;
}
case 7:
{
this.overrideMaterials("purpleColor");
this.ship.displayName = "Purple " + this.ship.name;
break;
}
case 8:
{
this.overrideMaterials("magentaColor");
this.ship.displayName = "Magenta " + this.ship.name;
break;
}
}
}
this.overrideMaterials = function(colName)
{
// this.ship.setMaterials({"butterflies_butterflyBody.png": { diffuse: colName }}); // body looks better dark - just change the wings
this.ship.subEntities[0].setMaterials({"butterflies_butterflyWing.png": { diffuse: colName }});
this.ship.subEntities[1].setMaterials({"butterflies_butterflyWing.png": { diffuse: colName }});
}
this.moveWings = function(delta)
{
if(delta && delta === 0) { return; }
if(this.ship.AI == "dockingAI.plist" && this.ship.position.distanceTo(this.ship.target.position) < 1500)
{
if(this.wingCounter != 0)
{
this.wingCounter = 0;
this.ship.subEntities[0].orientation = this.Q0; // flatten the wings for docking
this.ship.subEntities[1].orientation = this.Q1;
if(this.callbackID && isValidFrameCallback(this.callbackID))
{ removeFrameCallback(this.callbackID); }
}
return;
}
this.FPS = 1 / delta;
this.leftAngle = (Math.PI/4) * (Math.sin(this.wingCounter*(Math.PI/(this.FPS/2)))); // pi/4 to -pi/4 angle of rotation
this.rightAngle = (Math.PI/4) * (Math.sin(Math.PI + this.wingCounter*(Math.PI/(this.FPS/2)))); // anti-phase with left wing
if(this.leftAngle < 0) { this.leftAngle += (2*Math.PI) }; // keep angles clockwise only, for simplicity
if(this.rightAngle < 0) { this.rightAngle += (2*Math.PI) }; // ditto
this.ship.subEntities[0].orientation = this.Q0.rotateZ(this.leftAngle);
this.ship.subEntities[1].orientation = this.Q1.rotateZ(this.rightAngle);
if(this.wingCounter >= this.FPS)
{
this.wingCounter = 0;
}
else
{
this.wingCounter++;
}
}
this.shipDied = function (whom, why)
{
if(this.callbackID && isValidFrameCallback(this.callbackID))
{ removeFrameCallback(this.callbackID); }
// this.checkTimer.stop();
}