2010-09-15 62 views
3

我试图创建一个库存系统写在行动脚本2(Flash as2) 我试图创建一个库存点动态创建和一个影片剪辑远程加载到库存中的mc点。AS2库存系统

我无法找到如何做到这一点(你必须创建库存点自己只能手动的)任何在线指南

会有人能够指出我在正确的方向来做到这一点?

任何帮助,非常感谢!

谢谢 Daniel。

+0

请尽可能帮忙 – Daniel 2010-09-16 12:02:12

回答

3

我不明白你如何想象这个库存系统:它看起来如何?它是如何工作的 ?

从简化的角度来看,我知道你有一个将从外部源(使用LoadVars,xml文件等解析的csv文件)加载的项目列表,并且你想显示它们。 我将它拆分成这样:

  • 数据源
  • 数据提供者 - 最终你将会对invetory项目,每个 项目将有属性((TXT,XML等)。标签,图像,价格等)
  • 平局/渲染方法(在水平/垂直方向的列表项,平铺列表,数据网格等)

对于dinamically显示物品, 我推荐看看MovieClipLoader课程。您可以使用MovieClip的loadMovie()方法,但MovieClipLoader更加灵活(您可以了解该项是否加载(%),获取尺寸等),而不是loadMovie()方法。

不管你的数据源,你将最终库存对象的数组,将有我推测性质。

下面是一个基本的方法来显示库存物品:

,这里是一个预览:

basic inventory

请注意,这不是写它的快速/有效的方式,但是一个简单的例子,更容易说明这个想法。

一种不同的方法:

作为另一种选择,如果文件大小不是问题,你可以使用的组件(你可以得到有关BLOAD的70KB只是从他们那里)。一个好处是,已经有方法可以处理数据而不会触及太多的代码,并且可以使用hacky方式来显示项目,或者创建自定义单元格渲染器。

下面是一个使用List和一个XMLConnector的一个基本方法:

  • 添加一个List组件阶段, 命名为LS

  • 添加XMLConnector的(从数据组件)并将其命名为XML

  • 创建与您选择的结构的XML文件。

我用这样的结构:

<data> 
    <item label="item 1" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 2" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 3" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 4" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 5" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 6" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 7" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 8" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 9" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 10" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 11" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 12" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 13" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 14" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 15" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 16" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 17" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 18" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 19" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
    <item label="item 20" source="http://stackexchange.com/images/icon/stackoverflow.com"/> 
</data> 

参数,您需要将URL设置为你的XML, 组件检查的标签和方向接收。

接下来的事情就是导入XML模式到XML连接器,因此它可以“理解”你的XML文件的结构。您可以通过按组件检查器的“架构”选项卡中的导入架构按钮来完成此操作。它是选项卡右上角的小按钮,蓝色箭头指向下方。此外,确保您的结果被选中

import schema

你会看到结果的更新和阵列会从你的XML创建,你也可以看到每个对象的属性。

既然已经导入了xml schema/structure,我们可以创建一个绑定。我们通过按+图标组件检查的绑定选项卡:

create binding

的添加将出现绑定的对话框,这是你选择的数组元素,然后按OK。

add binding

对我来说,这就是所谓的项目,它可以被称为别的东西,这取决于XML,但这个想法是你选择含有重复([N])项的元素。

下一步是将传入数据绑定到某些东西,在这种情况下,我们的列表组件。设置方向出来,然后双击势必值:

bound to setup

势必将弹出对话框。这是我们选择列表的地方,它是数据提供者作为接收者。

bound to complete

这是所有你需要做的组件检查。现在你已经加载并处理了你的xml,并在数据准备就绪后将数据发送到列表中。

现在,我们只需要创建一个空的影片剪辑,命名为图标,我们绘制一个50x50的边界,因为这将持有的计算器图标(48×48)里面,我们为ActionScript导出(连锁)

这些最后的步骤涉及7行动作。 在我们触发XML连接器的主时间轴上,我们设置的DefaultIcon样式属性,设置行的高度,所以库存图像/标志将适合,并禁用翻转,只是为了避免重绘该事件:

xml.trigger();//trigger the xml loading 
ls.setStyle('defaultIcon','Icon');//use your library clip as an icon 
ls.setStyle('useRollOver',false);//stop redrawing list item on rollOver 
ls.rowHeight = 48;//you should know the size of the inventory item 

现在最后的3行代码是相当黑客的,因为我们正在做的是使用列表的图标(通常是重复相同的库项目)从图标剪辑本身找到该项目的索引,所以访问列表,数据提供者以及每个图标剪辑的数据。

我们将此代码放在图标的动画片段:

var index:Number = parseInt(_parent._name.substr(7))-10;//hack #1 use the rendered icon clip's name property to get it's index 
var image:MovieClip = createEmptyMovieClip('image',1); 
image.loadMovie(_parent._parent._parent.dataProvider.getItemAt(index).source);//hack #2 'clip' up from the icon clip to the list to find the list and source property for current item 

就是这样,你会得到一个显示LABES和图像从一个XML文件的列表: preview

这是一个选项,如果你愿意使用组件,并且只需要一些代码少的方法。如果您仍想使用这些组件,但是要以正确的方式进行操作,则需要创建一个自定义的Cell Renderer,它将显示每个库存项目的数据。有一个very good tutorial on the flash-db网站。

HTH

UPDATE:

我刚读了评论的其余部分。 @gmale是正确的,如果你打算使用库中的物品,attachMovie就是你需要的东西。 你使用这样的:

theContainerForYourItem.attachMovie('itemLinkageName','someOptionalName',depth);//note depth is a number 

这里有一个简单的测试,你必须改变itemSize,要么联动字符串(无论是你在用代码中使用的那些相同名称的库剪辑,或您更新代码名称):

var selectedItem:MovieClip;//this will keep track of the selected item 
var itemSize:Number = 67;//set this to the size of a inventory item 
var inventoryIds:Array = ["New","Folder","Disk","Mail","Graph"];//item linkage ids, make sure you've got some movie clips in library with these ids, or update the array 
var inventory:MovieClip = this.createEmptyMovieClip("inventory",0);//container 
var level:MovieClip = this.createEmptyMovieClip("level",1); 
//create items 
for(var i:Number = 0 ; i < inventoryIds.length ; i++){ 
    //create container for item 
    var itemContainer:MovieClip = inventory.createEmptyMovieClip('item'+i,i); 
    //draw border, invisible bg 
    itemContainer.lineStyle(1);itemContainer.beginFill(0,0),itemContainer.lineTo(itemSize,0);itemContainer.lineTo(itemSize,itemSize);itemContainer.lineTo(0,itemSize);itemContainer.lineTo(0,0);itemContainer.endFill(); 
    //position, add library item 
    itemContainer._x = (itemSize+2) * i;//2 is just spacing 
    itemContainer.attachMovie(inventoryIds[i],'icon',0); 
    itemContainer.onPress = itemSelected;//selec item 
} 
function itemSelected():Void{ 
    //if there was previously an item removed, restore it...depends on your game's logic, u need clicks though 
    if(lastItem != undefined) restoreItemToInventory(); 
    trace('selected item is: ' + inventoryIds[this.getDepth()]);//trace ths selected item 
    this.getInstanceAtDepth(0).removeMovieClip();//and remove the icon from the invotory 
    delete this.onPress;//disable clicks 
    lastItem = this;//update the last Item for restoring 
    //draw an item inside the level 
    var levelItem:MovieClip = level.attachMovie(inventoryIds[this.getDepth()],'icon',0); 
    levelItem._x = Stage.width * .5;level._y = Stage.height * .5;//position item; 
    levelItem.onPress = itemUsed; 
} 
function restoreItemToInventory():Void{ 
    lastItem.attachMovie(inventoryIds[lastItem.getDepth()],'icon',0);//attach the icon again 
    lastItem.onPress = itemSelected;//make restore click 
} 
function itemUsed():Void{ 
    this.removeMovieClip();//remove from stage 
    trace('item is: ' + inventoryIds[lastItem.getDepth()] + ' was used');//do whatever to hero/enemies 
    restoreItemToInventory();//restore to inventory or not 
} 

为每个库项目我用的容器。原因是,当我删除一个项目时,实际上我只是删除了容器所持有的库项目,并且容器仍然存在,并且深思熟虑它的深度,这与数组中项目的索引相同。这稍后用于删除/附加库存物品。一般来说,这是一种不好的做法,因为有紧密耦合的元素,但为了简单的演示,就可以做到。

如果在你的游戏中,将会添加新的物品(玩家不会一次拥有所有可能的物品)并将其移除,使用......也许值得学习如何使用Array类,尤其是splice()方法这将允许您在给定索引处添加,删除数组中的项目。根据您更新的库存数组,您将显示项目和设置交互性。

2

这听起来像是我可以帮助你的东西,但我需要更多关于你想要做的事情的信息。

另外,是否有一个很好的理由,你使用AS2而不是actionscript 3?

从我可以收集的信息来看,这听起来像你有一个动画片段(我们称之为“父容器”),并且想要动态地将另一个片段加载到它中。

如果我是正确的,那么你很幸运,因为这是一种非常常见的使用ActionScript的方式。有无处不在的指南。我现在正在编写一个应用程序,在AS3中做这件事情。给定一个父容器,它会根据用户的导航方式或选择的内容随着时间的推移动态加载不同的影片剪辑。但是,我的项目在AS3中。

在AS2中,使用MovieClip的attachMovie()方法实现了相同的技术。
Here is the old support document for that feature.

有一个伟大的工作示例这里:
ActionScript.org - Using attachMovie


或者,你可能会试图动态数据加载到你的电影。同样,这也是闪存的常见用法,因此您一定能够完成此操作。

下面是一些例子,可以帮助:
Thorough example on Loading vars that came from PHP
Loading variables from text files


如果这些方法都可以帮助,提供一些更多的细节,我们都可以帮你轻松一点。

希望帮助在某种程度上,

-gMale

+0

我必须使用AS2,因为游戏/虚拟世界在as2中。 – Daniel 2010-09-20 10:45:34

+0

@Daniel:好的。如果上述内容不是您要查找的内容,请详细说明您的问题(也可以举个例子),描述您正在尝试做什么。我相信我们可以解决它。 – gMale 2010-09-20 13:43:51

+0

我正在尝试在as2中为我的虚拟世界创建一个库存系统。我需要所谓的库存插槽来显示库存(物品数组)中的每个物品ID以将物品本身加载到插槽中。那么该插槽应该可以点击以穿戴项目 – Daniel 2010-09-21 09:15:51

1

设置它把项目的下一个开放插槽。这并不难。

+0

插槽必须动态创建 – Daniel 2010-09-27 08:39:41