Permalink
Browse files

- FIXED: GUI layer rendering

- ADDED: animation end event
  • Loading branch information...
Lars Gerckens
Lars Gerckens committed Jan 13, 2012
1 parent 72dd13c commit 5547dacafa226c0f47d783d2316986c67a4b4ce2
View
@@ -1,4 +1,3 @@
-- blurmaterial
- Zoë: A SWF animation exporter for the EaselJS library. ? more TextureAtlas tools?
- multiple worlds test
- camera zoom example for independent resolutions
@@ -14,4 +13,6 @@ What I don't like about the spritesheets is:
About Sprite2DBatch: only addChildAt/removeChildAt are implemented and I'm not sure why addChild/removeChild aren't - maybe there's a hidden reason? Also invalidation in the *At methods seem a bit aggressive.
getChildByTag()
+- events for end / start of animations?
- camera pivot? rotation point?
+- TextureAtlas clone?? for batches, better solution
@@ -33,11 +33,13 @@ package tests {
import de.nulldesign.nd2d.display.Node2D;
import de.nulldesign.nd2d.display.Scene2D;
import de.nulldesign.nd2d.display.Sprite2D;
+ import de.nulldesign.nd2d.display.TextField2D;
import de.nulldesign.nd2d.materials.texture.Texture2D;
import de.nulldesign.nd2d.utils.NumberUtil;
import flash.events.Event;
import flash.events.MouseEvent;
+ import flash.text.TextFormatAlign;
public class CameraTest extends Scene2D {
@@ -83,6 +85,16 @@ package tests {
s.mouseEnabled = true;
s.addEventListener(MouseEvent.CLICK, guiLayerItemClick);
sceneGUILayer.addChild(s);
+
+ var txt:TextField2D = new TextField2D();
+ txt.font = "Helvetica";
+ txt.textColor = 0xFF9900;
+ txt.size = 30.0;
+ txt.align = TextFormatAlign.LEFT;
+ txt.text = "GUI Layer";
+ txt.x = 120.0;
+ txt.y = stage.stageHeight - s.height * 0.5;
+ sceneGUILayer.addChild(txt);
}
private function guiLayerItemClick(e:MouseEvent):void {
@@ -75,6 +75,11 @@ package de.nulldesign.nd2d.display {
sceneGUILayer.handleDeviceLoss();
}
+ override internal function stepNode(elapsed:Number, timeSinceStartInSeconds:Number):void {
+ super.stepNode(elapsed, timeSinceStartInSeconds);
+ sceneGUILayer.stepNode(elapsed, timeSinceStartInSeconds);
+ }
+
override internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean, statsObject:StatsObject):void {
for each(var child:Node2D in children) {
@@ -0,0 +1,18 @@
+/**
+ * de.nulldesign.nd2d.events
+ * @Author: Lars Gerckens (lars@nulldesign.de)
+ * Date: 10.01.12 23:52
+ */
+package de.nulldesign.nd2d.events {
+
+ import flash.events.Event;
+
+ public class SpriteSheetAnimationEvent extends Event {
+
+ public static const ANIMATION_FINISHED:String = "animationFinished";
+
+ public function SpriteSheetAnimationEvent(type:String) {
+ super(type);
+ }
+ }
+}
@@ -30,144 +30,156 @@
package de.nulldesign.nd2d.materials.texture {
+ import de.nulldesign.nd2d.events.SpriteSheetAnimationEvent;
import de.nulldesign.nd2d.materials.texture.SpriteSheetAnimation;
+ import flash.events.EventDispatcher;
+
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
- public class ASpriteSheetBase {
+ public class ASpriteSheetBase extends EventDispatcher {
- protected var frames:Vector.<Rectangle> = new Vector.<Rectangle>();
- protected var offsets:Vector.<Point> = new Vector.<Point>();
- protected var sourceSizes:Vector.<Point> = new Vector.<Point>();
+ protected var frames:Vector.<Rectangle> = new Vector.<Rectangle>();
+ protected var offsets:Vector.<Point> = new Vector.<Point>();
+ protected var sourceSizes:Vector.<Point> = new Vector.<Point>();
protected var sourceColorRects:Vector.<Rectangle> = new Vector.<Rectangle>();
- protected var frameNameToIndex:Dictionary = new Dictionary();
- protected var uvRects:Vector.<Rectangle>;
- protected var spritesPackedWithoutSpace:Boolean;
+ protected var frameNameToIndex:Dictionary = new Dictionary();
+ protected var uvRects:Vector.<Rectangle>;
+ protected var spritesPackedWithoutSpace:Boolean;
+
+ protected var ctime:Number = 0.0;
+ protected var otime:Number = 0.0;
+ protected var interp:Number = 0.0;
- protected var ctime:Number = 0.0;
- protected var otime:Number = 0.0;
- protected var interp:Number = 0.0;
+ protected var triggerEventOnLastFrame:Boolean = false;
- protected var frameIdx:uint = 0;
+ protected var frameIdx:uint = 0;
- protected var activeAnimation:SpriteSheetAnimation;
- protected var animationMap:Dictionary = new Dictionary();
+ protected var activeAnimation:SpriteSheetAnimation;
+ protected var animationMap:Dictionary = new Dictionary();
- public var frameUpdated:Boolean = true;
+ public var frameUpdated:Boolean = true;
- protected var fps:uint;
+ protected var fps:uint;
- protected var _spriteWidth:Number;
- protected var _spriteHeight:Number;
- protected var _sheetWidth:Number;
- protected var _sheetHeight:Number;
+ protected var _spriteWidth:Number;
+ protected var _spriteHeight:Number;
+ protected var _sheetWidth:Number;
+ protected var _sheetHeight:Number;
- public function get spriteWidth():Number {
- return _spriteWidth;
- }
+ public function get spriteWidth():Number {
+ return _spriteWidth;
+ }
- public function get spriteHeight():Number {
- return _spriteHeight;
- }
+ public function get spriteHeight():Number {
+ return _spriteHeight;
+ }
- protected var _frame:uint = int.MAX_VALUE;
+ protected var _frame:uint = int.MAX_VALUE;
- public function get frame():uint {
- return _frame;
- }
+ public function get frame():uint {
+ return _frame;
+ }
- public function set frame(value:uint):void {
- if(frame != value) {
- _frame = value;
- frameUpdated = true;
+ public function set frame(value:uint):void {
+ if(frame != value) {
+ _frame = value;
+ frameUpdated = true;
- if(frames.length - 1 >= _frame) {
- _spriteWidth = frames[_frame].width;
- _spriteHeight = frames[_frame].height;
- }
- }
- }
+ if(frames.length - 1 >= _frame) {
+ _spriteWidth = frames[_frame].width;
+ _spriteHeight = frames[_frame].height;
+ }
+ }
+ }
- public function ASpriteSheetBase() {
- }
+ public function ASpriteSheetBase() {
+ }
- public function update(t:Number):void {
+ public function update(t:Number):void {
- if(!activeAnimation) return;
+ if(!activeAnimation) return;
- ctime = t;
+ ctime = t;
- // Update the timer part, to get time based animation
- interp += fps * (ctime - otime);
- if(interp >= 1.0) {
- frameIdx++;
- interp = 0;
- }
+ // Update the timer part, to get time based animation
+ interp += fps * (ctime - otime);
+ if(interp >= 1.0) {
+ frameIdx++;
+ interp = 0;
+ }
- if(activeAnimation.loop) {
- frameIdx = frameIdx % activeAnimation.numFrames;
- } else {
- frameIdx = Math.min(frameIdx, activeAnimation.numFrames - 1);
- }
+ if(activeAnimation.loop) {
+ frameIdx = frameIdx % activeAnimation.numFrames;
+ } else {
+ frameIdx = Math.min(frameIdx, activeAnimation.numFrames - 1);
+ }
- frame = activeAnimation.frames[frameIdx];
+ frame = activeAnimation.frames[frameIdx];
- otime = ctime;
- }
+ otime = ctime;
- public function playAnimation(name:String, startIdx:uint = 0, restart:Boolean = false):void {
- if(restart || activeAnimation != animationMap[name]) {
- frameIdx = startIdx;
- activeAnimation = animationMap[name];
- }
- }
+ if(triggerEventOnLastFrame && frameIdx == activeAnimation.numFrames - 1) {
+ dispatchEvent(new SpriteSheetAnimationEvent(SpriteSheetAnimationEvent.ANIMATION_FINISHED));
+ }
+ }
+
+ public function playAnimation(name:String, startIdx:uint = 0, restart:Boolean = false, triggerEventOnLastFrame:Boolean = false):void {
+
+ this.triggerEventOnLastFrame = triggerEventOnLastFrame;
+
+ if(restart || activeAnimation != animationMap[name]) {
+ frameIdx = startIdx;
+ activeAnimation = animationMap[name];
+ }
+ }
public function addAnimation(name:String, keyFrames:Array, loop:Boolean):void {
}
- public function clone():ASpriteSheetBase {
- return null;
- }
+ public function clone():ASpriteSheetBase {
+ return null;
+ }
- public function getOffsetForFrame():Point {
- return offsets[frame];
- }
+ public function getOffsetForFrame():Point {
+ return offsets[frame];
+ }
public function getDimensionForFrame():Rectangle {
return frames[frame];
}
- public function getUVRectForFrame(textureWidth:Number, textureHeight:Number):Rectangle {
+ public function getUVRectForFrame(textureWidth:Number, textureHeight:Number):Rectangle {
- if(uvRects[frame]) {
- return uvRects[frame];
- }
+ if(uvRects[frame]) {
+ return uvRects[frame];
+ }
- var rect:Rectangle = frames[frame].clone();
- var texturePixelOffset:Point = new Point((textureWidth - _sheetWidth) / 2.0, (textureHeight - _sheetHeight) / 2.0);
+ var rect:Rectangle = frames[frame].clone();
+ var texturePixelOffset:Point = new Point((textureWidth - _sheetWidth) / 2.0, (textureHeight - _sheetHeight) / 2.0);
- rect.x += texturePixelOffset.x;
- rect.y += texturePixelOffset.y;
+ rect.x += texturePixelOffset.x;
+ rect.y += texturePixelOffset.y;
- if(spritesPackedWithoutSpace) {
- rect.x += 0.5;
- rect.y += 0.5;
+ if(spritesPackedWithoutSpace) {
+ rect.x += 0.5;
+ rect.y += 0.5;
- rect.width -= 1.0;
- rect.height -= 1.0;
- }
+ rect.width -= 1.0;
+ rect.height -= 1.0;
+ }
- rect.x /= textureWidth;
- rect.y /= textureHeight;
- rect.width /= textureWidth;
- rect.height /= textureHeight;
+ rect.x /= textureWidth;
+ rect.y /= textureHeight;
+ rect.width /= textureWidth;
+ rect.height /= textureHeight;
- uvRects[frame] = rect;
+ uvRects[frame] = rect;
- return rect;
- }
- }
+ return rect;
+ }
+ }
}
@@ -30,8 +30,6 @@
package de.nulldesign.nd2d.materials.texture {
- import de.nulldesign.nd2d.materials.*;
-
import flash.geom.Point;
import flash.geom.Rectangle;
@@ -30,8 +30,6 @@
package de.nulldesign.nd2d.materials.texture {
- import de.nulldesign.nd2d.materials.*;
-
import flash.geom.Point;
import flash.geom.Rectangle;

0 comments on commit 5547dac

Please sign in to comment.