[英]Java to Javascript/HTML5 Canvas
I was wondering what the java to javascript/HTML5 translations were for these things: 1. Switching Activities/Layouts 2. Methods 3. Scanner Method (input via text box) 我想知道从Java到javascript / HTML5的翻译是什么:1.切换活动/布局2.方法3.扫描器方法(通过文本框输入)
I am writing a game basically that is written in javascript and HTML5. 我写的游戏基本上是用javascript和HTML5编写的。 HTML5 is being purely used to display the graphics via canvas. HTML5仅用于通过画布显示图形。 I am trying to create a menu for my game, but I was wondering how I would switch to another layout once I click the play button. 我正在尝试为我的游戏创建一个菜单,但是我想知道一旦单击播放按钮该如何切换到另一个布局。
You can do it a million ways. 您可以做到一百万种方式。 However you feel most comfortable. 但是,您感觉最舒适。
If you're looking for inspiration, here's one way that I do it. 如果您正在寻找灵感,这是我的一种方式。 I have a function that repeatedly draws a menu until a button is pressed. 我有一个反复绘制菜单直到按下按钮的功能。 When the button is pressed, the game loads and the old menu click event listeners are removed and new game click event listeners are added. 按下按钮后,将加载游戏,并删除旧的菜单单击事件侦听器,并添加新的游戏单击事件侦听器。 I also end the old draw loop of the menu and start a new game draw loop. 我还结束了菜单的旧绘制循环并开始了新的游戏绘制循环。 Here's some selected snippets to give you the idea of how its done: 以下是一些精选的片段,让您了解其操作方法:
Game.prototype.loadMenu = function() {
var game = this;
var can = this.canvas;
// now we can use the mouse for the menu
can.addEventListener('click', game.menuClickEvent, false);
can.addEventListener('touchstart', game.menuClickEvent, false);
// draw menu
this.loop = setInterval(function() { game.drawMenu() }, 30);
};
Game.prototype.drawMenu = function() {
// ... draw the menu
}
Game.prototype.loadLevel = function(levelstring) {
// unload menu
var can = this.canvas;
var game = this;
can.removeEventListener('click', game.menuClickEvent, false);
can.removeEventListener('touchstart', game.menuClickEvent, false);
if (this.loop) clearInterval(this.loop);
// ... other level init stuff
// now we can press keys for the game
//can.addEventListener('click', game.gameClickEvent, false);
can.addEventListener('touchstart', game.gameClickEvent, false);
can.addEventListener('keydown', game.gameKeyDownEvent, false);
this.loop = setInterval(function() { game.tick() }, 30);
}
// called from tick()
Game.prototype.draw = function(advanceFrame) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.