Inventory packing
Diablo 2 style inventory packing prototype thing
♥0 |
Line 133 |
Modified 2012-05-23 19:14:24 |
MIT License
archived:2017-03-30 23:01:00
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/1PwK
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
//myItem = new aItem();
// addChild(myItem);
graphics.clear();
graphics.lineStyle(1,0);
var k:int;
var j:int;
for (k = 0; k < 8; k++)
{
for (j = 0; j < 8; j++)
{
}//nextj
}//nextk
var i:int;
var a:aItem;
for (i = 0; i < 8; i++)
{
a = new aItem();
a.x = i * 64;
addChild(a);
}//nexti
addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
//public var myItem:aItem;
public function onEnter(e:Event):void
{
if (aItem.grab != null)
{
aItem.grab.x = mouseX - aItem.grab.width*0.5;
aItem.grab.y = mouseY - aItem.grab.height*0.5;
aItem.grab.stickGrid();
}//endif
}//onenter
}//classend
}
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject
internal class aItem extends Sprite
{
public static var grab:aItem;
public var ox:Number = 0;
public var oy:Number = 0;
public var w:Number = 64;
public var h:Number = 96;
public function aItem()
{
graphics.clear();
graphics.lineStyle(1,0);
graphics.beginFill(Math.random()*0xFFffFf,1);
graphics.drawRect(0,0,64,96);
graphics.endFill();
addEventListener(MouseEvent.MOUSE_DOWN, mdown);
// addEventListener(MouseEvent.MOUSE_UP, mup);
}//ctor
public function mdown(e:MouseEvent):void
{
if (grab == null)
{
grabItem();
/*
ox = x;
oy = y;
grab = this;*/
}
else
{
if (grab == this)
{
stickGrid();
//grab = null;
checkOverlap();
}
}//endif
}//mdown
/*
public function mup(e:MouseEvent):void
{
if (grab == this) { grab = null; }
putDown();
}//mup
*/
public function stickGrid():void
{
x = Math.floor(x/32)*32;
y = Math.floor(y/32)*32;
}//putdown
public function isOver(o:aItem):Boolean
{
if (x + w <= o.x) { return false; }
if (y + h <= o.y) { return false; }
if (o.x + o.w <= x) { return false;}
if (o.y + o.h <= y) { return false; }
return true;
}//isover
public function grabItem():void
{
ox = x;
oy = y;
grab = this;
toFront();
}//grab
public function toFront():void
{
var p:DisplayObjectContainer;
p = this.parent;
if (p != null)
{
p.swapChildrenAt(p.getChildIndex(this), p.numChildren-1);
}
}//toFront
public function checkOverlap():void
{
if (grab != this) { return; }
var p:DisplayObjectContainer;
var a:aItem;
var i:int;
var col:aItem;
var num:int;
p = this.parent;
if (p != null)
{
num = p.numChildren;
for (i = 0; i < num; i++)
{
a = p.getChildAt(i) as aItem;
if (a == this) { continue; }
if (a.isOver(this))
{
if (col == null) { col = a as aItem; }
else
{
//overlaps more than one item
// grab = null;
// x = ox;
// y = oy;
return;
} //endif2
}//endif
}//nexti
}//endif
if (col == null)
{ grab = null; }
else {
col.grabItem();
//col.ox = col.x; col.oy = col.y; //save old position of item
//grab = col; //grab it
}//endif
}//checkover
}//sprite