error #1006 value is not a function - actionscript-3

I have a problem with my project.
I use SWFLoader to load a .swf file and addEvent when it completed:
function completeHandler(event:LoaderEvent):void {
var mainObj:Object = Object(loader.rawContent.CTN_PROFILES);
var obj : Object = { };
mainObj.playerOptions(obj);
}
but always get this Error:#1006 value is not a function.
And it happened at :
mainObj.playerOptions(obj);
When i trace mainObj at console, i get all of its methods:
{Profiles}
Extends: {Object}
function tabInfoClickHandle(flash.events::MouseEvent):*
function closePopup(flash.events::MouseEvent):*
function playerOptions(Object):*
function defaultTab():*
function tabCodeClickHandle(flash.events::MouseEvent):*
function update(Object):*
function acceptProfile(flash.events::MouseEvent):*
function getGift(flash.events::MouseEvent):*
I've tried so many way but cannot call any methods, even casting it to MovieClip.But in another project, i've used exactly this code and it worked!I've read many topic but cannot find out how.
Is there any solution for my problem! Thanks!

Related

D3.js brushend function not properly called

I made a module that draws a bar chart including a d3.svg.brush(). Part of my code is
Bar.prototype.init = function ( ) {
//...
this.brush = d3.svg.brush()
.y( this.y)
.on("brushend", this.brushend);
console.log(this.brushend); // works
}
Bar.prototype.brushend = function() {
console.log(this.brush); // undefined. why?
}
To access this.* values, I cannot make brushend function a normal function by using function brushend() or var brushend = function().
How should I call it properly?
D3 will call the event handler provided for its brushend event much like event handlers for normal DOM events will get called. For standard events this will reference the element the event occured upon. This rule does also apply to D3's brush event handlers where this references the group containing the DOM elements of the brush. Obviously, this group doesn't have a property brush you could reference by using this.brush from with the handler.
A workaround would be to save your reference by using a closure:
Bar.prototype.init = function ( ) {
//...
this.brush = d3.svg.brush()
.y(this.y)
.on("brushend", this.brushend()); // Get handler by calling factory
}
// A factory returning the handler function while saving 'this'.
Bar.prototype.brushend = function() {
var self = this; // Save a reference to the real 'this' when the factory is called.
return function() { // Return the handler itself.
// By referencing 'self' the function has access to the preserved
// value of 'this' when the handler is called later on.
console.log(self.brush);
};
}

requestAnimationFrame type error in object

hi i'm trying to use requestAnimationFrame for my game and I actually use this code below, but as you can see ".bind()" create every loop a new function that slow down my game... I'm looking for a "efficient" solution for the best perfomance, thank you in advance :D
function myClass() {
this.loop = function() {
window.requestAnimationFrame(this.loop.bind(this));
/* here myGameLoop */
}
this.loop();
}
above code works but is slow.. instead this "standard" code give me "Type error":
window.requestAnimationFrame(this);
I have also found e tried this Q&A: requestAnimationFrame attached to App object not Window works just ONE time then give the same "Type error" :(
try if you don't believe me: http://jsfiddle.net/ygree/1 :'(
Without knowing the whole story of your object (what else is in there); you could simplify life by just doing this:
function myClass() {
var iHavAccessToThis = 1;
function loop() {
iHavAccessToThis++;
/* here myGameLoop */
requestAnimationFrame(loop);
}
loop();
//if you need a method to start externally use this instead of the above line
this.start = function() { loop() }
//...
return this;
}
Now you don't need to bind anything and you access local scope which is fast.
And then call:
var class1 = new myClass();
class1.start();

Is there a work around for an object oriented front-end in Google Script

I'm having some problems with an event handler in the object below. I can't remember the error message but it basically said that it could not find the function. The code below is an example of what I'm trying to do.
var anObject = function () {
var n = 0;
var HandleClick(e) {
n ++;
};
return {
Init: function () {
var app = UiApp.getActiveApplication();
var handler = app.createServerHandler("HandleClick");
var com = UiApp.LoadComponent("MyGui", {prefix: "a"});
com.getElementById("button").addClickHandler(handler);
}
}
}
Would really appreciate a work-around if possible, if that is not possible then please tell me what you would suggest because I'm not sure how best to get around this.
Thanks guys.
All handler functions must be top level functions on your script. It's not possible to have it inside an object like this.

Getting External Interface to work with swf object, can't figure out what I'm doing wrong

I'm using swf object to embed a swf. I'm trying to user external interface to to call a flash function from JS, but having trouble. I've searched all the docs, read a ton of stuff, and I'm convinced I'm doing it right. Can anyone point out where the problem might be?
var flashvars = {};
var params = {wmode:"opaque", allowscriptaccess:"always" };
var attributes = {id:"MySwf", name:"MySwf"};
swfobject.embedSWF("MySwf.swf", "flashContent", "600", "400", "9.0.0", "swfs/expressInstall.swf", flashvars, params, attributes,function(e){
if(e.success){
_flashRef = e.ref;
testExternalInterface();
}else{
alert("We are sorry, flash is required to view this content.");
}
});
function testExternalInterface(){
var swf = document.getElementById("MySwf");
swf.sendMeTheGoods("TEST TEST");
};
Above is the embed code and js function in my flash I have
if (ExternalInterface.available)
{
trace("adding external interface");
ExternalInterface.addCallback("sendMeTheGoods", sendMeTheGoods);
}
public function sendMeTheGoods(text:String):void
{
trace("setting vars")
trace(text);
txtYouSent.text = text;
}
I'm getting the error Uncaught TypeError: Object # has no method 'sendMeTheGoods'
I've tried both referenceing document.getElementById("MySwf"); and document.getElementById("flashContent"); and I get the error both ways. Any suggestions?
the external interface API is not immediately available, it takes a second for it to be ready. your callback is likely happening before Flash Player has initialized the EI API. try adding a delay in the callback, delaying the invocation of your 'test' function.
var swf = navigator.appName.indexOf("Microsoft") != -1 ? window["MySwf"] : document["MySwf"];

JSLint writing constructors that reference static variables

I'm writing a display class in Javascript (using jQuery) which may be instantiated before a web page has loaded. If the page isn't ready when the constructor is called, the instance is added to a static instances field for the class, which is iterated over when the page has loaded:
function MemDisplay(ready_callback) {
this.readyCallback = ready_callback;
if (MemDisplay.ready) {
this.linkToPage();
} else {
MemDislay.instances.push(this);
}
}
//this makes sure that the ready callback can be sent when the page has loaded
MemDisplay.ready = false;
MemDisplay.instances = [];
$(document).ready(function () {
var i;
MemDisplay.ready = true;
for (i = 0; i < MemDisplay.instances.length; i += 1) {
MemDisplay.instances[i].linkToPage();
} });
//example truncated for brevity
When I run this through JSLint, I get this error:
Problem at line 25 character 9:
'MemDislay' is not defined.
MemDislay.instances.push(this);
I need to reference MemDisplay.instances in the constructor, but the constructor is where MemDisplay is defined, so I'm puzzled about how to make this work while fitting within JSLint's guidelines. Is there a better way to do this? Should I just ignore JSLint in this instance?
JSLint here is actually highlighting a broader issue with the code without saying so.
You are referencing a class (MemDisplay) but never instantiating it as an object. I.e. you are treating the class like an already-instantiated object.
I've created a very simple equivalent to what you are trying to achieve (also at this JSFiddle)
function MyClass(p1, p2){
this.param1 = p1; //class member/property - use this to access internally.
if (this.param1 === 1){ //you might want to consider doing this as part of some setter method
alert("test");
}
this.MyMethod = function(){ //class method/function
alert("MyMethod Called");
};
}
var myObj = new MyClass(1,2); //instantiate
alert(myObj.param1); //get value of object member (you can set as well)
myObj.MyMethod(); //call a method
It'll take a bit of reorgansiation, but by declaring the values up front, you can get make JSLint happy.
My brain must have figured this out while I slept: the trick is to attach the field to the prototype, which seems pretty obvious now that I've thought of it, since that's what you have to do to define class methods.
The following checks out in JSLint, and demonstrates the sharing of a field between all instances of MyClass (or see this code on jsfiddle):
/*global alert */
function MyClass(name) {
this.name = name;
MyClass.prototype.field += 1;
}
MyClass.prototype.field = 0;
MyClass.prototype.myMethod = function () {
alert(this.name + "'s class's field is " + MyClass.prototype.field);
};
var myObj = new MyClass("first");
myObj.myMethod();
var myOtherObj = new MyClass("second");
myObj.myMethod();
myOtherObj.myMethod();
I'm not sure if there's a prettier way to do it, as having 'prototype' all over the place feels a bit excessive, on the other hand it could be a good thing because it makes it clear that prototype.field does not belong to the instance.