当前位置:网站首页>Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically

Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically

2022-06-11 06:46:00 Dare you look at the avatar for three seconds?

Problems encountered

 Insert picture description here
As shown in the figure , In order to realize the random increase of enemy aircraft and the first random flight of players on the stage, do you know , When checking the console, I found , When the enemy plane flies over the stage, it will not disappear automatically , So that the program will take up too much memory space , The situation is somewhat similar to the memory leak that is not released in time when dynamic memory allocation is implemented , So when the computer slows down at visible speed , How to make the enemy aircraft disappear automatically becomes more and more difficult
Before that, first understand a few concepts
Every enemy plane is loaded by us html One of the nodes in , Or labels

understand dom node

document object model---- Document object model
Our need is , After the enemy aircraft flies over the stage, the corresponding... Will be deleted automatically html node , Before deleting, we need to find a way to get the corresponding node of the aircraft flying out of the stage

dom The attribute acquisition problem of the node

Get the properties of all nodes ( Height or something ) Not directly in js Inside point , You can't get , This also applies jq Why

But the input field can be clicked directly value

Specific code :

// When you leave the stage, remember to remove dom node 
	setInterval(function(){
    
		
		
		for(i=0;i<enemy.length;i++){
    
			var tmp_enemy=parseFloat($(enemy[i]).css("top"));
			var tmp_stage=$(stage).height();
			// console.log(tmp_enemy); 
			// console.log(typeof($(tmp_stage).height()));
			 if(tmp_enemy>tmp_stage){
    
				$(enemy[i]).remove();
				console.log(" Removed successfully ")
				
			 }
			
		}
	
		
	},500)
原网站

版权声明
本文为[Dare you look at the avatar for three seconds?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020525471921.html