Aquarium (1)
//////////////////////////////////////////////////////////////////////////////
Aquarium (1)
[AS3.0] アクアリウム
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1087
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sYlZ
*/
////////////////////////////////////////////////////////////////////////////////
// Aquarium (1)
//
// [AS3.0] アクアリウム
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1087
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.system.Security;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var aquarium:Aquarium;
public function Main() {
Wonderfl.capture_delay(8);
init();
}
private function init():void {
Security.allowDomain("www.project-nya.jp");
Security.loadPolicyFile("http://www.project-nya.jp/crossdomain.xml");
aquarium = new Aquarium();
addChild(aquarium);
var base:Shape = new Shape();
addChild(base);
base.y = 450;
base.graphics.beginFill(0x000000, 0.8);
base.graphics.drawRect(0, 0, 465, 15);
base.graphics.endFill();
}
}
}
//////////////////////////////////////////////////
// Aquariumクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
class Aquarium extends Sprite {
public var water:Sprite;
public var glass:Sprite;
private var scene1:Scene;
private var scene2:Scene;
private var back:PhotoLoader;
private var bubble:ContentLoader;
private var plants:PhotoLoader;
private var front:PhotoLoader;
private var bgm:SoundEffect;
private var se:SoundEffect;
private static var basePath:String = "http://www.project-nya.jp/images/flash/aquarium/";
private static var backPath:String = "back.png";
private static var bubblePath:String = "bubble.swf";
private static var plantsPath:String = "plants.png";
private static var frontPath:String = "front.png";
private static var bubblesPath:String = "bubbles.mp3";
private static var knockPath:String = "knock.mp3";
public function Aquarium() {
init();
draw();
}
private function init():void {
bgm = new SoundEffect();
bgm.addEventListener(Event.COMPLETE, loaded, false, 0, true);
bgm.load(basePath + bubblesPath);
se = new SoundEffect();
se.load(basePath + knockPath);
}
private function draw():void {
scene1 = new Scene();
scene1.color = [0x0099FF, 0x000099];
scene2 = new Scene();
scene2.color = [0x0033FF, 0x000000];
var date:Date = new Date();
if (date.hours > 5 && date.hours < 18) {
addChild(scene1);
} else {
addChild(scene2);
}
back = new PhotoLoader();
addChild(back);
back.load(basePath + backPath, true);
bubble = new ContentLoader();
addChild(bubble);
bubble.load(basePath + bubblePath);
plants = new PhotoLoader();
addChild(plants);
plants.load(basePath + plantsPath, true);
water = new Sprite();
addChild(water);
front = new PhotoLoader();
addChild(front);
front.load(basePath + frontPath, true);
glass = new Sprite();
addChild(glass);
glass.graphics.beginFill(0x000000, 0);
glass.graphics.drawRect(0, 0, 465, 450);
glass.graphics.endFill();
glass.buttonMode = true;
glass.addEventListener(MouseEvent.CLICK, knock, false, 0, true);
}
private function loaded(evt:Event):void {
bgm.play(0.2, true);
}
private function knock(evt:MouseEvent):void {
if (se.initialized) {
se.play(0.5);
var value:Object = {x: mouseX, y: mouseY};
dispatchEvent(new CompoEvent(CompoEvent.SELECT, value));
}
}
}
//////////////////////////////////////////////////
// Sceneクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
class Scene extends Sprite {
private static var color1:uint;
private static var color2:uint;
public function Scene() {
}
public function set color(list:Array):void {
color1 = list[0];
color2 = list[1];
draw();
}
private function draw():void {
var base:Shape = new Shape();
addChild(base);
createBase(base);
var side:Shape = new Shape();
addChild(side);
createSide(side);
}
private function createBase(target:Shape):void {
var colors:Array = [color1, color2];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(345, 365, 0.5*Math.PI, 60, 40);
graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
graphics.drawRect(60, 40, 345, 365);
graphics.endFill();
}
private function createSide(target:Shape):void {
var colors:Array = [color1, color2];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 465, 0.5*Math.PI, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
graphics.moveTo(0, 0);
graphics.lineTo(0, 465);
graphics.lineTo(60, 405);
graphics.lineTo(60, 40);
graphics.lineTo(0, 0);
graphics.moveTo(465, 0);
graphics.lineTo(465, 465);
graphics.lineTo(405, 405);
graphics.lineTo(405, 40);
graphics.lineTo(465, 0);
graphics.endFill();
}
}
//////////////////////////////////////////////////
// PhotoLoaderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.LoaderContext;
class PhotoLoader extends Sprite {
private var loader:Loader;
private var info:LoaderInfo;
public var content:*;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function PhotoLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, false);
info.addEventListener(Event.COMPLETE, complete, false, 0, false);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(PhotoLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(PhotoLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(PhotoLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
if (smoothing) {
content = Bitmap(info.content);
content.smoothing = true;
} else {
content = info.content;
}
dispatchEvent(new Event(PhotoLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
addChild(loader);
dispatchEvent(new Event(PhotoLoader.COMPLETE));
}
public function centerize():void {
content.x = -uint(content.width*0.5);
content.y = -uint(content.height*0.5);
}
}
//////////////////////////////////////////////////
// ContentLoaderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
//import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.Security;
import flash.system.LoaderContext;
class ContentLoader extends Sprite {
public var id:uint;
private var loader:Loader;
private var info:LoaderInfo;
public var content:*;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function ContentLoader() {
Security.allowDomain("www.project-nya.jp");
Security.loadPolicyFile("http://www.project-nya.jp/crossdomain.xml");
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String):void {
info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, false);
info.addEventListener(Event.COMPLETE, complete, false, 0, false);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(ContentLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(ContentLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(ContentLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
//content = info.content;
dispatchEvent(new Event(ContentLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
addChild(loader);
dispatchEvent(new Event(ContentLoader.COMPLETE));
}
}
//////////////////////////////////////////////////
// SoundEffectクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
class SoundEffect extends EventDispatcher {
public var id:String;
private var sound:Sound;
private var channel:SoundChannel;
private var level:Number;
private var volume:Number = 1;
private var looping:Boolean = false;
public var initialized:Boolean = false;
public var playing:Boolean = false;
public function SoundEffect() {
}
public function init(Snd:Class):void {
sound = new Snd();
}
public function load(filePath:String):void {
sound = new Sound();
sound.load(new URLRequest(filePath));
sound.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
sound.addEventListener(Event.COMPLETE, initialize, false, 0, true);
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function initialize(evt:Event):void {
initialized = true;
channel = sound.play();
channel.stop();
dispatchEvent(evt);
}
public function play(lv:Number, loop:Boolean = false):void {
playing = true;
channel.stop();
level = lv;
looping = loop;
channel = sound.play();
var transform:SoundTransform = channel.soundTransform;
transform.volume = level*volume;
channel.soundTransform = transform;
if (looping) {
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
}
}
public function stop():void {
playing = false;
channel.stop();
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
}
public function setVolume(v:Number):void {
volume = v;
var transform:SoundTransform = channel.soundTransform;
transform.volume = level*volume;
channel.soundTransform = transform;
}
private function complete(evt:Event):void {
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
if (looping) {
channel = sound.play(0);
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
var transform:SoundTransform = channel.soundTransform;
transform.volume = level*volume;
channel.soundTransform = transform;
} else {
playing = false;
}
}
}
//////////////////////////////////////////////////
// CompoEventクラス
//////////////////////////////////////////////////
import flash.events.Event;
class CompoEvent extends Event {
public static const SELECT:String = "select";
public static const CHANGE:String = "change";
public var value:*;
public function CompoEvent(type:String, value:*) {
super(type);
this.value = value;
}
public override function clone():Event {
return new CompoEvent(type, value);
}
}