Code Monkey home page Code Monkey logo

phaser-nineslice's Introduction

Phaser Nineslice

Phaser-Nineslice plugin adds 9-slice scaling support to Phaser v2 and CE. An implementation for v3 is also available thanks to jdotrjs as phaser3-nineslice.

Key features:

  • Blazing fast
  • Low memory usage
  • Easy to use API

Important From here on this library will be published and updated under @azerion/phaser-nineslice at NPM, the old phaser-nineslice will no longer be maintained.

If you are coming from v1 you can read the migration guide at the bottom

Getting Started

First you want to get a fresh copy of the plugin. You can get it from this repo or from npm, ain't that handy.

npm install @azerion/phaser-nineslice --save-dev

Next up you'd want to add it to your list of js sources you load into your game

<script src="node_modules/@azerion/phaser-nineslice/build/phaser-nineslice.js"></script>

Usage

Load the plugin

You still need to load the plugin in your game. This is done just like any other plugin in Phaser. So, to load the plugin, include it in one of the Phaser States.

function preload(){
game.plugins.add(PhaserNineSlice.Plugin);
}

The plugin will patch your Phaser game with additional load/add/make methods so the nineslice container fits up in Phaser like any normal object!

Preloading

Like any other asset in Phaser, you still need to preload the image. It's a bit different then a regular image, in the sense that it requires some extra data.

Apart from the key and the url, you can also specify the sizes of top, left, right and bottom sides.

If you don't specify a bottom size, it will use the value for the top. Same goes for right and left.

It's also possible to only give one size. If only one value is given, this will use that for the edges.

//This will load a 9-slice texture where all sides are 25px
game.load.nineSlice('my-image', '/images/my-image.jpg', 25);

//This will load a 9-slice texture where the
// top is 10, left is 15, right is 20 and bottom is 30 pixels in size
game.load.nineSlice('my-image', '/images/my-image.jpg', 10, 15, 20, 30);

Adding a container

Also adding and creating of 9-slice containers is exposed through Phaser's GameObjectCreator and GameObjectFactory. So you can add a 9-slice container to your game just like any other image once it's preloaded!

//We specify the x and y position. the key for the image and the width and height of the container. It will be automaticly scaled!
game.add.nineSlice(100, 100, 'my-image', 200, 50);

Or if you'd want to do something with it first:

//Two options here, first we use Phaser
var container = game.create.nineSlice(0, 0, 'my-image', 200, 50);
container.x = 20;
container.y = 20;
game.add.existing(container);

//Or we use the Constructor
var nineSlice = new PhaserNineSlice.NineSlice(game, 0, 0, 'my-image', null, 200, 50);
nineSlice.x = 50;
nineSlice.y = 50;
game.add.existing(nineSlice);

Using a Texture Atlas

It's also possible to use an image that's located inside a Texture atlas. The only difference then is that you don't have to preload the image, instead you use the object's constructor and pass the framedata directly on creation:

//Add an nineslice image with an texture atlas
var sliceButton = new PhaserNineSlice.NineSlice(
        game,           // Phaser.Game
        150,            // x position
        100,            // y position
        'buttons',      // atlas key
        'btn_clean.png',// Image frame
        200,            // expected width
        100,            // expected height
        { //And this is the framedata, normally this is passed when preloading. Check README for details
            top: 20,    // Amount of pixels for top
            bottom: 23, // Amount of pixels for bottom
            left: 27,   // Amount of pixels for left
            right: 28   // Amount of pixels for right
        }
);
this.game.add.existing(sliceButton);

Resize method

9-slice container has a resize method which lets you expand/shorten the width or length of the image. When using resize method, make sure values are not lower than the width of the image corners

var sliceContainer = game.add.nineSlice(5, 5, 'image', null, 48, 48);
sliceContainer.resize(100, 200);

Migrating from v1

The API of the objects is the same as before but the namespace changed. We decided to remove the Fabrique namespace, and house the plugin on it's own (PhaserNineSlice). so: Fabrique.Plugins.NineSlice becomes: PhaserNineSlice.Plugin

All other references of Fabrique.Plugins can be replaced with PhaserNineSlice. If you are still unsure how or what, both the example and this readme have been adjusted to work with the new namespace.

Disclaimer

We at Azerion just love playing and creating awesome games. We aren't affiliated with Phaser.io. We just needed some awesome nine slice containers in our awesome HTML5 games. Feel free to use it for enhancing your own awesome games!

Phaser NineSlice is distributed under the MIT license. All 3rd party libraries and components are distributed under their respective license terms.

phaser-nineslice's People

Contributors

16patsle avatar alebles avatar cxong avatar florisdh avatar jdotrjs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phaser-nineslice's Issues

Cannot load the plugin

Hi,
I'm trying to use the plugin in my TypeScript code.
I've installed the plugin via npm.
I've added the typescript definition file "phaser-nineslice.d.ts" to my tsconfig.json

In my preloader state I've added the plugin using

this.game.plugins.add(PhaserNineSlice.Plugin);

Directly after this line I've added

this.game.load.nineSlice('my-image', '/images/my-image.jpg', 25);

I get the error: "property nineSlice does not exist on type Loader"

I've spent a couple of hours to find a solutiion without any success.

I also tried to use directly the typescript code from your github project but I get also a bunch of error messages.

Any help really appreciated

Thanks

Tint property causes nineslice to bug out

Hi,

When I try using tint, it works along with the resize function, but the nineslice graphic gets messed up. The edges (in my case, the right and the bottom) disappears. I'm on Phaser 2.8.1, canvas mode btw.

Update frame

Is there a way to update the frame used for the niceSlice within the spritesheet?

With a sprite I can do a simple sprite.framename = "anotherFrame" to change the frame used by the sprite.
Is this possible with niceSlice?

Atlases support

I can see that atlas support was added in the latest commit but I can't get it working. Is there any sample how to use it?

Button with Over & Pressed states

Consider the following spriteseet:

image

Is it possible to create a 9sliced button with over and pressed states from this kind of asset?

Error in README 'Adding a container'

This Issue is about an error in the example.
game.add.nineSlice(100, 100, 'my-image', 200, 50);

This call requires an additional argument for the frame of the image with the given key.

The correct way of creating a nineslice image can be seen in the example:
game.add.nineSlice(5, 5, 'image', null, 48, 48);

Trying to add two instances of PhaserNineSlice.NineSlice causes a webGL error and doesn't show the second instance.

  • A bug in the API (always say which version you're using!)
    Version: "@orange-games/phaser-nineslice": "^2.0.0",

Trying to add two instances with a texture from the same atlas causes a WebGL error:
[.Offscreen-For-WebGL-07472F00]GL ERROR :GL_INVALID_OPERATION : glDrawElements: Source and destination textures of the draw are the same.
The result is that the second instance doesn't show up.

The code that causes the error:

	this.nineSlice = new PhaserNineSlice.NineSlice(
		this.game as PhaserNineSlice.NineSliceGame,
		400,
		100,
		AssetsKeys.ATLAS_GAME_VIEW,
		AssetsKeys.ATLAS_GAME_VIEW_KEYS.MY_CARDS_VIEW.BOOKS_REVIEWER_BACK_BTN_UP,
		500,
		200,
		{
			top: 20,
			bottom: 20,
			left: 50,
			right: 50
		}
	);
	this.game.add.existing(this.nineSlice);

	this.nineSlice2 = new PhaserNineSlice.NineSlice(
		this.game as PhaserNineSlice.NineSliceGame,
		0,
		0,
		AssetsKeys.ATLAS_GAME_VIEW,
		AssetsKeys.ATLAS_GAME_VIEW_KEYS.MY_CARDS_VIEW.BOOKS_REVIEWER_BACK_BTN_UP,
		200,
		500,
		{
			top: 20,
			bottom: 20,
			left: 50,
			right: 50
		}
	);
	this.game.add.existing(this.nineSlice2);

Error in README 'load the plugin'

Current README says:

function preload(){
game.plugins.add(PhaserNineSlice.NineSlice);
}

Which outputs an error to the console:
Uncaught TypeError: game.cache.getNineSlice is not a function

The correct way of adding the plugin can be seen in the example:

game.plugins.add(PhaserNineSlice.Plugin);

Non-transparent texture on Android 5.1.1

The screenshot below was taken on a Galaxy S6 running Android 5.1.1 and Google Chrome 56.0
screenshot_2017-03-28-12-05-27

I managed to fix it, but it is not an elegant fix. The texture needs to be cleared before renderXY() is called. I solved the issue by setting the flag clear to true only in the first iteration of the for loops:

https://github.com/orange-games/phaser-nineslice/blob/master/ts/NineSlice.ts#L91

(<Phaser.RenderTexture>this.texture).renderXY(s, finalXs[xi], finalYs[yi], (!yi && !xi));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.