有没有一种方法可以使数组主题内部的内容被单击? [英] Is there a way to make what's inside an array subject to be clicked?

查看:54
本文介绍了有没有一种方法可以使数组主题内部的内容被单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款类似于"Cookie clicker"游戏的游戏,我想知道是否有一种方法可以使阵列中的内容可点击,即像商店一样,用户可以在其中找到不同的物品可以单击并影响得分.

I'm making a game that's similar to the game 'Cookie clicker' and I was wondering if there was a way to make what is in an array clickable, ie, like a shop, where there are different items that the user can click and it effects the score.

var i;
var shop = new Array();
shop[0] = "Bins:" + "  " + "5";
shop[1] = "Shops:" + "  " + "100";
shop[2] = "Factories: " + "   " + "500";
shop[3] = "Warehouses:" + "   " + "2,000";
shop[4] = "Over seas:" + "   " + "8,000";

for (i=0; i<shop.length; i++){
  document.write(shop[i] + "<br>")
}

上面是存储商店中商品和商品价格的数组.我发现很难找到如何使这些项目可点击并使它们影响得分的方法.

The above is the array that stores what would be in the shop and the prices for the items. I'm finding it hard to find out how to make those items clickable and to make them effect the score.

推荐答案

稍微扩展一下Conor的答案,下面是针对每个商店"的循环解决方案-

Expanding on Conor's answer a bit, here is the solution done in a loop for each 'shop' -

http://jsfiddle.net/uLyJu/10/

您实际上应该使用对象数组,而不仅仅是字符串,因为您实际上为每个商店存储了两个名称,值和值.这样您就可以独立使用它们了.

You should really be using an array of objects and not just strings as you're really storing two things for each shop, a name and a value. That will let you use each independently.

var shops = new Array();
shops[0] = { name: "Bins:", value: 5 };
shops[1] = { name: "Shops:", value: 100 };
shops[2] = { name: "Factories:", value: 500 };
shops[3] = { name: "Warehouses:", value: 2000 };
shops[4] = { name: "Over seas:", value: 8000 };

这篇关于有没有一种方法可以使数组主题内部的内容被单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆