Phaser's textbox

Source: Internet
Author: User
Tags define function

Using phaser for a while, more and more like the game engine, but it is not as egret as a professional company and team to maintain, and provide a lot of tools, so phaser not so many tools, but really very useful.

In the game login interface, found that Phaser did not provide a TextBox control, in the official forum after the search has been the following conclusions

In most cases, the DOM approach is used to implement the TextBox.

This paper uses DOM to implement a TextBox that can be adapted to the Phaser canvas, as follows:

The implementation steps are as follows:

1. Adaptive screen program in Phaser provided template, specific to phaser official online Download the latest source code, found in the Source Temple folder fullscreen project, because the project to achieve the adaptive scaling of the screen.

2. Create a JS file named

3. Define function Htmltextbox (Game,x,y,width,height,text,style) in this file as the use class

4. Implementation of the main content

 This. game = game; This. parentelement = Game.canvas.parentNode;//Create a inputelement This. textboxelement = document.createelement (' input '); This. textBoxElement.style.position = ' absolute '; This. TextBoxElement.style.left = x+ ' px '; This. textBoxElement.style.top = y+ ' px '; This. TextBoxElement.style.width =width; This. TextBoxElement.style.height =height; This. Parentelement.insertbefore ( This. Textboxelement,game.canvas);
The above code adds an HTML input.
5. Adaptive screen
When the code above implements the update, the location and size of the DOM are calculated based on the new location, which automatically adapts to the screen size.
//update function to recalculate the location of the textboxHtmlTextBox.prototype.update =function(){    varCanvas = This. Game.canvas; varCANVASX =Canvas.offsetleft; varCanvasy =Canvas.offsettop; varCanvaswidth =Canvas.offsetwidth; varCanvasheight =Canvas.offsetheight; if( This. Oldcanvaswidth = = Canvaswidth && This. Oldcanvasheight = =Canvasheight&& This. oldcanvasx = = canvasx && This. Oldcanvasy = = Canvasy)return; varGamewidth = This. Game.world.width; varGameheight = This. Game.world.height; varWidthscanle = Canvaswidth/gamewidth;varHeightscanle = Canvasheight/gameheight;varXscanle = This. _x/gamewidth;varYscanle = This. _y/gameheight;varNEWX = canvasx + canvaswidth *Xscanle; varNewy = Canvasy + canvasheight *Yscanle; varNewwidth = This. _width *Widthscanle; varNewheight = This. _height *Heightscanle;  This. SetX (NEWX);  This. Sety (Newy);  This. SetWidth (Newwidth);  This. SetHeight (Newheight);  This. Oldcanvaswidth =Canvaswidth;  This. Oldcanvasheight =Canvasheight;  This. oldcanvasx =canvasx;  This. Oldcanvasy =Canvasy;}
Finally, attach the full source of this textbox.
Htmltextbox.js

Phaser's textbox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.