简体   繁体   English

如何加载带有图像链接的XML文件以用于2D JavaScript数组?

[英]How to load XML files with links to images for use in a 2D JavaScript Array?

I have been working on a little project in my free time sporadically since June. 自从六月以来,我一直在业余时间从事一个小项目。 It is an open source clone of SimTower with original, higher resolution graphics I have made. 它是SimTower的开源克隆,其中包含我制作的原始的更高分辨率的图形。

At first, I made a webpage that loaded a bunch of images into a 2D array to make a pretty picture that one could scroll through. 首先,我制作了一个网页,将一堆图像加载到2D数组中,以制作出可以滚动浏览的漂亮图片。 The main problem then was that certain images (the ones for lobbies) that were supposed to repeat until they reached the edge did not work correctly (because the space given for them is not equal to a multiple of their width and in the future the player will need to be able to draw lobbies with 1 tile-width accuracy). 然后主要的问题是某些图像(用于大厅的图像)应该重复播放直到到达边缘为止(由于为其提供的空间不等于其宽度的倍数,因此将来播放器将无法正常工作)将需要能够以1个图块宽度的精度绘制大厅。

I decided that the code I was using for loading images into an array could not be reused once I add a simulation and animations so I decided to put the information for the rooms and lobbies into separate XML files that should be loaded by JavaScript to gather the information (price and picture being what matters at the moment). 我决定,一旦添加模拟和动画,就无法重用用于将图像加载到数组中的代码,因此我决定将有关房间和大厅的信息放入单独的XML文件中,这些文件应由JavaScript加载以收集信息(当前重要的是价格和图片)。

One problem I have run into is that when trying to load the page with all the data created locally is that all 3 major browsers (Firefox, Chrome, and IE11) throw me error messages about not being able to open files using Http requests. 我遇到的一个问题是,当尝试使用本地创建的所有数据加载页面时,所有3种主要浏览器(Firefox,Chrome和IE11)都会向我抛出错误消息,提示无法使用Http请求打开文件。 I figured out how to get it to load in chrome by allowing local files to access other files in the same directory. 我想出了如何通过允许本地文件访问同一目录中的其他文件来将其加载到chrome中的方法。

Once I find out how to load XML files with JavaScript (both online and offline), I will need to figure out how to put the right data in the right place (at the moment, I just want the picture of the rooms to show up in the correct tiles and for their price to be deducted from the budget once placed). 一旦找到如何使用JavaScript(在线和离线)加载XML文件的方法,我将需要弄清楚如何将正确的数据放置在正确的位置(此刻,我只希望显示房间的图片)正确的磁贴,并在放置后从预算中扣除其价格)。

In short this is what I need help with first is this: 简而言之,这是我首先需要帮助的是:

Making use of the data within the XML files (what images to use at various times and what the stats should be) that have been loaded (I figured out how to load the XML offline by changing the property of the chrome shortcut). 利用已加载的XML文件中的数据(各种时间使用什么图像以及统计信息应该是什么)(我想出了如何通过更改chrome快捷方式的属性来离线加载XML)。

EDIT: I figured out how to get Chrome to work with letting offline files read other files in the same directory but I need help with the rest (utilizing the data and image links in the XML into the web page). 编辑:我想出了如何让Chrome允许脱机文件读取同一目录中的其他文件的方法,但是我需要其他方面的帮助(利用XML到网页中的数据和图像链接)。 I am practicing with a table first. 我先练习一张桌子。

I made a JSFiddle but I don't know how much good it will do: http://jsfiddle.net/AzemOcram/4uqz1na0/2/ 我做了一个JSFiddle,但我不知道它会做多少: http : //jsfiddle.net/AzemOcram/4uqz1na0/2/

Here is the XML file I have made: 这是我制作的XML文件:

<?xml version="1.0" encoding="utf-8"?>

<Room ID="Large Office">
<Type>Office</Type>
<Name>Large Office</Name>
<Name_Plural>Large Offices</Name_Plural>
<Cost>1200000</Cost>
<PaymentFrequency>monthly</PaymentFrequency>
<PaymentAmount_VeryLow>1100</PaymentAmount_VeryLow>
<PaymentAmount_Low>2200</PaymentAmount_Low>
<PaymentAmount_Average>3300</PaymentAmount_Average>
<PaymentAmount_High>4400</PaymentAmount_High>
<PaymentAmount_VeryHigh>5500</PaymentAmount_VeryHigh>
<TileWidth>10</TileWidth>
<TileHeight>1</TileHeight>
<DestructionCost>100</DestructionCost>
<CanBeDestroyed>True</CanBeDestroyed>
<Variant>
<Occupant>Empty</Occupant>
<Description>A large office room that you may lease out to companies. If demand for offices is low you can always advertise.</Description>
<dayImage>largeOffice-0-day-0.png</dayImage>
<nightImage>largeOffice-0-night-0.png</nightImage>
<Population>0</Population>
<Resource> 
<Electricity>-6</Electricity>
<Water>0</Water>
<Garbage>0</Garbage>
</Resource>
<CleaningNeeds>None</CleaningNeeds>
<CleaningFrequency>None</CleaningFrequency>
<CleaningDay>None</CleaningDay>
<TenantSoundSet>emptyrooms</TenantSoundSet>
</Variant>
<Variant>
<Occupant>1</Occupant>
<Description>A meeting room.</Description>
<dayImage>largeOffice-1-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-1-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>3</Water>
<Garbage>2</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Sat</CleaningDay>
<TenantSoundSet>ConferenceRoom</TenantSoundSet>
</Variant>
<Variant>
<Occupant>2</Occupant>
<Description>A meeting room.</Description>
<dayImage>largeOffice-3-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-3-nightoccupied-0.png</nightImage>
<Population>6</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>3</Water>
<Garbage>2</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Sat</CleaningDay>
<TenantSoundSet>ConferenceRoom</TenantSoundSet>
</Variant>
<Variant>
<Occupant>3</Occupant>
<Description>A meeting room.</Description>
<dayImage>largeOffice-4-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-4-nightoccupied-0.png</nightImage>
<Population>7</Population>
<Resource> 
<Electricity>-12</Electricity>
<Water>4</Water>
<Garbage>2</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Sat</CleaningDay>
<TenantSoundSet>ConferenceRoom</TenantSoundSet>
</Variant>
<Variant>
<Occupant>4</Occupant>
<Description>A meeting room.</Description>
<dayImage>largeOffice-5-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-5-nightoccupied-0.png</nightImage>
<Population>6</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>3</Water>
<Garbage>2</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Sat</CleaningDay>
<TenantSoundSet>ConferenceRoom</TenantSoundSet>
</Variant>
<Variant>
<Occupant>5</Occupant>
<Description>A large office room that has been leased out to companies. Very productive. Try to keep them satisfied.</Description>
<dayImage>largeOffice-6-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-6-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>2</Water>
<Garbage>5</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
<Variant>
<Occupant>6</Occupant>
<Description>A large office room that has been leased out to companies. Very productive. Try to keep them satisfied.</Description>
<dayImage>largeOffice-7-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-7-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>2</Water>
<Garbage>5</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
<Variant>
<Occupant>7</Occupant>
<Description>A large office room that has been leased out to companies. Very productive. Try to keep them satisfied.</Description>
<dayImage>largeOffice-8-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-8-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>2</Water>
<Garbage>5</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
<Variant>
<Occupant>8</Occupant>
<Description>A large office room that has been leased out to companies. Very productive. Try to keep them satisfied.</Description>
<dayImage>largeOffice-9-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-9-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>2</Water>
<Garbage>5</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
<Variant>
<Occupant>9</Occupant>
<Description>A large office room that has been leased out to companies. Very productive and high tech. Try to keep them satisfied.</Description>
<dayImage>largeOffice-10-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-10-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-12</Electricity>
<Water>2</Water>
<Garbage>3</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
<Variant>
<Occupant>10</Occupant>
<Description>A meeting room.</Description>
<dayImage>largeOffice-11-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-11-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-10</Electricity>
<Water>3</Water>
<Garbage>2</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Sat</CleaningDay>
<TenantSoundSet>ConferenceRoom</TenantSoundSet>
</Variant>
<Variant>
<Occupant>11</Occupant>
<Description>A large office room that has been leased out to companies. Very productive and high tech. Try to keep them satisfied.</Description>
<dayImage>largeOffice-12-dayoccupied-0.png</dayImage>
<nightImage>largeOffice-12-nightoccupied-0.png</nightImage>
<Population>5</Population>
<Resource> 
<Electricity>-12</Electricity>
<Water>2</Water>
<Garbage>3</Garbage>
</Resource>
<CleaningNeeds>Recommended</CleaningNeeds>
<CleaningFrequency>Weekly</CleaningFrequency>
<CleaningDay>Weekdays</CleaningDay>
<TenantSoundSet>Office</TenantSoundSet>
</Variant>
</Room>

In Chrome you can not load files offline using AJAX unless you start the browser with a special option like --allow-file-access-from-files . 在Chrome中,除非您使用--allow-file-access-from-files类的特殊选项启动浏览器,否则无法使用AJAX离线加载--allow-file-access-from-files See this issue , closed with "won't fix" . 看到此问题 ,并用“不会修复”关闭。

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

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