Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Code by rodrigolopezpeker (aka 7interactive™) on 12/26/13 1:25 PM.
- */
- package {
- import com.genome2d.components.GCamera;
- import com.genome2d.components.renderables.GSprite;
- import com.genome2d.context.GContext;
- import com.genome2d.core.GNode;
- import com.genome2d.g2d;
- import com.genome2d.textures.GTexture;
- import flash.geom.Rectangle;
- use namespace g2d;
- /**
- * Usage:
- * use GSpriteTile::scaleX instead of node.transform.scaleX, is a quick workaround to match the transformations.
- *
- * grass = GNodeFactory.createNodeWithComponent(GSpriteTile) as GSpriteTile ;
- * grass.textureId='grass';
- * Genome2D.getInstance().root.addChild(grass.node);
- * grass.scaleX = 2 ;
- * grass.width = 200 ;
- * grass.height = 200 ;
- *
- * You can play with the offsetX/offsetY values to create a scroll effect, or to offset
- * the position of the uv to your needs, it uses PIXELS values, so should be easier to
- * get an accurate offset :)
- *
- */
- public class GSpriteTile extends GSprite {
- private var _tw:int;
- private var _th:int;
- private var _width:Number;
- private var _height:Number;
- private var _invalidateSize:Boolean;
- private var _scaleX:Number=1;
- private var _scaleY:Number=1;
- private var _offsetX:Number = 0 ;
- private var _offsetY:Number = 0 ;
- /**
- * Constructor.
- * @param pNode
- */
- public function GSpriteTile(pNode:GNode) {
- super(pNode);
- }
- override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
- if(_invalidateSize){
- _invalidateSize = false ;
- resize() ;
- }
- super.render(p_context, p_camera, p_maskRect);
- }
- private function resize():void {
- var ratioX:Number = _width/_tw;
- var ratioY:Number = _height/_th ;
- cTexture.nUvScaleX = ratioX ;
- cTexture.nUvScaleY = ratioY ;
- cNode.cTransform.setScale( ratioX*_scaleX, ratioY*_scaleY) ;
- }
- override public function set textureId(p_value:String):void {
- super.textureId = p_value;
- _width = _tw = cTexture.width ;
- _height = _th = cTexture.height ;
- }
- override public function setTexture(p_texture:GTexture):void {
- super.setTexture(p_texture);
- _width = _tw = cTexture.width ;
- _height = _th = cTexture.height ;
- }
- public function get width():Number {
- return _width;
- }
- public function set width(value:Number):void {
- if(value==_width || value < 0) return ;
- _width = value;
- _invalidateSize = true
- }
- public function get height():Number {
- return _height;
- }
- public function set height(value:Number):void {
- if(value==_height || value < 0) return ;
- _height = value;
- _invalidateSize = true
- }
- public function get scaleX():Number {
- return _scaleX;
- }
- public function set scaleX(value:Number):void {
- if(value==_scaleX) return ;
- _scaleX = value;
- _invalidateSize = true
- }
- public function get scaleY():Number {
- return _scaleY;
- }
- public function set scaleY(value:Number):void {
- if(value==_scaleY) return ;
- _scaleY = value;
- _invalidateSize = true
- }
- public function get offsetX():Number {
- return _offsetX;
- }
- public function set offsetX(value:Number):void {
- if(value==_offsetX) return ;
- _offsetX = value;
- cTexture.nUvX = _offsetX/_tw ;
- }
- public function get offsetY():Number {
- return _offsetY;
- }
- public function set offsetY(value:Number):void {
- if(value==_offsetY) return ;
- _offsetY = value;
- cTexture.nUvY = _offsetY/_th ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment