[英]HTML5 Game Development
I'm making an illustrated storybook, with the user clicking to read the next part of the story. 我正在制作带插图的故事书,用户单击以阅读故事的下一部分。 I've made three different scenes (three different pages). 我做了三个不同的场景(三个不同的页面)。 I can switch from scene One to scene two using eventListener, but I am not able to switch from scene Two to scene Three. 我可以使用eventListener从场景1切换到场景2,但是无法从场景2切换到场景3。
I want to be able to switch between the scenes as in 我希望能够像这样在场景之间切换
When the user clicks the mouse: 当用户单击鼠标时:
But I'm not sure how to do it. 但是我不确定该怎么做。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!
I should also mention I'm learning the basics so that I can move forward with Game Development. 我还应该提到我正在学习基础知识,以便我可以继续进行游戏开发。 And so this is a very basic code. 因此,这是一个非常基本的代码。
Here is my code: 这是我的代码:
var c = document.querySelector("#c");
var ctx = c.getContext("2d");
var x = document.getElementById("c");
//Scene One
function sceneOne(){
ctx.fillStyle = "rgb(235,247,255)";
ctx.fillRect(0,0,750,750);
ctx.fillStyle = "rgb(0, 85, 255)";
ctx.font = "70px Impact";
ctx.fillText("The Story of Winston", 40, 130);
};
//Scene Two
function sceneTwo() {
ctx.fillStyle = "rgb(173,239,255)";
ctx.fillRect(0,0,750,750);
ctx.fillStyle="rgb(7, 14, 145)";
ctx.fillText("Lil Winston is born!", 10, 100);
};
//Scee Three
function sceneThree() {
ctx.fillStyle = "rgb(173,239,255)";
ctx.fillRect(0,0,750,750);
ctx.fillStyle = "rgb(7, 14, 145)";
ctx.fillText("Winston grows up!", 10, 100);
};
//Calling scene one
sceneOne();
The example below contains the functionality that you require. 下面的示例包含所需的功能。
In summary: 综上所述:
currentScene
variable to keep track of which screen the user is currently on 我们使用currentScene
变量来跟踪用户当前所在的屏幕 switch
statement to call the appropriate function to display the next screen, depending on which screen the user is currently on 在事件监听器中,我们使用switch
语句调用适当的函数以显示下一个屏幕,具体取决于用户当前所处的屏幕 var c = document.querySelector("#c"); c.width = 800; c.height = 200; var ctx = c.getContext("2d"); // Init variable to store screen the user is on var currentScene; // Load next screen when user clicks the canvas c.addEventListener("click", function(){ switch(currentScene) { case 1: sceneTwo(); break; case 2: sceneThree(); break; case 3: sceneOne(); break; } }); //Scene One function sceneOne(){ currentScene = 1; ctx.fillStyle = "rgb(235,247,255)"; ctx.fillRect(0,0,750,750); ctx.fillStyle = "rgb(0, 85, 255)"; ctx.font = "70px Impact"; ctx.fillText("The Story of Winston", 40, 130); }; //Scene Two function sceneTwo() { currentScene = 2; ctx.fillStyle = "rgb(173,239,255)"; ctx.fillRect(0,0,750,750); ctx.fillStyle="rgb(7, 14, 145)"; ctx.fillText("Lil Winston is born!", 10, 100); }; //Scee Three function sceneThree() { currentScene = 3; ctx.fillStyle = "rgb(173,239,255)"; ctx.fillRect(0,0,750,750); ctx.fillStyle = "rgb(7, 14, 145)"; ctx.fillText("Winston grows up!", 10, 100); }; //Calling scene one sceneOne();
<canvas id="c"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.