Created
October 5, 2013 01:17
-
-
Save arisetyo/6835369 to your computer and use it in GitHub Desktop.
Using spritesheets with pixi.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE HTML> | |
| <html> | |
| <head> | |
| <title>spritesheet</title> | |
| <style type="text/css"> | |
| body { | |
| margin: 0; | |
| padding: 0; | |
| background-color: #000000; | |
| } | |
| </style> | |
| <script src="pixi.js"></script> | |
| </head> | |
| <body onclick="detectClick()"> | |
| <script type="text/javascript"> | |
| //SPRITES ARRAY | |
| var aliens = []; | |
| var alienFrames = ["eggHead.png", "flowerTop.png", "helmlok.png", "skully.png"]; | |
| var isAliensLoaded = false; | |
| //LOAD SPRITESHEET | |
| var assetsToLoader = ["SpriteSheet.json"]; | |
| loader = new PIXI.AssetLoader(assetsToLoader); | |
| loader.onComplete = onAssetsLoaded; | |
| loader.load(); | |
| //CREATE STAGE | |
| var stage = new PIXI.Stage(0xFFFFFF); | |
| renderer = PIXI.autoDetectRenderer(800, 600); | |
| document.body.appendChild(renderer.view); | |
| function onAssetsLoaded() { | |
| // start animating | |
| isAliensLoaded = true; | |
| requestAnimFrame( animate ); | |
| } | |
| function animate() { | |
| requestAnimFrame( animate ); | |
| /* | |
| //REMOVE COMMENT TO ROTATE SPRITES ON STAGE | |
| for(var i=0; i<aliens.length; i++) { | |
| var sprite = aliens[i]; | |
| sprite.rotation += 0.025; | |
| } | |
| */ | |
| renderer.render(stage); | |
| } | |
| function detectClick() { | |
| if(isAliensLoaded) { | |
| var e = window.event; | |
| var posX = e.clientX; | |
| var posY = e.clientY; | |
| var alien = PIXI.Sprite.fromFrame( alienFrames[ Math.floor(Math.random()*4) ] ); | |
| alien.anchor.x = 0.5; | |
| alien.anchor.y = 0.5; | |
| alien.position.x = posX; | |
| alien.position.y = posY; | |
| aliens.push(alien); | |
| stage.addChild(alien); | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment