当前位置: 首页 > 工具软件 > Citrus Engine > 使用案例 >

Citrus Engine 学习笔记 第一弹 入口类和引擎demo

冀冯浩
2023-12-01

官网教程页面http://citrusengine.com/tutorials/上的入门视频教程讲得很详细,但是那是老版本的了,新版本比如我用的CEV3.1.9,那样写会报错,试了很多方法,最后在这个页面http://wiki.starling-framework.org/citrus/multi_resolution 找到解决,大概就是用一个入口类的模式来写。写法如下:

主类

package 
{
	import citrus.core.starling.StarlingCitrusEngine;
	
	/**
	 * ...
	 * @author Roshan
	 */
	
	[SWF(backgroundColor="#FFFFFF", frameRate="60", width="800", height="600")]
	public class Main extends StarlingCitrusEngine
	{
		
		public function Main()
		{
			
		}
		
		override public function initialize():void
		{
			setUpStarling(true);
			
		}
		
		override public function handleStarlingReady():void {
			state = new GameState();
		}
		
	}
	
}

其中千万不能漏掉

override public function handleStarlingReady():void {
			state = new GameState();
		}
这段,重载的这函数是当引擎初始化后就绪后所做的操作,所以把新建GameState放在这里,而不是原来那种直接写在
initialize()

函数里面,这个貌似是机制问题,不这样写会Flash Player会报错,说主类没有继承StarlingCitrusEngine,或者没调用setUpStarling()方法。



然后,新建一个GameState类,这个类的写法可以照搬官网demo的例子,自己参考api http://citrusengine.com/documentation/ 调用下各个组件试一试,比如:

package  
{
	import Box2D.Collision.b2AABB;
	import Box2D.Collision.b2ContactID;
	import Box2D.Dynamics.Contacts.b2Contact;
	import citrus.core.starling.StarlingState;
	import citrus.objects.platformer.box2d.Coin;
	import citrus.objects.platformer.box2d.Enemy;
	import citrus.objects.platformer.box2d.Hero;
	import citrus.objects.platformer.box2d.MovingPlatform;
	import citrus.objects.platformer.box2d.Platform;
	import citrus.physics.box2d.Box2D;
	
	/**
	 * ...
	 * @author Roshan
	 */
	public class GameState extends StarlingState 
	{
		
		public function GameState() 
		{
			super();
			
		}
		
		override public function initialize():void {
			super.initialize();
			
			var physics:Box2D = new Box2D("box2D");
			physics.visible = true;
			add(physics);
			
			add(new Platform("p1", {x:700, y:stage.stageHeight/4, width:stage.stageWidth/5,height:40}));
			
			var floor:Platform = new Platform("floor",{x:512,y:550,width:1024,height:40});
			add(floor);
			
			var mp:MovingPlatform = new MovingPlatform("Moving" ,
				{x:200,y:450,width:100, height:35, startX:200, startY:450, endX:500, endY:200,speed:3} );
			add(mp);
			
			var hero:Hero = new Hero("Roshan", { x:50, y:50, width:75, height:100 } );
			add(hero);
			
			var enemy:Enemy = new Enemy("tiger", { x:500, y:500, width:60, height:40,leftBound:100, rightBound:700, speed:2 } );
			add(enemy);
			
			var ring:Coin = new Coin ("ring", { x:750, y:60, width: 50, height:50 } );
			ring.onBeginContact.add(function(c:b2Contact):void {
				trace("Roshan Win!");
				remove(ring);
	
				
			})
			add(ring);
		}
	}

}
然后设定下Main.as为文档类,运行下就能看到效果了。


总结下,其实就是把原来initialize()的内容分离到handleStarlingReady()中处理,也可以加入其他内容,比如官方例子multi_resolution的做法,在这里处理适配不同分辨率的操作:

override public function handleStarlingReady():void
		{
			assetManager = new AssetManager(scaleFactor);
			assetManager.verbose = true;
 
			assetManager.enqueue("assets" + scaleFactor + "x");
 
			assetManager.loadQueue(function(ratio:Number):void
				{
					if (ratio == 1)
						state = new FirstState();
				});
		}
菜鸟自学日记就先记录到这,埋个坑以防自己忘了,呵呵
 

 类似资料: