Phaser学习笔记

功能特点

易维护代码(Easy Asset Loading)

Phaser可以加载图片,音频文件,数据文件,文本文件并自动解析精灵图和纹理地图集数据(出口纹理封隔器或Flash CS6)

屏幕(Cameras)

查看你的游戏窗口,你可以有多个摄像头,每个摄像头可以分为一个独特的大小区域,屏幕上任何位置,或窗口规模,缩放和旋转,并有摄像效果,如晃动,闪光灯和褪色。他们还可以按照精灵定制盲区。

精灵和游戏物体(Sprites and GameObjects)

Phaser内置游戏对象的物理属性,包括速度,加速度,弹性和阻力。它们可以缩放,旋转和碰撞在一起。scrollFactor属性允许他们一些漂亮的视差效果、屏幕以不同的速度滚动。

组(Groups)

组允许您轻松对象回收(例如:方便了发射弹药)游戏物体捆绑在一起。组也可以与其他团体或者精灵相撞。您还可以快速设置所有下级组和通话功能的可视性。

动画(Animation)

无论从精灵表或纹理地图集数据(JSON数组格式)支持动画精灵。动画播放控制,包括循环,一个fps基于定时器和自定义帧。

滚动区(Scroll Zones)

滚动的区域,可以在任何图像无缝地在任何方向滚动。或者,您可以创建多个滚动区域内一个单一的形象。不断循环有用的游戏背景和效果。

碰撞(Collision)

有一个四叉树的精灵,精灵,精灵集合或集合集合碰撞系统。以及大量的几何交集方法(线与线,线与圆,圆与矩形和多边形)。

颗粒(Particles)

Phaser带有一个基本的触发器类,可以在你的控制下放出精灵。无论是在突发或以平稳的速度触发粒子。颗粒可以反弹,旋转,缩放,并有自己的生命周期值。

输入(Input)

有本地的键盘,鼠标和触摸处理(MSPointer事件即将推出)

舞台(Stage)

舞台是你的游戏生活。您可以轻松地改变其在浏览器的位置,宽度,高度,背景颜色(透明的背景上,有你的游戏)和StageScaleMode内。

窗口(World)

窗口是你的游戏的中心。这就是所有的游戏对象,颗粒和其他对象的生存区。窗口可以是任意大小,对象被定位在它里面。您可以使用屏幕查看到窗口。

声音(Sound)

目前,Phaser使用一个基本的WebAudio实现进行播放。在这方面需要做很多工作,但是,因为它代表你可以加载和播放音乐和效果。

状态管理(State Management)

对于较大的游戏,它是非常有用,进入游戏状态down into States,即主菜单,1级,GAMEOVER,Phaser支持状态管理JavaScript和typescript。或者,你可以忽略的状态完全建立你游戏的几个核心功能。

缓存(Cache)

所有加载的资源都存储在一个易于访问游戏缓存,它可以清除之间的状态变化或通过坚持了整场比赛。

多格地图(Tilemaps)

Phaser将本地解析为CSV tilemap的数据或JSON接口的多格地图。它支持分层平铺图,图层的可见性和基于层tilemap的碰撞,所以你可以在一种无形的tilemap层,如果你想你的精灵碰撞。

游戏缩放(Game Scaling)

游戏缩放在你的控制之下。轻松删除URL/状态栏移动brpwsers(iOS和Android),并启用比例缩放,固定的大小和方向检查。

Key Features

We'll highlight a few of what we consider to be the most important features of Phaser from a developer's point of view: It's Just Plain JavaScript

This may sound like a strange 'feature', but it should actually be quite a compelling one. Phaser doesn't use any faux OO-style practises internally. There's no massive inheritance chain or component system, and you don't have to force your objects into any fixed class structure, either. It's a simple straight-forward prototype chain, the way JavaScript was meant to be used.

This doesn't mean you can't build your game in a structured way; far from it. All it means is that we don't enforce it. It also means that Phaser is internally very easy to hack around with.

But I Like TypeScript!

Good—because we have a TypeScript definitions file as well! There is a guide specifically on using Phaser with TypeScript available.

Easy Asset Loading

Phaser has a built-in asset Loader that can handle:

Images
Sprite Sheets (fixed sized frames)
Texture Atlases (including Texture Packer, JSON Hash, JSON Array, Flash CS6/CC, and Starling XML formats)
Audio files
Data Files (XML, JSON, plain text)
JavaScript files (so you can part-load your games or JS based resources)
Tilemaps (CSV and Tiled map formats)
Bitmap Fonts

We routinely export texture atlas files from Flash directly to our Phaser games, and it supports fully trimmed atlases. Assets can be part-loaded, cached, and pulled from different URLs (for CDN support), and you can even turn any sprite into a progress bar with a single line of code. Rendering: WebGL and Canvas

Internally, Phaser uses Pixi.js for rendering. Pixi is a great, fast rendering library that focuses on Canvas and WebGL. It's a library we continue to contribute towards to help push things forward.

For your games, this means that if the browser supports WebGL then the player will more often than not get a smoother experience. WebGL is common on desktop but still emerging on mobile; even so, it is where HTML5 games are headed in the future, so supporting it is vital. The latest release of Phaser introduced WebGL shader and filter support, and forthcoming ones will implement normal maps so you can benefit from new tools like Sprite Lamp.

Audio: Web Audio and Legacy Audio

Audio has been one of the weakest points of HTML games for a long time. Only a couple of years ago, we were faced with the choice of having one single channel of high-latency audio that would cripple the device during playback or having no audio at all. But times have changed, and the Web Audio API came to our salvation. It allows for proper node-based audio support, with multiple channels, node routing and all kinds of effects. So Phaser fully supports Web Audio.

However, on Android especially, lots of devices still don't support thi—so we also support legacy Audio and the use of Audio Sprites: the method of packing a bunch of samples together into a single file and using playback markers to jump to different effects. Phaser will swap between the two depending on the device capabilities and also includes unlocking the audio sub-system automatically for you, something that catches out a lot of first-time mobile devs!

Input: Multi-Touch, Keyboard, Pointer, Mouse

When supporting desktop and mobile there are an increasingly varied number of potential input options. Phaser supports keyboard, mouse, touch, MSPointer (now Pointer under IE11), and combinations thereof. For example, on Windows Surface devices you could swap between using a mouse and touch, or indeed use both together.

For touch input, Phaser copes with both single-touch and multi-touch environments. You can define up to 10 touch points and track them all independently, using their events to handle Sprite interactions such as dragging, tapping, and collision.

Physics, Tweens, and Particles

Bundled into the core library are the ArcadePhysics and ArcadeParticles systems. These are simple AABB light-weight libraries that allow you to apply gravity and motion to any Sprite, then test for collision and separation. Using a world-based quadtree to help minimise collision tests, you can achieve decent results quickly with little processing time required.

However, we understand that this won't suit all types of games, so the physics system is easy to replace and none of the physics properties are bound to actual sprites (but rather to a body component), so can be swapped out for the likes of Box2D or p2.js. A tweening system is also built-in, allowing you to tween objects or properties easily. And should the game pause, then all of your tweens will pause automatically and resume as needed.

Plugins

It is our aim that the core of Phaser will eventually settle down and hit a nice stable equilibrium, where we're not likely to touch it much beyond fixes and browser updates. However, we want Phaser to carry on growing, providing features for all kinds of games but without causing the core library to explode in growth. To this end, we've built in a plugin system.

Phaser plugins can register themselves with the core framework, be updated in line with the core game loop, and perform all kinds of useful additional tasks. A great example of this is the recently released EasyStar path finding plugin. We will be releasing plugins ourselves and expect to see more coming from the community in the future.