【问题标题】:Automatically replace string with image location in angular用角度的图像位置自动替换字符串
【发布时间】:2018-10-30 02:29:27
【问题描述】:

我正在为我仍然不太熟悉的 angular/ionic 格斗游戏构建框架数据应用程序。 每个角色都会有一个电子表格,显示他们所有动作的属性。

Example Spreadsheet

对于大多数列纯文本都可以,但对于“命令”和“级别”列,我希望程序能够解释我预先设置的字符串并将它们更改为 图像位置 .

单个容器由需要更改的多个命令组成。数字将参考数字键盘上的方向,而 A、B、K 和 G 将参考水平、垂直、踢球或后卫。 例如:字符串“6 A”应该导致加载向右箭头的图片 -> 和 A 按钮的图片。

要放入电子表格的值是从相应的 .ts 文件中加载的。 (水平移动示例)

export class Sc6mitsurugiPage {

  horMoves = [
    {move : "Twin Splinters", command: "A", level: "H", dmg: "12", grd: "-8", hit: "2", ch: "2", notes: ""},
    {move : "Twin Splinters (C)", command: "A A", level: "H", dmg: "28", grd: "-6", hit: "6", ch: "6", notes: ""},
    {move : "Reaver", command: "6 A", level: "H", dmg: "18", grd: "-2", hit: "8", ch: "8", notes: ""},
    {move : "Splitting Gold", command: "3 A", level: "M", dmg: "22", grd: "-6", hit: "4", ch: "STN", notes: ""},
    {move : "Knee Slice", command: "2 A", level: "SL", dmg: "12", grd: "-6", hit: "8", ch: "8", notes: ""},
    {move : "Shin Slicer", command: "1 A", level: "L", dmg: "38", grd: "-16", hit: "KND", ch: "KND", notes: ""},
    {move : "Shin Slicer Feint", command: "1 A~B", level: "M", dmg: "42", grd: "12", hit: "SLNC", ch: "SLNC", notes: ""},
    {move : "Dawn Breath", command: "4 A", level: "M", dmg: "28", grd: "-4", hit: "4", ch: "4", notes: ""},
    {move : "Dawn Breath ~Mist", command: "4 A 6", level: "H, SS", dmg: "28", grd: "-2", hit: "6", ch: "6", notes: ""},
    {move : "Knee Slice", command: "FC A", level: "SL", dmg: "12", grd: "-6", hit: "8", ch: "8", notes: ""},
    {move : "Silent Slash", command: "WR A", level: "M", dmg: "28", grd: "-7", hit: "5", ch: "5", notes: ""},
    {move : "Field Reave", command: "7 A", level: "H", dmg: "22", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Field Reave", command: "8 A", level: "H", dmg: "24", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Field Reave", command: "9 A", level: "H", dmg: "26", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Reverse Slice", command: "BT A", level: "H", dmg: "14", grd: "-6", hit: "6", ch: "7", notes: ""},
    {move : "Hidden Slice", command: "BT 2 A", level: "SL", dmg: "14", grd: "-6", hit: "8", ch: "8", notes: ""}
  ];

是否可以在仅将 单个字符串 作为命令/级别的值的情况下执行此操作,或者将 字符串数组 包含所有他们只是一个角色? 如果后者是真的,我将如何更改数据结构?

这是我当前 HTML 文件的相关部分:

<ion-row nowrap *ngFor="let horMove of horMoves">
  <ion-col class="sheetColumn wideCol">
    <div>{{horMove.move}}</div>
  </ion-col>
  <ion-col class="sheetColumn wideCol">
      <div>{{horMove.command}}</div>
  </ion-col>
  <ion-col class="sheetColumn narrowCol">
      <div>{{horMove.level}}</div>
  </ion-col>
  <ion-col class="sheetColumn narrowCol">
      <div>{{horMove.dmg}}</div>
  </ion-col>
  <ion-col class="sheetColumn narrowCol">
      <div>{{horMove.grd}}</div>
  </ion-col>
  <ion-col class="sheetColumn narrowCol">
      <div>{{horMove.hit}}</div>
  </ion-col>
  <ion-col class="sheetColumn narrowCol">
      <div>{{horMove.ch}}</div>
  </ion-col>
  <ion-col class="sheetColumn wideCol">
      <div><img class="inputImg" src="assets/imgs/input/a.png"><img class="inputImg" src="assets/imgs/input/b.png"><img class="inputImg" src="assets/imgs/input/k.png"><img class="inputImg" src="assets/imgs/input/g.png"></div>
  </ion-col>
</ion-row>

我需要如何在电子表格上显示相关图片,而不仅仅是纯文本?

【问题讨论】:

  • 你能发布你的 char 到图像路径之间的映射吗?
  • @yanis-git 映射为文件位置和命名约定?所有图像都在“assets/imgs/input/”中,并命名为 A、B、G 或从 1 到 9 的数字。我认为通过为包含命令字符串的 div 提供一个特定类,然后我可以在 Dom 中找到它,我认为我已经更接近解决方案了。这样我就设法用所需的图片交换了部分字符串。但是,我仍然不太确定如何逐个字符地完全实现此功能。
  • 对不起,我的意思是,命令 A 是这张图片,命令 WR A 是这张图片......我担心不明白你到目前为止试图实现的目标
  • @yanis-git 对不起,我不够清楚,感谢您的支持。例如,字符串“6A”应该获取 2 张图片。图片“assets/imgs/input/6.png”和图片“assets/imgs/input/A.png”并显示在“命令”栏中。 WR 代表“While rise”,BT 代表“Back turn”,但似乎我必须想出自己的命名约定并将其保留为一个字符。我现在已经非常接近解决方案了。稍后我会发布它以询问是否可以改进。

标签: angular dom ionic-framework


【解决方案1】:

我为最初的问题找到了解决方案,但是我很确定必须有更好的方法来解决这个问题,如果有人能发布另一个答案并说明这些,我会很高兴。

我将 command 中所有长于一个字母的命令更改为单个字母: FC(完全蹲伏)=> C,WR(上升时)=> W,BT(后转)=> T 等在我的阵列中。

  horMoves = [
    {move : "Twin Splinters", command: "A", level: "H", dmg: "12", grd: "-8", hit: "2", ch: "2", notes: ""},
    {move : "Twin Splinters (C)", command: "AA", level: "H", dmg: "28", grd: "-6", hit: "6", ch: "6", notes: ""},
    {move : "Reaver", command: "6A", level: "H", dmg: "18", grd: "-2", hit: "8", ch: "8", notes: ""},
    {move : "Splitting Gold", command: "3A", level: "M", dmg: "22", grd: "-6", hit: "4", ch: "STN", notes: ""},
    {move : "Knee Slice", command: "2A", level: "SL", dmg: "12", grd: "-6", hit: "8", ch: "8", notes: ""},
    {move : "Shin Slicer", command: "1A", level: "L", dmg: "38", grd: "-16", hit: "KND", ch: "KND", notes: ""},
    {move : "Shin Slicer Feint", command: "1M", level: "M", dmg: "42", grd: "12", hit: "SLNC", ch: "SLNC", notes: ""},
    {move : "Dawn Breath", command: "4A", level: "M", dmg: "28", grd: "-4", hit: "4", ch: "4", notes: ""},
    {move : "Dawn Breath ~Mist", command: "4A6", level: "H, SS", dmg: "28", grd: "-2", hit: "6", ch: "6", notes: ""},
    {move : "Knee Slice", command: "CA", level: "SL", dmg: "12", grd: "-6", hit: "8", ch: "8", notes: ""},
    {move : "Silent Slash", command: "WA", level: "M", dmg: "28", grd: "-7", hit: "5", ch: "5", notes: ""},
    {move : "Field Reave", command: "7A", level: "H", dmg: "22", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Field Reave", command: "8A", level: "H", dmg: "24", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Field Reave", command: "9A", level: "H", dmg: "26", grd: "-3", hit: "7", ch: "7", notes: ""},
    {move : "Reverse Slice", command: "TA", level: "H", dmg: "14", grd: "-6", hit: "6", ch: "7", notes: ""},
    {move : "Hidden Slice", command: "T2A", level: "SL", dmg: "14", grd: "-6", hit: "8", ch: "8", notes: ""}
  ];

然后我给包含命令的 div 一个特殊的类,所以它可以在 DOM 中找到。

<ion-col class="sheetColumn wideCol">
  <div class="command">{{horMove.command}}</div>
</ion-col>

之后,我进入了相应的 .ts 文件并开始操作 DOM。 此代码获取所有具有“命令”类的节点,然后我可以使用该列表遍历每个字母并创建一个新元素,然后将其附加到 DOM 中的正确位置。

  ionViewDidLoad() {
    let comArr = document.getElementsByClassName("command"); // Fetches a nodelist with all command cells
    for(let i = 0; i < comArr.length; i++){
      let elString = document.getElementsByClassName("command")[i].innerHTML; // Fetches string for this iteration of the loop
      document.getElementsByClassName("command")[i].innerHTML = ""; // Clears raw text from column
      for(let j = 0; j < elString.length; j++){
        let newImg = document.createElement("img"); // creates new <img>
        newImg.setAttribute("class", "inputImg"); //  sets class for <img>
        let alphaNum = elString[j];

        switch(alphaNum){

          //Diretions
          case "1": //Lower Left
            newImg.setAttribute("src", "assets/imgs/input/1.png");
            break;

          case "2": //Bottom
            newImg.setAttribute("src", "assets/imgs/input/2.png");
            break;

          case "3": //Lower Right
            newImg.setAttribute("src", "assets/imgs/input/3.png");
            break;

          case "4": //Left
            newImg.setAttribute("src", "assets/imgs/input/4.png");
            break;

          case "6": //Right
            newImg.setAttribute("src", "assets/imgs/input/6.png");
            break;

          case "7": //Upper Left
            newImg.setAttribute("src", "assets/imgs/input/7.png");
            break;

          case "8": //Top
            newImg.setAttribute("src", "assets/imgs/input/8.png");
            break;

          case "9": //Upper Right
            newImg.setAttribute("src", "assets/imgs/input/9.png");
            break;

          //Standard Buttons
          case "A": //Horizontal
            newImg.setAttribute("src", "assets/imgs/input/A.png");
            break;

          case "B": //Vertical
            newImg.setAttribute("src", "assets/imgs/input/B.png");
            break;

          case "K": //Kick
            newImg.setAttribute("src", "assets/imgs/input/K.png");
            break;

          case "G": //Guard
            newImg.setAttribute("src", "assets/imgs/input/G.png");
            break;

          //Multi Button
          case "M": // A~B Horizontal ~ Vertical
            newImg.setAttribute("src", "assets/imgs/input/M.png");
            newImg.setAttribute("class", "inputImgWide");
            break;

          case "N": // B~A Vertical ~ Horizontal
            newImg.setAttribute("src", "assets/imgs/input/N.png");
            newImg.setAttribute("class", "inputImgWide");
            break;

          case "O": // K~A Kick ~ Horizontal
            newImg.setAttribute("src", "assets/imgs/input/O.png");
            newImg.setAttribute("class", "inputImgWide");
            break;

          case "P": // K~B Kick ~ Vertical
            newImg.setAttribute("src", "assets/imgs/input/P.png");
            newImg.setAttribute("class", "inputImgWide");
            break;

          // States (Not complete yet)
          case "C": // Full Crouch
            break;

          case "T": // Back Turn
            break;

          case "R": // Run
            break;

          case "W": // While Rising
            break;
        }
        document.getElementsByClassName("command")[i].appendChild(newImg) // appends <img> child to corresponding cell
      }
    }
  }
}

到目前为止一切正常,但我担心一旦我将每个角色的所有特殊姿势添加到 switch 语句中,我会开始用完可能的角色,并且一切都会开始变得混乱。

Result Image

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2012-09-29
    相关资源
    最近更新 更多