新人求助

新人求助,触摸事件监听问题~ 继续讨论:

想要实现的功能:
第二次触摸会把第一次触摸时生成的NewNote销毁后再生成新的NewNote

但是现在完全不生成NewNote

代码如下

cc.Class({
extends: cc.Component,

properties: {
	NewNotePrefab:{
		default:null,
		type:cc.Prefab
	},
	bar0:{
		default:null,
		type:cc.Node
	},
	bar1:{
		default:null,
		type:cc.Node
	},
},

setNoteFunction:function(){
	//把this.bar0给setNoteFunctionsub的targetbar
	this.setNoteFunctionsub(this.bar0);
    this.setNoteFunctionsub(this.bar1);
},

setNoteFunctionsub:function(targetbar){
    targetbar.on("touchstart",function(){
    	cc.NewNote.destory();
		var NewNote = cc.instantiate(this.NewNotePrefab);
		NewNote.parent = targetbar;
		NewNote.setPosition(0,0);
	},this);
},

onLoad () {
	cc.repeatForever(this.setNoteFunction());
},

start () {

},

});Musical Notation Translator.zip (710.8 KB)

demo已上传
完成进度30%左右
实现功能:五线谱音符翻译为简谱

这个是什么,能把demo传上来吗

demo已上传

this.NewNote在MaiinScene.js里面根本不存在

在InitialLize里面是 var NewNote

而在setNoteFunctionsub里面是 this.NewNote

谢谢指出错误
但是我现在还是不知道要怎么修改:sweat_smile:
我的理解是这样的:
根据API
this.target.destroy();
在 .destory 之前必须是一个node类型,所以我使用this.NewNote来获取NewNote这个节点

在你帮我指出错误之后,我的理解是:
NewNote不是一个节点,而是一个被赋值为节点的变量,所以不能用 this.NewNote获取它
那我应该怎么办呢?我尝试了把这行代码改为NewNote.destory();
似乎没什么用
而且我比较奇怪的是,这行代码出现了问题但是它是如何影响到下面的代码呢?为什么新的节点也不生成了?:joy:

那行代码错了后,js不会执行下去

你的理解是对的,但是this.NewNote.destory()是错误的,因为this.NewNote是undefined,undefined.destory()是不行的

但是我改成了NewNote也不行的原因是什么?因为NewNote是变量而不是节点吗?

你的this.NewNote是什么? 前面都没定义,node.destory()才行.

InitialLize:function(){//在点击之前就有一个NewNote存在
	this.NewNote = cc.instantiate(this.NewNotePrefab);
	this.NewNote.parent = this.bar0;
	this.NewNote.setPosition(0,0);
},

setNoteFunctionsub:function(targetbar){
var self = this;
    targetbar.on("touchstart",function(){
	self.NewNote.destroy();//只要发生点击NewNote就被销毁然后再生成一个NewNote
		self.NewNote = cc.instantiate(self.NewNotePrefab);
		self.NewNote.parent = targetbar;
		self.NewNote.setPosition(0,0);
	},this);
},

哇,行了!,大佬能不能解释一下呀:grinning:
为何一定要引入一个self变量啊:open_mouth:

//所以,为了在内部函数能使用外部函数的this对象,要给它赋值了一个名叫self的变量。
是这样的吗?

你的意思是在Initiallize函数里面this.NewNote = cc.instantiate(this.NewNotePrefab);所作出的定义在setNoteFunctionsub函数里是无效的?无法访问到它?
//所以,为了在内部函数能使用外部函数的this对象,要给它赋值了一个名叫self的变量。
是这样的吗?

看看js闭包

不用self也行的,on最后的参数是this

最好看一下this的用法

首先你的问题是this的用法,其次一个很low的问题你的destroy拼错了你之前是destory,你可以去下载你那个demo看一下