actionscript 3 - Wrap items in a grid -
im making inventory system , iv managed add/remove , sort items on x axis. add , remove , resort them on 1 line. cant seem figure out how make go down 1 y space every 10 tiles, or so. im using method thing in game, idk.. ic ant figure out how inventory
tile.x = tile_size * (i % 800); tile.y = tile_size * (j % 600);
heres code adds items inventory
protected function addinvitem(item:movieclip, c:class) { item = new c(); inventory.itemsininventory.push(item); inventory.inventorysprite.addchild(item); item.x = (inventory.itemsininventory.length-1)*40; item.y = 0; item.width = 30; item.height = 25; item.addeventlistener(mouseevent.click, inventory.useitem); }
this code deletes , sorts inventory when u click on item
public function useitem(e:mouseevent) { var item:movieclip = movieclip(e.currenttarget); inventorysprite.removechild(item); itemsininventory.splice(itemsininventory.indexof(item), 1); item.removeeventlistener(mouseevent.click, useitem); sortinventory(); } public function sortinventory() { (var i:int = 0; < itemsininventory.length; i++) { itemsininventory[i].x = * 40; } }
use modular operator
var len:int = inventory.itemsininventory.length; item.x = 40 * (len%10); item.y = 40 * math.floor(len/10);
your loop this:
for (var i:int = 0; < itemsininventory.length; i++) { itemsininventory[i].x = 40 * (i%10); itemsininventory[i].y = 40 * math.floor(i/10); }
Comments
Post a Comment