Created
April 9, 2012 19:50
-
-
Save zachwlewis/2346120 to your computer and use it in GitHub Desktop.
Sprite Switching Example
This file contains 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
package | |
{ | |
import flash.display.*; | |
import net.flashpunk.*; | |
import net.flashpunk.graphics.*; | |
import net.flashpunk.utils.*; | |
/** | |
* A sample world to demonstrate swapping a sprite from the World on player input. | |
* @author Zachary Weston Lewis (http://zacharylew.is) | |
*/ | |
public class SpriteFlipWorld extends World | |
{ | |
/** The default sprite. */ | |
protected var _spriteA:Image; | |
/** The sprite to show when {@code Key.SPACE} is held down. */ | |
protected var _spriteB:Image; | |
/** The Entity to switch sprites. */ | |
protected var _switchMan:Entity; | |
public function SpriteFlipWorld() | |
{ | |
// Initialize our sprites. | |
// _spriteA will be a 32x32 red square. | |
_spriteA = new Image(new BitmapData(32, 32, true, 0xffff0000)); | |
// _spriteB will be a 64x16 blue rectangle. | |
_spriteB = new Image(new BitmapData(64, 16, true, 0xff0000ff)); | |
// Construct _switchMan. | |
// He'll start at (100, 75) with _spriteA as his graphic and no mask. | |
_switchMan = new Entity(100, 75, _spriteA, null); | |
} | |
override public function begin():void | |
{ | |
// Add the Entity to our World. | |
// This will cause him to be rendered. | |
add(_switchMan); | |
super.begin(); | |
} | |
override public function update():void | |
{ | |
// Handle input checking each update. | |
if (Input.pressed(Key.SPACE)) | |
{ | |
// If the user pressed SPACE, change _switchMan's graphic to _spriteB. | |
_switchMan.graphic = _spriteB; | |
} | |
else if (Input.released(Key.SPACE)) | |
{ | |
// If the user released SPACE, change _switchMan's graphic back to _spriteA. | |
_switchMan.graphic = _spriteA; | |
} | |
super.update(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment