アイテムを放り投げてお着替え
アイテムをつかんで投げてみて下さい。
現在身に着けているものと同じものを投げると脱がす事ができます。
一応Fullscreenにも対応しています。
// forked from ahn's Ameba Pigg Sample
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF (width = "465", height = "465", frameRate = "30", backgroundColor = "0xFFFFFF")]
public class Index extends Sprite {
private var _stageWidth:int;
private var _stageHeight:int;
private var _imageURL:String = "http://assets.wonderfl.net/images/related_images/0/08/08c6/08c6abba45f60ced2e7e58322dc4c59144bdcbaa";
private var _loader:Loader;
private var _piggItem:PiggItem;
private var _iconContainer:IconContainer;
private var _assigneContainer:AssigneContainer;
private var _throwItemList:Array = [];
private var _draggedID:int;
private var _playingFlgList:Array;
public function Index() {
_loader = new Loader();
_loader.load(new URLRequest(_imageURL), new LoaderContext(true));
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _init);
}
private function _init(event:Event):void {
_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _init);
stage.scaleMode = "noScale";
stage.align = "TL";
_stageWidth = stage.stageWidth;
_stageHeight = stage.stageHeight;
_piggItem = new PiggItem(Bitmap(_loader.content).bitmapData);
_initIcons();
_initCharacter();
_initThrowItem();
addChildAt(new Bitmap(_createBackGround()), 0);
_iconContainer.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
}
private function _initCharacter():void {
_assigneContainer = new AssigneContainer(_piggItem);
_assigneContainer.x = 32;
_assigneContainer.y = (_stageHeight - _assigneContainer.height + 36) >> 1;
addChild(_assigneContainer);
_changeItem("top", 2);
_changeItem("bottom", 4);
_changeItem("foot", 6);
}
private function _initIcons():void {
var iconList:Array = _piggItem.iconList;
_iconContainer = new IconContainer(48, 48, 2);
addChild(_iconContainer);
for (var i:int = 0; i < iconList.length; i++) {
_iconContainer.add(iconList[i]);
}
_iconContainer.x = _stageWidth - 24 - _iconContainer.width;
_iconContainer.y = (_stageHeight - _iconContainer.height) >> 1;
}
private function _initThrowItem():void {
_throwItemList.push(_piggItem.capList[0], _piggItem.capList[2]);
_throwItemList.push(_piggItem.topList[0], _piggItem.topList[2]);
_throwItemList.push(_piggItem.bottomList[0], _piggItem.bottomList[2]);
_throwItemList.push(_piggItem.footList[0], _piggItem.footList[2]);
for (var i:int = 0; i < _throwItemList.length; i++) {
var item:Item = _throwItemList[i];
item.id = i;
item.content.x -= item.content.width >> 1;
item.content.y -= item.content.height >> 1;
item.content.smoothing = true;
}
_playingFlgList = [false, false, false, false];
}
private function _changeItem(itemName:String, id:int):void {
_assigneContainer.changeItem(itemName, id);
_iconContainer.fillUsingItem(_assigneContainer.currentIDList);
}
private var _tempX:Number = 0;
private var _tempY:Number = 0;
private var _dx:Number = 0;
private var _dy:Number = 0;
private function _mouseDownHandler(event:MouseEvent):void {
var id:int = event.target.id;
if (_playingFlgList[Math.floor(id * 0.5)] == true) return;
var item:Item = _throwItemList[id];
addChild(item);
_tempX = stage.mouseX;
_tempY = stage.mouseY;
item.x = _tempX;
item.y = _tempY + 4;
item.startDrag();
_draggedID = id;
stage.addEventListener(Event.ENTER_FRAME, _enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
}
private function _enterFrameHandler(event:Event):void {
var currentX:Number = stage.mouseX;
var currentY:Number = stage.mouseY;
_dx = currentX - _tempX;
_dy = currentY - _tempY;
_tempX = currentX;
_tempY = currentY;
}
private function _mouseUpHandler(event:MouseEvent):void {
var id:int = _draggedID;
var item:Item = _throwItemList[id];
item.stopDrag();
if (_dx < -1) {
var target:Item = _assigneContainer.maskList[id];
var targetX:Number = _assigneContainer.x + target.x + target.width;
var dx:Number = targetX - item.x;
if (dx > 0) {
removeChild(item);
} else {
var targetY:Number = _assigneContainer.y + target.y + target.height * 0.5;
var dy:Number = targetY - item.y;
var d:Number = Math.sqrt(dx * dx + dy * dy);
var vy:Number = Math.min(24, Math.max(-24, _dy));
var frame:int = int(4 + Math.sqrt(d) - Math.sqrt(Math.abs(_dx)) + Math.abs(vy) * 0.3);
var vx:Number = dx / frame;
var ay:Number = 2 * (dy / frame - vy) / frame;
var av:Number = -(360 * Math.floor(1 + frame / 20)) / frame;
var anime:InstantAnimation = new InstantAnimation(item);
anime.play(frame, {vx:vx, vy:vy, ax:0, ay:ay}, {v:av, a:0});
_playingFlgList[Math.floor(id * 0.5)] = true;
anime.addEventListener(Event.COMPLETE, function(event:Event):void {
_changeItem(item.name, id);
removeChild(item);
_playingFlgList[Math.floor(id * 0.5)] = false;
anime.removeEventListener(Event.COMPLETE, arguments.callee);
});
}
} else {
removeChild(item);
}
stage.removeEventListener(Event.ENTER_FRAME, _enterFrameHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
}
private function _createBackGround():BitmapData {
var tile:BitmapData = new BitmapData(8, 8, true, 0x000000);
var color1:uint = 0x10000000;
var color2:uint = 0x0C000000;
for (var i:int = 0; i < 8; i++) {
tile.setPixel32(i, i, color1);
tile.setPixel32((i + 1) % 8, i, color2);
tile.setPixel32(i, (i + 1) % 8, color2);
}
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.beginFill(0xF6F2CB);
g.drawRect(0, 64, _stageWidth, _stageHeight - 128);
g.beginBitmapFill(tile);
g.drawRect(0, 64, _stageWidth, _stageHeight - 128);
g.beginFill(0x111111, 0.1);
g.drawRect(_assigneContainer.x - 18, _assigneContainer.y - 28, _assigneContainer.width + 16, _assigneContainer.height + 16);
var bg:BitmapData = new BitmapData(_stageWidth, _stageHeight, false, 0x666666);
bg.draw(shape);
var matrix:Matrix = new Matrix();
matrix.translate(_iconContainer.x - 8, _iconContainer.y - 8);
bg.draw(_createBaseImage(_iconContainer.width + 16, _iconContainer.height + 16), matrix);
return bg;
}
private function _createBaseImage(width:int, height:int):BitmapData {
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.beginFill(0x202020, 0.5);
g.drawRoundRect(0, 0, width, height, 5, 5);
g.endFill();
g.lineStyle(0, 0x202020, 0.5);
g.drawRoundRect(0.5, 0.5, width - 1, height - 1, 5, 5);
g.lineStyle(0, 0xFFFFFF, 0.06);
g.drawRoundRect(1.5, 1.5, width - 3, height - 3, 3, 3);
var base:BitmapData = new BitmapData(width + 8, height + 8, true, 0xFFFFFF);
base.draw(shape);
base.applyFilter(base, base.rect, new Point(), new DropShadowFilter(1, 45, 0, 0.7, 2, 2));
return base;
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
class PiggItem {
public var character:Item;
public var sitagi:Item;
public var capList:Array = [];
public var topList:Array = [];
public var bottomList:Array = [];
public var footList:Array = [];
public var iconList:Array = [];
public function PiggItem(image:BitmapData) {
_separateItems(image);
}
private function _separateItems(image:BitmapData):void {
var list:Array = [1, 1, 106, 172, 1, 266, 34, 288, 222, 1, 348, 79, 222, 81, 348, 159, 108, 1, 220, 147, 108, 149, 220, 295, 1, 174, 60, 219, 1, 221, 60, 264, 300, 161, 339, 203, 300, 205, 339, 247, 261, 161, 298, 199, 261, 201, 298, 237, 222, 161, 259, 213, 222, 215, 259, 263, 62, 174, 105, 198, 62, 200, 105, 224, 62, 226, 105, 253, 62, 255, 105, 282, 99, 301, 146, 348, 148, 301, 195, 348, 295, 301, 342, 348, 295, 252, 342, 299, 1, 301, 48, 348, 50, 301, 97, 348, 197, 301, 244, 348, 246, 301, 293, 348];
var i:int = 0;
character = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
sitagi = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
var item:Item;
for (; i < 24; ) {
item = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
item.name = "cap";
capList.push(item);
}
for (; i < 40; ) {
item = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
item.name = "top";
topList.push(item);
}
for (; i < 56; ) {
item = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
item.name = "bottom";
bottomList.push(item);
}
for (; i < 72; ) {
item = new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++])));
item.name = "foot";
footList.push(item);
}
for (; i < 104; ) {
iconList.push(new Item(new Bitmap(_clipImage(image, list[i++], list[i++], list[i++], list[i++]))));
}
}
private function _clipImage(image:BitmapData, x1:int, y1:int, x2:int, y2:int):BitmapData {
var rect:Rectangle = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
var data:BitmapData = new BitmapData(rect.width, rect.height, true, 0xFFFFFF);
data.copyPixels(image, rect, new Point());
return data;
}
}
class Item extends Sprite {
public var content:Bitmap;
public var id:int;
public function Item(content:Bitmap = null) {
this.content = content;
if (content != null) addChild(content);
}
}
class IconContainer extends Sprite {
public var list:Array = [];
public var count:int = 0;
private var _iconWidth:int;
private var _iconHeight:int;
private var _row:int;
private var _offsetX:int;
private var _offsetY:int;
private var _usingShape:Shape;
public function IconContainer(iconWidth:int, iconHeight:int, row:int, offsetX:Number = 6, offsetY:Number = 6) {
_iconWidth = iconWidth;
_iconHeight = iconHeight;
_row = row;
_offsetX = offsetX;
_offsetY = offsetY;
mouseEnabled = false;
buttonMode = true;
_usingShape = Shape(addChild(new Shape()));
}
public function add(icon:Item):void {
icon.x = (count % _row) * (_iconWidth + _offsetX);
icon.y = Math.floor(count / _row) * (_iconHeight + _offsetY);
icon.id = count++;
list.push(icon);
addChildAt(icon, 0);
}
public function fillUsingItem(currentIDList:Object):void {
var g:Graphics = _usingShape.graphics;
g.clear();
g.beginFill(0x2869CB, 0.16);
for each (var id:int in currentIDList) {
if (id >= 0) {
var icon:Item = list[id];
g.drawRoundRect(icon.x, icon.y, _iconWidth, _iconHeight, 3, 3);
}
}
}
}
class AssigneContainer extends Sprite {
public var character:Item;
public var sitagi:Item;
public var empty:Item;
public var maskList:Array = [];
public var currentIDList:Object = {cap:-1, top:-1, bottom:-1, foot:-1};
public function AssigneContainer(piggItem:PiggItem) {
character = piggItem.character;
addChild(character);
sitagi = piggItem.sitagi;
sitagi.x = 37;
sitagi.y = 125;
addChild(sitagi);
empty = new Item();
maskList.push(piggItem.capList[1], piggItem.capList[3]);
maskList.push(piggItem.topList[1], piggItem.topList[3]);
maskList.push(piggItem.bottomList[1], piggItem.bottomList[3]);
maskList.push(piggItem.footList[1], piggItem.footList[3]);
var list:Array = [-16, -6, -3, -24, 25, 92, 35, 91, 35, 118, 35, 115, 27, 148, 27, 144];
for (var i:int = 0; i < maskList.length; i++) {
var index:int = maskList.length - 1 - i;
var mask:Item = maskList[index];
mask.x = list[index * 2];
mask.y = list[index * 2 + 1];
mask.visible = false;
mask.id = index;
addChild(mask);
}
}
public function changeItem(itemName:String, id:int):void {
var item:Item = maskList[id];
if (item.name != itemName) return;
var currentID:int = currentIDList[itemName];
if (id == currentID) {
currentIDList[itemName] = -1;
item.visible = false;
return;
}
var currentItem:Item;
if (currentID >= 0) {
currentItem = maskList[currentID];
} else {
currentItem = empty;
}
item.visible = true;
currentItem.visible = false;
currentIDList[itemName] = id;
}
}
class InstantAnimation extends Shape {
private var _target:DisplayObject;
public function InstantAnimation(target:DisplayObject) {
_target = target;
}
public function play(frame:int, linear:Object = null, angular:Object = null):void {
if (linear == null) linear = {vx:0, vy:0, ax:0, ay:0};
if (angular == null) angular = {v:0, a:0};
var currentFrame:int = 0;
addEventListener(Event.ENTER_FRAME, function(event:Event):void {
if (currentFrame >= frame) {
removeEventListener(Event.ENTER_FRAME, arguments.callee);
dispatchEvent(new Event(Event.COMPLETE));
} else {
_target.x += linear.vx;
_target.y += linear.vy;
linear.vx += linear.ax;
linear.vy += linear.ay;
_target.rotation += angular.v;
angular.v += angular.a;
currentFrame++;
}
});
}
public function get target():DisplayObject {
return _target;
}
}