Browsed by
月份:2012年12月

TexturePacker两三事

TexturePacker两三事

学习Starling,不可避免的涉及到素材格式的问题,虽然Flash CS6也可以导出Sprite sheet 但是,还是没有TexturePacker给力。TexturePacker功能还是蛮全面的,可以导出多种格式,不只限于Starling。

使用完整版的话需要激活码,不过可以免费得到,作者的建议是 就像我这样写个文章介绍下,今天已经收到了做着的激活码,虽然作者也说不是必须的,但是做人要厚道,还是帮作者写下,另外,这个工具也确实挺不错的,有些功能是Flash CS6不具备的,比如多张图片合并等。

TexturePackerICO

相关网址:http://www.codeandweb.com

starling 粒子系统学习(Particle-System)

starling 粒子系统学习(Particle-System)

学习Starling的粒子效果,做了个关于火柴燃烧效果的小应用。

火柴效果

主要代码如下:

[codesyntax lang=”actionscript3″]

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;

	import starling.core.Starling;
	[SWF(width="480", height="800", frameRate="60", backgroundColor="0x333333")]
	public class ParticlesTest extends Sprite
	{
		public function ParticlesTest()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;

			var myStarling:Starling = new Starling(Game,stage);
			myStarling.showStats = true;
			myStarling.start();
		}
	}
}

[/codesyntax]

[codesyntax lang=”actionscript3″]

package
{
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.TouchEvent;
	import starling.extensions.PDParticleSystem;
	import starling.extensions.ParticleSystem;
	import starling.textures.Texture;

	public class Game extends Sprite
	{
		[Embed(source="../assets/match_fire.pex", mimeType="application/octet-stream")]
		private static const MatchFireConfig:Class;

		[Embed(source="../assets/match_fire.png")]
		private static const FirePng:Class;

		[Embed(source="../assets/match_begin.png")]
		private static const MatchBeginPng:Class;
		[Embed(source="../assets/match_ing.png")]
		private static const MatchIngPng:Class;
		[Embed(source="../assets/match_end.png")]
		private static const MatchEndPng:Class;

		private var particleSystem:ParticleSystem;
		private var matchBegin:Image;
		private var matchIng:Image;
		private var matchEnd:Image;
		private var status:String;
		private const STATUS_BEGIN:String = "BEGIN";
		private const STATUS_ING:String = "ING";
		private const STATUS_END:String = "END";

		public function Game()
		{
			addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}

		private function addToStage(e:Event):void
		{
			this.matchBegin = new Image(Texture.fromBitmap(new MatchBeginPng()));
			this.matchIng = new Image(Texture.fromBitmap(new MatchIngPng()));
			this.matchEnd = new Image(Texture.fromBitmap(new MatchEndPng()));
			addChild(matchBegin);
			addChild(matchIng);
			addChild(matchEnd);

			var config:XML = XML(new MatchFireConfig());
			var texture:Texture = Texture.fromBitmap(new FirePng());
			this.particleSystem = new PDParticleSystem(config,texture);
			particleSystem.stop();
			addChild(particleSystem);
			particleSystem.emitterX = 98;
			particleSystem.emitterY = 140;
			particleSystem.alpha = 0.5;
			particleSystem.scaleX = particleSystem.scaleY = 2.5;
			Starling.juggler.add(particleSystem);

			addEventListener(TouchEvent.TOUCH,touchHandler);
			this.status = STATUS_BEGIN;
			updateStatus();
		}

		private function touchHandler(e:TouchEvent):void
		{
			if(status == STATUS_BEGIN){
				status = STATUS_ING;
			}else if(status ==STATUS_ING){
				status = STATUS_END;
			}else if(status ==STATUS_END){
				status = STATUS_BEGIN;
			}
			updateStatus();
		}

		private function updateStatus():void{
			this.matchBegin.visible = status ==STATUS_BEGIN;
			this.matchIng.visible = status ==STATUS_ING;
			this.matchEnd.visible = status ==STATUS_END;

			if(status == STATUS_ING){
				particleSystem.start();
			}else{
				particleSystem.pause();
			}
		}
	}
}

[/codesyntax]

 

发布了一个apk,有Andriod的朋友可以试试,设定的60帧/s,最低会掉到30帧/s,没优化.(手机是htc g12的,480×800 如果手机屏幕不一样的话 可能坐标略有差别)

下载(apk): [download id=”14″ format=”2″]

项目源码下载:[download id=”15″]  (环境: Flash Builder 4.7  两个类库(starling和particle system)没有打包,可以在下面的地址下载下)

相关学习资料:

starling源码:https://github.com/PrimaryFeather/Starling-Framework

粒子系统:https://github.com/PrimaryFeather/Starling-Extension-Particle-System

粒子系统在线编辑器:http://onebyonedesign.com/flash/particleeditor/