2 回答

TA貢獻1860條經驗 獲得超9個贊
您可以將您的數據結構從簡單的opt1:sometext, 擴展到opt1:{text:sometext, nextpoint:2}, 這樣,您就可以以相對簡單的方式展示和遍歷您的新游戲。您只需要能夠執行 JSON 即可從此處繼續。
使用代碼示例,嘗試玩以下游戲的迷你版!
const sales = [
{
action: "Salesman Chad wants to talk!",
opt1: {
text: "Tell him you're not interested",
nextpoint: 1,
},
opt2: {
text: "Hear him out",
nextpoint: 2,
},
},
{
action: "He has a rebuttal for that! 'Sir, a classy man like you needs this car!'",
opt1: {
text: "We're not even at a dealership!",
nextpoint: 0,
},
opt2: {
text: "Ooo a new car",
nextpoint: 2,
}
},
{
action: "Ssssoooooooo, what kind of TV service do you have?",
opt1: {
text: "Tell him your landlord pays for it",
nextpoint: 1,
},
opt2: {
text: "Tell him you don't watch much TV",
nextpoint: 0,
}
}
];
function applyPoint(next) {
const point = sales[next];
$('#action-text').text(point.action);
$('#option-1').text(point.opt1.text);
$('#option-2').text(point.opt2.text);
$('#option-1').attr('data-nextpoint', point.opt1.nextpoint);
$('#option-2').attr('data-nextpoint', point.opt2.nextpoint);
}
$('#option-1').click(function(e) {
applyPoint($(this).attr('data-nextpoint'));
});
$('#option-2').click(function(e) {
applyPoint($(this).attr('data-nextpoint'));
});
applyPoint(0);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<section class="main-hub">
<div id="image-box">
<img id="opponent" src="img/salesman-chad.png">
</div>
<div id="text-box">
<h1 id="action-text"></h1>
</div>
<div id="options">
<div id="option-1"></div>
<div id="option-2"></div>
</div>
</section>
</body>
如您所見,小說本身const sales
作為一個簡單的 JSON 對象數組存儲在 中。在函數中applyPoint()
,我們從故事的當前點移動到故事的新點??刂七@兩個選項的按鈕是 jQuery 語法,$('#option-1').click(function(e) {...})
同樣適用于#option-2
.
定義完所有這些后,我通過以下方式簡單地從初始起點開始游戲/小說: applyPoint(1);
。

TA貢獻2036條經驗 獲得超8個贊
這個解決方案包括幾個概念(除了數據存儲),這些概念可以使開發更復雜的游戲更容易推理。
<body>
? ? <section class="main-hub">
? ? ? ? <div id="image-box">
? ? ? ? ? ? <img id="opponent" src="img/salesman-chad.png">
? ? ? ? </div>
? ? ? ? <div id="text-box">
? ? ? ? ? ? <h1 id="action-text"></h1>
? ? ? ? </div>
? ? ? ? <div id="options">
? ? ? ? ? ? <div id="option-1"></div>
? ? ? ? ? ? <div id="option-2"></div>
? ? ? ? </div>
? ? </section>
</body>
const sales = [
? ? {
? ? ? ? action: "Salesman Chad wants to talk!",
? ? ? ? opt1: {
? ? ? ? ? ? text: "Tell him you're not interested",
? ? ? ? ? ? nextpoint: 1,
? ? ? ? },
? ? ? ? opt2:? {
? ? ? ? ? ? text: "Hear him out",
? ? ? ? ? ? nextpoint: 2,
? ? ? ? },
? ? },
? ? {
? ? ? ? action: "He has a rebuttal for that! 'Sir, a classy man like you needs this car!'",
? ? ? ? opt1: {
? ? ? ? ? ? text: "We're not even at a dealership!",
? ? ? ? ? ? nextpoint: 0,
? ? ? ? },
? ? ? ? opt2: {
? ? ? ? ? ? text: "Ooo a new car",
? ? ? ? ? ? nextpoint: 2,
? ? ? ? }
? ? },
? ? {
? ? ? ? action: "Ssssoooooooo, what kind of TV service do you have?",
? ? ? ? opt1: {
? ? ? ? ? ? text: "Tell him your landlord pays for it",
? ? ? ? ? ? nextpoint: 1,
? ? ? ? },
? ? ? ? opt2: {
? ? ? ? ? ? text: "Tell him you don't watch much TV",
? ? ? ? ? ? nextpoint: 0,
? ? ? ? }
? ? }
];
const state = {
? index: 0,
? data: sales,
? get current() {
? ? return this.data[this.index]
? }
};
const ui = {
? action: document.querySelector('#action-text'),
? left: document.querySelector('#option-1'),
? right: document.querySelector('#option-2')
};
const update = (nextpoint) => {
? state.index = nextpoint;
? render();
};
const render = () => {
? ui.action.innerText = state.current.action;
? ui.left.innerText = state.current.opt1.text;
? ui.right.innerText = state.current.opt2.text;
};
ui.left.addEventListener('click', () => update(state.current.opt1.nextpoint));
ui.right.addEventListener('click', () => update(state.current.opt2.nextpoint));
render();
我添加的第一件事是游戲狀態對象。這實際上可以是任何東西,但將游戲狀態保存在可訪問區域(例如變量或變量集)而不是 DOM 中通常是個好主意。您可以想象添加patience、一個score值、一個timer、inventory、npcs或任何其他可能代表游戲的東西。index對于我們的案例,我們可能只需要對話中的當前信息,也許是所有dialog信息,然后是當前派生的對話值,這樣查找起來更容易。它實際上可以是讓您的生活更簡單的任何東西。
const state = {
? index: 0,
? data: sales,
? get current() {
? ? return this.data[this.index]
? }
};
接下來是更新方法。這可以被認為是我們游戲中的一個步驟。它負責獲取我們當前的游戲狀態和一些輸入和/integrating或導出下一個狀態。更新函數通常以固定間隔調用,比如每秒 60 次,但對于這款游戲,我們實際上只需要響應用戶。在更復雜的游戲中,更新函數調用會處理許多子系統,例如物理、動畫、游戲邏輯等。出于我們的目的,當我們收到用戶的新決定時,我們的游戲就會“開始”。
const update = (nextpoint) => {
? state.index = nextpoint;
? render();
};
代碼的最后一部分只是渲染當前的游戲狀態。渲染函數從字面上看當前狀態并確保 UI 反映這些值。該ui對象僅允許我以語義方式組織 ui 元素,這不是必需的。
如果你過去做過任何網絡編程,你可能會發現這與 MVC 或反應式 ui 框架非常相似。數據單向流動。
添加回答
舉報