zoukankan      html  css  js  c++  java
  • Starling案例分析——<Hungry Hero>资源管理

    就如同大部分游戏一样,我们在做游戏的时候,少不了需要Image、MovieClipe、Sound、Font这些资源的引入和使用,Starling因为是基于Stage3D的,所以他对于资源的实现上有一些不同,用到了很多的3D方面的知识才好理解一些。其实Starling只是通过3D的纹理绘制,来实现图片加载的,所以纹理Texture成为了必须要记住的东西。

    通过上一次的了解,我们知道了默认包中有很多资源管理,再次就以Assets.as为例

    这个类里面主要的资源就是welcome界面的背景,游戏中背景,以及各个元素的Spritesheet.png以及他的配置文件。在看游戏的代码之前,先总结一下Image、MovieClipe等这些资源是如何使用的。

    1、Image的使用

    var image:Image = new Image(Texture.fromBitmap(new Pic()));
    this.addChild(image);

    我们只需要通过[Embed(source=“res/*.png”)] var Pic:Class就可以了

    2、MovieClipe的使用

    MovieClipe的资源总共有两个一个是.png文件,还有一个是xml文件。

    这些文件的制作时通过TexturePackerGUI这个工具来制作,Starling官网上可以找到工具的下载,虽然和Image有些类似,但是xml的引入还是有一些不同。

    [Embed(source="../media/graphics/mySpritesheet.png")]
            public static const AtlasTextureGame:Class;
            
    [Embed(source="../media/graphics/mySpritesheet.xml", mimeType="application/octet-stream")]
            public static const AtlasXmlGame:Class;

    我们在使用的时候在创建MovieClipe的方法如下:

    var texture:Texture = Texture.fromBitmap(new AtlasTextureGame());
    var xml:XML = XML(new AtlasXmlGame());
    TextureAtlas ta = new TextureAtlas(texture,xml);
    MovieClipe mc = new MovieClipe(ta.getTextures("fly_"));

    这里ta.getTextures(“fly_”)中传入的字符串其实是得和xml当中相匹配,并且建议将一个MovieClipe的每张素材用一个数组命名的规则。

    最后我们再来看《Hungry Hero》的资源管理代码的总结,其实是定义了两个静态方法,分别获取TextureAtlas或者Texture,用于我们以后的操作。并且使用了Dictionary这个类来做索引匹配,类似于Java中的HashMap,但是原理并不同。

    代码如下:

    public static function getAtlas():TextureAtlas
            {
                if (gameTextureAtlas == null)
                {
                    var texture:Texture = getTexture("AtlasTextureGame");
                    var xml:XML = XML(new AtlasXmlGame());
                    gameTextureAtlas=new TextureAtlas(texture, xml);
                }
    
                return gameTextureAtlas;
            }
            
    
            public static function getTexture(name:String):Texture
            {
                if (gameTextures[name] == undefined)
                {
                    var bitmap:Bitmap = new Assets[name]();
                    gameTextures[name]=Texture.fromBitmap(bitmap);
                }
                
                return gameTextures[name];
            }

    AS可以通过 new Assets[name](),这个是通过name值,确定创建Assets的成员变脸定义的类的对象。

    还是很好理解的

  • 相关阅读:
    3. Image Structure and Generation
    STM32F103
    10.2 External interrupt/event controller (EXTI)
    10.1 Nested vectored interrupt controller (NVIC) 嵌套矢量中断控制器
    ibatis 使用 in 查询的几种XML写法
    文字纵向打印
    oracle每天清理归档日志
    使用语句查询mssql死锁
    Xml序列化UTF-8格式错误
    Nginx的优点
  • 原文地址:https://www.cnblogs.com/flashbird/p/3311900.html
Copyright © 2011-2022 走看看