AS2 Inventory System

I am trying to create an inventory system written in action script 2 (flash as2) I am trying to create one that has dynamic creations and the clip is remotely loaded into mc at the location of the inventory.

I can not find a single online guide on how to do this (only manual ones that you must create yourself in the inventory)

Can anyone point me in the right direction?

Any help is much appreciated!

Thanks Daniel.

+3
source share
3 answers

I do not understand how you present this inventory system: what does it look like? how it works?

, , ( csv, LoadVars, xml ..), . :

  • (txt, xml ..)
  • - invetory, (, , ..).
  • draw/render ( / , , ..).

, MovieClipLoader. LoadMovie() MovieClip, MovieClipLoader ( , (%), ..) loadMovie().

, , , .

:

//using dummy data, you would populate the array by parsing loaded vars or xml, whatever works best for you
var dataProvider:Array = [];
var dummyItems:Number = 20;
for(var i:Number = 0 ; i < dummyItems ; i++) dataProvider[i] = {label:'item '+i,source:'http://stackexchange.com/images/icon/stackoverflow.com'};
//make a container
var inventory:MovieClip = this.createEmptyMovieClip('inventory',1);
var inventoryMask:MovieClip = this.createEmptyMovieClip('inventoryMask',2);
inventoryMask.beginFill(0);inventoryMask.lineTo(Stage.width,0);inventoryMask.lineTo(Stage.width,50);inventoryMask.lineTo(0,50);inventoryMask.lineTo(0,0);inventoryMask.endFill();
inventory.setMask(inventoryMask);
//add items to it
for(i = 0 ; i < dummyItems ; i++) makeInventoryRenderItem(dataProvider[i],inventory,i);
//scroll on mouse over
inventory.onRollOver = function():Void{
    inventory.onEnterFrame = function(){
        inventory._x = -_xmouse;//this is just a stub, replace with any navigation method you wish
    }
}
inventory.onRollOut = function():Void{
    delete inventory.onEnterFrame;
}
//make a movie clip with a loader and a label
function makeInventoryRenderItem(data:Object,container:MovieClip,index:Number):Void {
    var renderItem:MovieClip = container.createEmptyMovieClip(data.label,index);
    var loader:MovieClipLoader = new MovieClipLoader();
    loader.loadClip(data.source,renderItem);
    var handler:Object = new Object();
    handler.onLoadInit = function(target:MovieClip):Void{
        target._x = (target._width + 2) * index;
        var label:TextField = renderItem.createTextField(data.label+'Label',renderItem.getNextHighestDepth(),0,0,40,22);
        label.text = data.label;
    }
    loader.addListener(handler);
}

:

basic inventory

, / , , .

:

, , ( 70 bload ). , , , , .

List XMLConnector:

  • , ls

  • XMLConnector ( Data) 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, . , + "" " ":

create binding

" ", "" "".

add binding

item, - , xml, , , ([n]) .

- -, , . , :

bound to setup

Bound To. .

bound to complete

, . -, xml , .

MovieClip , 50x50, stackoverflow (48x48), ActionScript (Linkage)

7 ActionScript. 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 , ( ), , , , .

MovieClip:

var index:Number = parseInt(_parent._name.substr(7))-10;//hack #1 use the rendered icon clip name property to get it 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

, XML : preview

, . , , Cell Renderer, . flash-db .

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 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(), , . .

+3

, , , .

, , AS2 actionscript 3?

, , , ( " " ), .

, , ActionScript. . , AS3. , , . AS3.

AS2 attachMovie() MovieClip.
.

:
ActionScript.org - attachMovie


. , flash, .

, :
, PHP


, , .

, ,

-gMale

+2

Just install it to place the item in the next open slot. It is not that difficult.

+1
source

Source: https://habr.com/ru/post/1764860/


All Articles