0

再会;

我正在继续尝试为 Cardfight 创建套牌构建应用程序!先锋

有一条规则,一副牌不能超过 4 张同一张牌,一副牌不能超过 50 张牌。

我正在努力做到这一点,以便我有一个计数器,当卡片添加到牌组时会增加,当牌从牌组中取出时会减少。但是这个计数器必须针对每张从 0 开始并在每次将具有特定名称的卡添加到套牌时增加 1 的卡。

我有一个 vanguard 对象和数据库数组,可以在其中轻松创建新的 Vanguard 对象。我在 Vanguard 对象 ( this.cardLimit = 0) 中有一个变量,它将最初将每张新卡的卡限制设置为 0。

然后当一张新卡[addToDeck]被添加到卡组中时,它将在数据库中为该特定卡增加一个计数器:database[addToDeck].cardLimit++,直到达到最大值 4,之后这个 if 语句将不起作用:

if (deck.length < 51 && database[addToDeck].cardLimit < 5)

我的问题是 - 正如您将在下面的代码中看到的那样 - 当我使用 jquery 删除卡时,我无法找到减少此值的方法。我所做的是将卡片名称动态添加到 div,当单击该 div 中的卡片名称时,它不仅会从 div 中删除卡片,还会通过拼接从卡组数组中删除卡片。

我不能database[addToDeck].cardLimit--像你在我的代码中看到的那样做,因为我使用 jQuery 和“this”来专门指div被点击的部分。

那么我的问题是....如何根据我迄今为止提供的代码规范减少计数器?当我的用于移除部分牌组的 Jquery(代码中的最后一个 jQuery 实例)运行时,我需要减少已移除的特定卡片的计数器。

如果您需要任何进一步的澄清,请告诉我......我用简单的术语解释我的问题并不容易。非常感谢您的阅读。

JavaScript / Jquery:

function Vanguard(name,grade,skill,power,shield,critical, type, trigger, nation, clan, race, flavor, effect, imageURL){
    this.name = name;
    this.grade = grade;
    this.skill = skill;
    this.power = power;
    this.shield = shield;
    this.critical = critical;
    this.type = type;
    this.trigger = trigger;
    this.nation = nation;
    this.clan = clan;
    this.race = race;
    this.flavor = flavor;
    this.effect = effect;
    this.imageURL = imageURL;
    this.cardLimit = 0;
};

var database = {};
database['asura kaiser'] = new Vanguard("Asura Kaiser", 3, "Twin Drive!!", 11000, "", 1, "Normal Unit", "None", "Star Gate", "Nova Grappler", "Battleroid", "The death blow! Kaiser Buster!", "<br/>[CONT](VC/RC):If you do not have another «Nova Grappler» vanguard or rear-guard, this unit gets [Power] -2000. <br/> [AUTO](VC):When this unit's drive check reveals a grade 3 «Nova Grappler», choose one of your rear-guards, and [Stand] it.", "http://images4.wikia.nocookie.net/__cb20120428001646/cardfight/images/thumb/a/a6/VGE_BT01-008EN.jpg/300px-VGE_BT01-008EN.jpg");
database['king of knights, alfred'] = new Vanguard("King of Knights, Alfred", 3, "Twin Drive!!", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Royal Paladin", "Human", "I command you under the name of the King of Knights! Warriors, heed my call!", "<br/>[CONT](VC):Your units cannot boost this unit. <br/> [CONT](VC):During your turn, this unit gets [Power]+2000 for each of your «Royal Paladin» rear-guards. <br/> [ACT](VC/RC):[Counter Blast (3)] Search your deck for up to one grade 2 or less «Royal Paladin», call it to (RC), and shuffle your deck.", "http://images2.wikia.nocookie.net/__cb20121009013434/cardfight/images/thumb/9/95/BT01-001EN_RRR.jpg/300px-BT01-001EN_RRR.jpg");
database['dragonic overlord'] = new Vanguard("Dragonic Overlord", 3, "Twin Drive!!", 11000, "", 1, "Normal Unit", "None", "Dragon Sanctuary", "Kagerou", "Dragon", "<br/>(TD02):Reduce all to ashes, flame of the apocalypse! Eternal Flame! <br/> (BT01):Burn, flame of despair! Eternal Flame!", "<br/>[CONT](VC/RC):If you do not have another «Kagero» vanguard or rear-guard, this unit gets [Power]-2000. <br/> [ACT](VC/RC):[Counter Blast (3)] Until end of turn, this unit gets [Power]+5000, gets '[AUTO](VC/RC):When this unit's attack hits an opponent's rear-guard, [Stand] this unit', and loses 'Twin Drive!!'.", "http://images1.wikia.nocookie.net/__cb20120131161306/cardfight/images/thumb/3/33/TD02-001EN.jpg/300px-TD02-001EN.jpg");
database['ceo amaterasu'] = new Vanguard("CEO Amaterasu", 3, "Twin Drive", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Oracle Think Tank", "Human", "<br/>(BT01):Shape my desire. Project it before me, Yata no Kagami! <br/>(EB05):Feel the flow of time. You will create the future.", "<br/>[CONT](VC):During your turn, if the number of cards in your hand is four or greater, this unit gets [Power] +4000. <br/> [AUTO](VC):At the beginning of your main phase, Soul Charge (1), look at the top card of your deck, and put that card on the top or the bottom of your deck. <br/> [AUTO](VC/RC):[Soul Blast (8) & Counter Blast (5)] When this unit's attack hits, you may pay the cost. If you do, draw up to five cards.)", "http://images2.wikia.nocookie.net/__cb20120831173054/cardfight/images/thumb/3/30/BT01-S05EN_SP.jpg/300px-BT01-S05EN_SP.jpg");
database['alfred early'] = new Vanguard("Alfred Early", 3, "Twin Drive!!", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Royal Paladin", "Human", "To preserve the smiles of his people, the young King leads knights to battle.", "AUTO: When this unit is placed on Vanguard Circle, choose up to one card named 'Blaster Blade' from your soul, and call it to Rear-guard Circle.", "http://images1.wikia.nocookie.net/__cb20120926194218/cardfight/images/thumb/5/5e/PR-0005EN.jpg/300px-PR-0005EN.jpg");

function printVanguard(p, name){
      for (var p in database[name]){
       if (p !== 'imageURL'){
         document.getElementById('output').innerHTML +=('<b>' + p.charAt(0).toUpperCase() + p.slice(1) + '</b>: ' + database[name][p] + '<br />');
      }}
};

$(document).ready(function() {
 $('#button').click(function(){
   $('#output').html(""); //clear old search
   $('#image').html(""); // clear old image
    var userInput = $('input[name=searchItem]').val();
    userInput = userInput.toLowerCase();
     $('#output').append(printVanguard(userInput, userInput));
   var image = $('<img>').attr('src', database[userInput].imageURL);
   $('#image').append(image);
 });
 var deck = [];

 $('#add').click(function(){
     var addToDeck = $('input[name=searchItem]').val();
     addToDeck = addToDeck.toLowerCase();
     database[addToDeck].cardLimit++;
     if (deck.length < 51 && database[addToDeck].cardLimit < 5){
      deck.push(addToDeck);
      $('#quantity').html("");
      $('#save').prepend('<div id="quantity">' + 'Total: ' + deck.length + '</div>');   
      $('#save').append('<div id="cardList">' + database[deck[deck.length-1]].name + '</div>');
      }
 });
  $(document).on('click','[id^=cardList]', function(){
   $(this).remove();
     deck.splice(deck[this], 1);
    $('#quantity').html("");
     $('#save').prepend('<div id="quantity">' + 'Total: ' + deck.length + '</div>');
  });
});

HTML 代码:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css" />
        <script type = "text/javascript" src='script.js'>
        </script>
        <title></title>
    </head>
    <body>
     <form name="searchForm">
         <input type="text" id="search" placeholder="Enter Card Name" name="searchItem"/>
     </form>
     <div id="button">Search!</div>
     <div id="add">Add to Deck</div>
    <br/>
    <div id="output"></div>
    <div id="image"></div>
    <div id="save"></div>
    </body>
</html>

小提琴:jsfiddle.net/8QR7q 由 Malik 提供

测试输入:“Alfred Early”、“CEO Amaterasu”、“Dragonic Overlord”、“Asura Kaiser”或“King of Knights, Alfred”

4

0 回答 0