简体   繁体   English

HTML5游戏(画布)-UI技术?

[英]HTML5 Game (Canvas) - UI Techniques?

I'm in the process of building a JavaScript / HTML5 game (using Canvas) for mobile (Android / iPhone/ WebOS) with PhoneGap. 我正在使用PhoneGap开发用于移动设备(Android / iPhone / WebOS)的JavaScript / HTML5游戏(使用Canvas)。 I'm currently trying to design out how the UI and playing board should be built and how they should interact but I'm not sure what the best solution is. 我目前正在尝试设计UI和游戏板的构建方式以及它们之间的交互方式,但我不确定最佳解决方案是什么。 Here's what I can think of - 这是我能想到的-

  1. Build the UI right into the canvas using things like drawImage and fillText 使用drawImage和fillText之类的东西将UI构建到画布中
  2. Build parts of the UI outside of the canvas using regular DOM objects and then float a div over the canvas when UI elements need to overlap the playing board canvas. 使用常规DOM对象在画布外部构建UI的一部分,然后在UI元素需要与游戏板画布重叠时在画布上浮动div。

Are there any other possible techniques I can use for building the game UI that I haven't thought of? 我有没有想到的其他可用来构建游戏UI的技术? Also, which of these would be considered the "standard" way (I know HTML5 games are not very popular so there probably isn't a "standard" way yet)? 另外,其中哪一种将被视为“标准”方式(我知道HTML5游戏不是很流行,因此可能还没有“标准”方式)? And finally, which way would YOU recommend / use? 最后,您会推荐/使用哪种方式?

Many thanks in advance! 提前谢谢了!

EDIT 编辑

I've moved this question over to gamedev.stackoverflow.com. 我已经将此问题移至gamedev.stackoverflow.com。 You can find the new question here: https://gamedev.stackexchange.com/questions/7090/html5-game-canvas-ui-techniques/7115#7115 您可以在这里找到新问题: https : //gamedev.stackexchange.com/questions/7090/html5-game-canvas-ui-techniques/7115#7115

You can do it a million ways. 您可以做到一百万种方式。 However you feel most comfortable and your engineers feel most confident. 但是,您会感到最舒适,工程师也会感到最有信心。

If you're looking for inspiration or a code example, 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) {
  // ...
}

This way I'm able to separate out game drawing and game events from menu drawing and menu events. 这样,我就可以将游戏绘图和游戏事件与菜单绘图和菜单事件分开。 It also gives me leeway to use game/animation elements in my menus should I want to make them look real pretty. 如果我想让菜单看起来更漂亮,这也让我有余地在菜单中使用游戏/动画元素。

(I posted this at the twin gamedev discussion too) (我也在双胞胎gamedev讨论中发布了此内容)

I do not think that there is a "standard" for this. 我认为这没有“标准”。 It highly depends on your UI. 这在很大程度上取决于您的UI。 I think using the DOM elements is better in most cases, since you do not need to build all of the UI components, events, etc. yourself. 我认为在大多数情况下使用DOM元素会更好,因为您不需要自己构建所有UI组件,事件等。 They can be styled with CSS to achieve the desired look. 可以使用CSS设置样式,以实现所需的外观。 If this is not enough, you'll probably need to build the interface elements yourself, but you should make sure that this is really needed. 如果这还不够,那么您可能需要自己构建接口元素,但是您应该确保确实需要这样做。 It is probably a huge amount of work to roll your own solution. 推出自己的解决方案可能需要大量工作。

Try this : 尝试这个 :

With Visual js you can setup page like this : 使用Visual js,您可以像这样设置页面:

Visual-JS multiplatform game engine windows GUI - source editor Visual-JS多平台游戏引擎Windows GUI-源代码编辑器

OnPage editor - for design OnPage编辑器-用于设计

You will get : 你会得到 :

*99% canvas 2d * 99%帆布2D

Add new object 添加新对象

Create webcam component (nui or normal) 创建网络摄像头组件(nui或普通)

Create coallision (basic - rect) 创建合并(基本-矩形)

Create textBox (virtual keyboard for mobile) 创建文本框(用于移动设备的虚拟键盘)

Create particle 创建粒子

Atach player (basic movement) Atach播放器(基本运动)

MultipEER (Networking)* MultipEER(网络)*

localStarage 本地星空

App created from visual js always work on all browsers(mobile / desktop). 通过Visual JS创建的应用始终可在所有浏览器(移动/台式机)上运行。 Networking - webRTC - multipeer 网络-WebRTC-多对等

Try online at : https://jsfiddle.net/user/zlatnaspirala/fiddles/ 在线尝试: https : //jsfiddle.net/user/zlatnaspirala/fiddles/

Api look like this : api看起来像这样:

Application Programming Interface Documentation for Visual JS 0.5 > Visual JS 0.5的应用程序编程接口文档>

GAME_OBJECT is main object in this framework. GAME_OBJECT是此框架中的主要对象。

1) Adding new game object (name will be 'GO' ):   

HELLO_WORLD.ENGINE.MODULES.ACCESS_MODULE("STARTER").NEW_OBJECT("GO" ,
x , y , w , h , speed )

HELLO_WORLD.ENGINE.MODULES.ACCESS_MODULE("STARTER").NEW_OBJECT( "GO" ,
45   , 45 , 10 , 10 , 10)


// 2) Adding image or animation : 

// DRAW TYPE can be   // 'DRAW_FRAME' no animation   // 'LOOP' playing
animation  // this number '1111123123' is ID  can be any number 
//ANIMATION (  surf ,TYPE_, FrameIndex ,source , PARENT , ID , blink_
, min_ , max_ , step , speed_ , opacity_  )
HELLO_WORLD.ENGINE.MODULES.ACCESS_MODULE("STARTER").GAME_OBJECTS.ACCESS("GO").CREATE_ANIMATION(
SURF , "DRAW_FRAME" , 6 , RESOURCE.Tiles  , 1111123123 , "no" ,
1,11,1,1,1) 


3)Disable draging   GO.DRAG = false;

//  RESOURCE.NAMEOFFOLDERANIMATION

add folder "Tiles"  with images in folder /res/ and run node res.js   

// refresh page and you will get

RESOURCE.Tiles ready for use !   
// MAKE MODULE ACCESS EASY  var

STARTER = HELLO_WORLD.ENGINE.MODULES.ACCESS_MODULE("STARTER");    

STARTER.GAME_OBJECTS.ACCESS("GO").CREATE_ANIMATION( SURF ,
"DRAW_FRAME" , 6 , RESOURCE.Tiles  , 1111123123 , "no" , 1,11,1,1,1) 


//DRAG initial value is true GO.DRAG = false;
//setup quard height  =  width  GO.POSITION.DIMENSION.H = GO.POSITION.DIMENSION.W;

4) EVENTS  FOR MOUSE AND MOBILE TOUCH HANDLED
//CLICK OR TOUCH START  GO.TAP = function(){  

//this make point directing to the game object instance   
//  this.NAME or this.ANIMATION.CURRENT_FRAME   };


GO.TOUCH_DOWN = function(){  

STARTER.DESTROY_OBJECT("GO")        console.log("THIS MUST BE TERMINATED
ON MOUSE DOWN or TOUCH_DOWN  : " + this.NAME);
//this.DESTROY_ME_AFTER_X_SECUND( 100 );    //console.log("THIS MUST BE
TERMINATED ON CLICK : " + this.NAME);      };

GO.TOUCH_MOVE = function(){  
console.log("HOVER ON OBJECT OR MOBILE TOUCH_MOVE  : " + this.NAME);   };

GO.TOUCH_UP = function(){  
console.log("MOUSE UP ON OBJECT OR MOBILE TOUCH_UP  : " + this.NAME);   };*

Download git 下载git

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

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