简体   繁体   English

return语句未退出for循环

[英]return statement not exiting for loop

I want to hover over createJS labels and show english translation output only for the chinese label (Ex: "Wan" would show "Bowl") that I hover over. 我想将鼠标悬停在createJS标签上,并仅将我悬停在的中文标签上显示英文翻译输出(例如:“ Wan”将显示“ Bowl”)。

1) So I add all my labels on create to an array 1)所以我将所有创建时的标签添加到数组中

2) Then loop through all labels in hitTest() and add mouseover/mouseout handler to them 2)然后遍历hitTest()所有标签,并向其添加mouseover / mouseout处理程序

3) then handleMouseEvent() will call toggle english label on or off depending on hover 3)然后handleMouseEvent()将根据悬停调用切换英文标签的打开或关闭

4) then toggleItemLabel() loops through all createJS containers containing that label... if the hovered Chinese word matches the label that belongs to the container, then it will set that second english label's input, and show it. 4)然后toggleItemLabel()遍历包含该标签的所有createJS容器...如果悬停的中文单词与属于该容器的标签匹配,则它将设置该第二个英语标签的输入,并显示它。

This works fine, except the toggleItemLabel() seems to continue looping through all labels in allMyLabels[] even after it has found a match and returned. 效果很好,除了toggleItemLabel()似乎在找到匹配并返回后似乎继续遍历allMyLabels[]所有标签外。

As you can see from the output, it's showing 4 outputs... which means it's not returning/exiting the loop when it finds a match. 从输出中可以看到,它显示了4个输出...这表示找到匹配项时,它不返回/退出循环。

Why is this? 为什么是这样?

Output: 输出:

Lee Xian Sheng, Lee Xian Sheng 
Lee Xian Sheng, Lee Xian Sheng 
Lee Xian Sheng, Lee Xian Sheng 
Lee Xian Sheng, Lee Xian Sheng 

在此处输入图片说明

function hitTest() {
    for (var i = 0 ; i < allMyLabels.length; ++i) {
        allMyLabels[i].on("mouseover", handleMouseEvent);
        allMyLabels[i].on("mouseout", handleMouseEvent);
    }
}

function handleMouseEvent(evt) {
    for (var i = 0 ; i < allMyLabels.length; ++i) {
        if (evt.type === "mouseover") { 
            toggleItemLabel(allMyLabels[i].text, false);        
        }
        else {
            toggleItemLabel(allMyLabels[i].text, true);
        }
    }
}

function toggleItemLabel(word, hide) {

    for (var i = 0; i < All_Items_Array.length; i++) {
        for (var j = 0; j < All_Items_Array[i].children.length; j++) {
            log(word + ", " + All_Items_Array[i].children[j].children[1].text);
            if (word === All_Items_Array[i].children[j].children[1].text) {
                if (hide) {
                    return All_Items_Array[i].children[j].children[2].text = "";
                }
                else {
                    return All_Items_Array[i].children[j].children[2].text = All_Items_Array[i].children[j].name;
                }
            }
        }
    }
    stage.update();
}

EDIT: to give some insight as to what is in All_Items_Array: 编辑:提供有关什么All_Items_Array的一些见解:

When a new item is created: 创建新项目时:

function createNewItem(itemObj) {
    if (itemObj.name != "undefined") {
        if (!checkIfExists(itemObj.name) || itemObj.quest_name == null) {       
            container = new createjs.Container();
            bitmap = new createjs.Bitmap(itemObj.path);
            container.x = itemObj.x;
            container.y = itemObj.y;
            container.name = itemObj.name;

            container.taskNum = 0;

            cn_label = new createjs.Text(itemObj.cn_name, "14px Arial", "white");   
            cn_label.y = -45;

            en_label = new createjs.Text("", "14px Arial", "black");    
            en_label.y = -25;           

            //loop through all labels, check hittest for each
            allMyLabels.push(cn_label);

            container.addChild(bitmap, cn_label, en_label);

            container.cn = itemObj.cn_name;

            if (itemObj.itemType === "newItem") {
                container.count = 0;
                ContainerOfItems.addChild(container);
            }
            else {
                container.questName = itemObj.quest_name;
                ContainerOfPeople.addChild(container)
            }       
        }
        else {
            log(itemObj.quest_name + " already added...");
        }
    }
    hitTest(); //add mouse listeners to newly added labels
    stage.update();
}

And All_Items_Array.push(ContainerOfPeople, ContainerOfItems); All_Items_Array.push(ContainerOfPeople, ContainerOfItems);


EDIT 2 : tried break statements in the loop instead of returns: 编辑2 :在循环中尝试了break语句,而不是返回:

Now when I hover over label, it returns english labels for all chinese labels: 现在,当我将鼠标悬停在标签上时,它将为所有中文标签返回英文标签:

在此处输入图片说明

The first console.log() outputs all items in array, regardless if word===label text or not. 第一个console.log()输出数组中的所有项目,无论是否word===label text This is wrong. 错了 Hovering over wan should only return when wan === wan : 将鼠标悬停在wan仅应在wan === wan时返回:

wǎn , Lee Xian Sheng 
wǎn , fàn 
wǎn , shūcài 
wǎn , wǎn  
text: bowl, result: bowl 

Updated toggleItemLabel() code: 更新了toggleItemLabel()代码:

function toggleItemLabel(word, hide) {
    var result = "", label = "";
    for (var i = 0; i < All_Items_Array.length; i++) {
        for (var j = 0; j < All_Items_Array[i].children.length; j++) {
            log(word + ", " + All_Items_Array[i].children[j].children[1].text);
            if (word === All_Items_Array[i].children[j].children[1].text) {
                label = All_Items_Array[i].children[j].children[2];
                if (hide) {
                    result = "";
                    break;
                }
                else {
                    result = All_Items_Array[i].children[j].name;
                    break;
                }
            }
        }
    }
    label.text = result;
    log("text: " + label.text + ", result: " +  result);
    return label;
}

I would rewrite that to look more like 我将其重写为更像

function hitTest() {
    $(allMyLabels).on('mouseenter mouseleave', function(e) {
        var word = $(this).text, 
            hide = e.type == 'mouseenter';

        $.each(All_Items_Array, function(_, arr) {
            $.each(arr.children, function(_, child) {
                var w = child.children[1].text;
                log(word + ", " + w);
                if (word == w) {
                    child.children[1].text = hide ? child.name : '';
                }
            });
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM