JS - 删除相同类型的每个对象

我正在编写一个画布游戏作为学校项目,我已经为“按钮”对象创建了一个构造函数,如下所示:


// button object constructor


function button(xL, xR, yT, yB) {

    this.xLeft = xL;

    this.xRight = xR;

    this.yTop = yT;

    this.yBottom = yB;

    this.width = xR - xL;

    this.height = yB - yT;

    this.drawMe = function() {

        neonWariorPlayArea.context.strokeStyle = "blue";

        neonWariorPlayArea.context.beginPath();

        neonWariorPlayArea.context.rect(this.xLeft, this.yTop, this.width, this.height);

        neonWariorPlayArea.context.stroke();

    }

}


button.prototype.clicked = function() {

    if (this.xLeft <= mouseX && mouseX <= this.xRight && this.yTop <= mouseY && mouseY <= this.yBottom) {

        return true;


    }

}

现在我遇到了一个我不知道如何解决的问题,那就是如何删除已经创建的每个按钮?我需要这个,因为当我更改屏幕(例如从主菜单到角色创建者)时,按钮仍然存在并且可单击。我试图创建某种数组,在其中我将保存所有按钮对象,然后循环该数组删除数组的每个元素。


var buttons = new Array();


playBtn = new button(500, 650, 50, 100);

tutorialBtn = new button(500, 760, 110, 160);

scoreBtn = new button(500, 670, 180, 230);


buttons.push(playBtn, tutorialBtn, scoreBtn);


function deleteBtns() {

    buttons.forEach(iterate);

}


function iterate(item, index) {

    console.log(index);

    delete buttons[index];

}

现在我已经到了我没有想法的地步,我的谷歌fu不够强大。感谢您的帮助或建议。


偶然的你
浏览 132回答 3
3回答

互换的青春

您可以按按钮来消除不需要的按钮,并重新分配给数组。.filter删除所有您可以通过分配:blank arraybuttons = []要获得唯一的按钮,您可以创建一个唯一的功能。function unique(buttons) {&nbsp; let set = new Set();&nbsp; let filtedBtns = [];&nbsp; buttons.forEach((btn) => {&nbsp; &nbsp; if (!set.has(btn.toString())) {&nbsp; &nbsp; &nbsp; filtedBtns.push(btn);&nbsp; &nbsp; &nbsp; set.add(btn.toString());&nbsp; &nbsp; }&nbsp; });&nbsp; return filtedBtns;}var buttons = new Array();function button(x, y, a, b) {&nbsp; this.x = x;&nbsp; this.y = y;&nbsp; this.a = a;&nbsp; this.b = b;&nbsp; this.toString = () => {&nbsp; &nbsp; return `${x}::${y}::${a}::${b}`;&nbsp; };}playBtn = new button(500, 650, 50, 100);tutorialBtn = new button(500, 760, 110, 160);duplicateBtn = new button(500, 760, 110, 160);scoreBtn = new button(500, 670, 180, 230);buttons.push(playBtn, tutorialBtn, duplicateBtn, scoreBtn);function unique() {&nbsp; let set = new Set();&nbsp; let filtedBtns = [];&nbsp; buttons.forEach((btn) => {&nbsp; &nbsp; if (!set.has(btn.toString())) {&nbsp; &nbsp; &nbsp; filtedBtns.push(btn);&nbsp; &nbsp; &nbsp; set.add(btn.toString());&nbsp; &nbsp; }&nbsp; });&nbsp; return filtedBtns;}function deleteBtns(index) {&nbsp; buttons = buttons.filter((_, i) => i !== index);}function deleteAll() {&nbsp; buttons = [];}console.log(buttons);deleteBtns(1);buttons = unique(buttons)console.log(buttons);deleteBtns(1);console.log(buttons);deleteAll();console.log(buttons);基于类的方法:class Button {&nbsp; constructor(x, y, a, b) {&nbsp; &nbsp; this.x = x;&nbsp; &nbsp; this.y = y;&nbsp; &nbsp; this.a = a;&nbsp; &nbsp; this.b = b;&nbsp; }&nbsp; toHash() {&nbsp; &nbsp; return `${this.x}::${this.y}::${this.a}::${this.b}`;&nbsp; }}class Buttons {&nbsp; constructor(...btns) {&nbsp; &nbsp; this.buttons = btns;&nbsp; }&nbsp; unique() {&nbsp; &nbsp; let set = new Set();&nbsp; &nbsp; let filtedBtns = [];&nbsp; &nbsp; this.buttons.forEach((btn) => {&nbsp; &nbsp; &nbsp; if (!set.has(btn.toHash())) {&nbsp; &nbsp; &nbsp; &nbsp; filtedBtns.push(btn);&nbsp; &nbsp; &nbsp; &nbsp; set.add(btn.toHash());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; this.buttons = filtedBtns;&nbsp; }&nbsp; deleteBtns(index) {&nbsp; &nbsp; this.buttons = this.buttons.filter((_, i) => i !== index);&nbsp; }&nbsp; deleteAll() {&nbsp; &nbsp; this.buttons = [];&nbsp; }}let playBtn = new Button(500, 650, 50, 100);let tutorialBtn = new Button(500, 760, 110, 160);let duplicateBtn = new Button(500, 760, 110, 160);let scoreBtn = new Button(500, 670, 180, 230);const btns = new Buttons(playBtn, tutorialBtn, duplicateBtn, scoreBtn);console.log(btns.buttons);btns.unique(); // update uniqueconsole.log(btns.buttons);btns.deleteBtns(1); // delete by indexconsole.log(btns.buttons);btns.deleteAll(); // delete allconsole.log(btns.buttons);

天涯尽头无女友

以下是我的做法:<canvas id=canvas width=350 height=180></canvas><script>&nbsp; &nbsp; function object(x, y, c) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; &nbsp; &nbsp; this.color = c&nbsp; &nbsp; &nbsp; &nbsp; this.draw = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.fillStyle = this.color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.beginPath();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.arc(this.x, this.y, 10, 0, 2 * Math.PI, false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.fill();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; var canvas = document.getElementById('canvas')&nbsp; &nbsp; var context = canvas.getContext('2d')&nbsp; &nbsp; var img = new Image;&nbsp; &nbsp; img.onload = draw;&nbsp; &nbsp; img.src = "http://i.stack.imgur.com/UFBxY.png";&nbsp; &nbsp; var objects = []&nbsp; &nbsp; objects.push(new object(200, 100, "blue"));&nbsp; &nbsp; objects.push(new object(250, 90, "cyan"));&nbsp; &nbsp; function draw() {&nbsp; &nbsp; &nbsp; &nbsp; context.clearRect(0, 0, canvas.width, canvas.height)&nbsp; &nbsp; &nbsp; &nbsp; context.drawImage(img, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; objects.forEach(o => o.draw());&nbsp; &nbsp; }&nbsp; &nbsp; canvas.onclick = function (e) {&nbsp; &nbsp; &nbsp; &nbsp; objects.push(new object(Math.random()*200, Math.random()*100, "red"));&nbsp; &nbsp; &nbsp; &nbsp; if (objects.length > 5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objects = objects.filter(o => o.color != "red");&nbsp; &nbsp; &nbsp; &nbsp; draw()&nbsp; &nbsp; }</script>将注意力集中在加载图像时调用,每次单击画布时也会调用它。function draw()在每次绘制时,我们都会擦除整个画布并再次绘制它,这可能听起来很多,但在现代浏览器上,这种情况发生得如此之快,以至于您看不到任何闪烁。我创建了一个数组,单击时,我在随机位置添加一个新对象,第四次单击时,我将删除所有红色对象,并在连续单击时添加新对象。objects = []有人建议从画布上清除矩形部分,但这会导致问题,因为你可以看到我也画了一个背景图像,如果我清除一个部分,它也可以清除背景,而不是最好的行为,我相信你的形状会变得更加复杂。关键是要清除整个画布并再次绘制所有内容。考虑到这一点,您需要对触发删除的事件所执行的所有操作只需循环访问数组并删除不需要的那些。

慕工程0101907

元素本身只是一个位图,不提供有关任何绘制对象的信息。[来源&nbsp;https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API]canvas您不能“选择”,然后从画布中清除对象。而是使用该方法从画布中清除矩形区域。clearRect检查堆栈闪电战以获取工作示例&nbsp;https://stackblitz.com/edit/js-j81gxm?file=index.js我又向按钮类添加了两个属性描边宽度 - 描边的宽度始终添加到矩形的尺寸中。例如:如果矩形的宽度和笔触为1px,则矩形的有效宽度变为1 + 100 + 1 = 102px。可以使用 strokeWidth 属性来设置变量 strokeWidth,并将其用作函数的参考remove活动按钮 - 跟踪按钮是否处于活动状态在类上引入了一种方法 此方法将删除矩形区域并将按钮标记为非活动状态removebuttondeleteBtns函数将过滤掉按钮数组,同时调用单个按钮的 remove 方法。const canvas = document.getElementById('app');var neonWariorPlayArea = canvas.getContext("2d");function button(xL, xR, yT, yB) {&nbsp; &nbsp; this.xLeft = xL;&nbsp; &nbsp; this.xRight = xR;&nbsp; &nbsp; this.yTop = yT;&nbsp; &nbsp; this.yBottom = yB;&nbsp; &nbsp; this.width = xR - xL;&nbsp; &nbsp; this.height = yB - yT;&nbsp; &nbsp; this.active = true;&nbsp; &nbsp; this.strokeWidth = 1;&nbsp; &nbsp; this.drawMe = function() {&nbsp; &nbsp; &nbsp; &nbsp; neonWariorPlayArea.strokeStyle = "green";&nbsp; &nbsp; &nbsp; &nbsp; neonWariorPlayArea.beginPath();&nbsp; &nbsp; &nbsp; &nbsp; neonWariorPlayArea.rect(this.xLeft, this.yTop, this.width, this.height);&nbsp; &nbsp; &nbsp; &nbsp; neonWariorPlayArea.stroke();&nbsp; &nbsp; }}button.prototype.clicked = function() {&nbsp; &nbsp; if (this.xLeft <= mouseX && mouseX <= this.xRight && this.yTop <= mouseY && mouseY <= this.yBottom && this.active ) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}button.prototype.remove = function() {&nbsp; &nbsp; this.active = false;&nbsp; &nbsp; neonWariorPlayArea.clearRect ( this.xLeft-this.strokeWidth, this.yTop-this.strokeWidth, this.width+this.strokeWidth*2, this.height+this.strokeWidth*2 );}var buttons = new Array();const playBtn = new button(0, 50, 0, 50);const tutorialBtn = new button(50, 100, 50, 100);const scoreBtn = new button(100, 100, 50, 50);playBtn.drawMe ();tutorialBtn.drawMe ();scoreBtn.drawMe ();buttons.push(playBtn, tutorialBtn, scoreBtn);function deleteBtns() {&nbsp; &nbsp; buttons = buttons.filter ( btn => {&nbsp; &nbsp; &nbsp; btn.remove ();&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; })}deleteBtns ();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript