因为坐标系转换的问题,需要覆盖NODE的转换X,Y属性的getter setter ,但是发现设置无效。
根据说明文档:http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/html5/v3/getter-setter-api/zh.md
var MySprite = cc.Sprite.extend({
getPositionY: function() {
// Your own implementation
var y=this._super();
var $y=cc.winSize.height-y;
cc.log(">>>>>>>>>>>>>>>>>> get y:"+y);
return $y;
},
setPositionY: function(y) {
// Your own implementation
var $y=cc.winSize.height-y;
cc.log("<<<<<<<<<<<<<<<<<<<<<<< set y:"+y+" ,"+$y);
this._super($y);
}
});
var sp=new MySprite();
sp.y=20; //cc.log 不输出任何东西
sp.setPositionY(30)//必须如此显示书写。
我需要通过sp.x sp.y的方式来访问属性,请问最佳的方法是什么?
是否要自己用原始的JS方法来设置getter setter ?那样设置的 貌似没办法继承,得自己改很多Class的extend方法才行。
PS:吐槽下,cc.defineGetterSetter(object, “propertyName”, getterFunc, setterFunc); 这个根本无用(难道我不会用吗?反正怎么都试过 不起作用的)
======使用原始的JS方式=======
MySprite.prototype.createGetterSetter=function(){
this.defineGetter(“y”, function(){
var $y=cc.winSize.height- this.getPositionY();
return $y;
});
this.defineSetter(“y”, function(val){
var $y=cc.winSize.height-val;
this.setPositionY( $y);
});
}
PS:
原来是要传入prototype…
cc.defineGetterSetter(MySprite.prototype,“y”,function(){
var $y=- this.getPositionY();
trace(“get y:”+this.getPositionY()+"to "+$y);
return $y;
},function(val){
var $y=-val;
trace(“set y:”+val+"to "+$y);
this.setPositionY( $y);
});