[英]Pixi.js stage parent
I have a graph object in javascript. 我在javascript中有一个图对象。 Inside this I have a pixi js stage as a private member.
在此内部,我有一个pixi js阶段作为私有成员。 In the stage I have a
PIXI.Graphics()
object where I will hold two vertical lines. 在该阶段中,我有一个
PIXI.Graphics()
对象,其中将保留两条垂直线。 I have methods in the graph object that manipulate these based on the clicks. 我在graph对象中有一些方法,可以根据点击操作这些方法。
The problem is: I set the stage to be interactive and then I set the stage.mousedown event to be a function, however in this function, my this
is the stage and not my Graph object. 问题是:我定的阶段是互动的,然后我设置stage.mousedown事件是一个函数,但是在此功能,我
this
是舞台,而不是我的图形对象。 I tried to access the parent but it is null. 我尝试访问父级,但它为null。 I tried to set the parent before creating the mousdown action by doing something like:
在尝试执行mousdown操作之前,我尝试通过以下方式设置父项:
this.stage.parent = this;
this.stage.mousedown = function(mouseData) {...}
but then the mousedown action never gets hit. 但随后mousedown操作永远不会被击中。 Any ideas?
有任何想法吗?
You'll need to make a variable in the same scope the mousedown function is created in. JS functions automatically know about variables in their parent scope. 您需要在创建了mousedown函数的范围内创建一个变量。JS函数会自动知道其父范围内的变量。 That could be a reference to the parent object scope or a function you want to call.
这可能是对父对象作用域或您要调用的函数的引用。
function PixiObject() {
var stage,
renderer;
this.init = function() {
stage = new PIXI.Stage(0x000000, true);
renderer = PIXI.autoDetectRenderer(480, 320, null, false);
document.body.appendChild(renderer.view);
var onStageDownA = function() {
console.log('onStageDownA called');
};
this.onStageDownB = function() {
console.log('onStageDownB called');
};
var self = this;
stage.mousedown = stage.touchstart = function() {
console.log(this); // stage
console.log(self); // pixi object
onStageDownA(); // calls function
self.onStageDownB(); // calls function
};
update();
}
function update() {
requestAnimFrame(update);
renderer.render(stage);
}
}
window.onload = function() {
var p = new PixiObject();
p.init();
};
Here's a working example: 这是一个工作示例:
http://codepen.io/ianmcgregor/pen/eFEJv http://codepen.io/ianmcgregor/pen/eFEJv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.