ひよこちゃん簡易チャット
//////////////////////////////////////////////////////////////////////////////
ひよこちゃん簡易チャット
[AS3.0] PHPと連携 (2)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1038
テキスト入力して、Enterキーで会話できます。
会話辞書がしょぼいので、稚拙な会話しかできません。
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/o1Wq
*/
////////////////////////////////////////////////////////////////////////////////
// ひよこちゃん簡易チャット
//
// [AS3.0] PHPと連携 (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1038
//
// テキスト入力して、Enterキーで会話できます。
// 会話辞書がしょぼいので、稚拙な会話しかできません。
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.Security;
import flash.system.LoaderContext;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.net.URLVariables;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private static var sunshinePath:String = "9/9b/9bbe/9bbec77d53bddc5e7a5f2c00abbade6bd641c549";
private var loader:Loader;
private static var piyoPath:String = "http://www.project-nya.jp/images/flash/piyo2d.swf";
private var Piyo:Class;
private var piyo:MovieClip;
private var message:PlateMessage;
private var reply:PlateReply;
private var replyMsg:String;
private var timer:Timer;
private var connector:PHPConnector;
private var phpPath:String = "http://www.project-nya.jp/images/flash/php/message.php";
public function Main() {
//Wonderfl.disable_capture();
Wonderfl.capture_delay(4);
init();
}
private function init():void {
draw();
Security.allowDomain("www.project-nya.jp");
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete, false, 0, true);
loader.load(new URLRequest(piyoPath), new LoaderContext(true));
}
private function complete(evt:Event):void {
loader.removeEventListener(Event.COMPLETE, complete);
var content:MovieClip = MovieClip(evt.target.content);
//Piyoクラス
Piyo = MovieClip(content.piyo).constructor;
setup();
loader = null;
}
private function setup():void {
//Piyoインスタンス
piyo = new Piyo();
addChild(piyo);
piyo.x = 172;
piyo.y = 300;
piyo.scale = 2;
var you:MovieClip = new Piyo();
addChild(you);
you.x = 292;
you.y = 360;
you.scale = 2;
you.eyeL.visible = false;
you.eyeR.visible = false;
you.mouth.visible = false;
message = new PlateMessage();
addChild(message);
message.x = 292;
message.y = 380;
message.init({width: 300, height: 30, offset: -60});
message.txt.addEventListener(KeyboardEvent.KEY_DOWN, keyDown, false, 0, true);
reply = new PlateReply();
addChild(reply);
reply.x = 172;
reply.y = 170;
reply.init({width: 240, height: 80});
reply.visible = false;
connector = new PHPConnector();
connector.addEventListener(Event.COMPLETE, receive, false, 0, true);
}
/////////////////////////////////////////////
//送信
/////////////////////////////////////////////
private function keyDown(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.ENTER) {
var msg:String = message.text;
if (check(msg)) send(msg);
}
}
private function check(msg:String):Boolean {
if (msg.length > 0) {
var str:String = msg.concat();
str = str.split(" ").join("");
str = str.split(" ").join("");
if (str.length > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
private function send(msg:String):void {
message.enable = false;
message.show(msg);
message.txt.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
reply.visible = false;
var variables:URLVariables = new URLVariables();
variables.message = msg;
connector.connect(phpPath, variables);
//you.speak();
}
/////////////////////////////////////////////
//受信
/////////////////////////////////////////////
private function receive(evt:Event):void {
var variables:URLVariables = URLVariables(evt.target.data);
replyMsg = variables.reply;
timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, showReply, false, 0, true);
timer.start();
}
private function showReply(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, showReply);
reply.visible = true;
reply.show(replyMsg);
//piyo.speak();
timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, clear, false, 0, true);
timer.start();
}
private function clear(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, clear);
message.show("");
timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, showMessage, false, 0, true);
timer.start();
}
private function showMessage(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, showMessage);
message.enable = true;
message.txt.addEventListener(KeyboardEvent.KEY_DOWN, keyDown, false, 0, true);
}
/////////////////////////////////////////////
//背景
/////////////////////////////////////////////
private function draw():void {
drawSky();
drawGround();
drawSun();
}
private function drawSky():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 250, 0.5*Math.PI, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, [0x3F68AB, 0x77B2EE], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 0, 465, 250);
graphics.endFill();
}
private function drawGround():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 215, 0.5*Math.PI, 0, 250);
graphics.beginGradientFill(GradientType.LINEAR, [0x99CC33, 0x7EB133], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 250, 465, 215);
graphics.endFill();
}
private function drawSun():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(200, 200, 0, -90, -90);
graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0xFFFFFF, 0xFFFFFF], [1, 0.3, 0], [25, 102, 231], matrix);
graphics.drawCircle(10, 10, 100);
graphics.endFill();
var shine:Loader = new Loader();
addChild(shine);
shine.alpha = 0.5;
shine.load(new URLRequest(basePath + sunshinePath), new LoaderContext(true));
}
}
}
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
class PHPConnector extends EventDispatcher {
private var loader:URLLoader;
private var _data:*;
public static const TEXT:String = URLLoaderDataFormat.TEXT;
public static const BINARY:String = URLLoaderDataFormat.BINARY;
public static const VARIABLES:String = URLLoaderDataFormat.VARIABLES;
public static const COMPLETE:String = Event.COMPLETE;
public function PHPConnector() {
loader = new URLLoader();
}
public function connect(file:String, variables:URLVariables = null):void {
loader.dataFormat = PHPConnector.VARIABLES;
loader.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
var request:URLRequest = new URLRequest(file);
if (variables) {
request.method = URLRequestMethod.POST;
request.data = variables;
}
loader.load(request);
} catch (err:Error) {
trace(err.message);
}
}
private function ioerror(evt:IOErrorEvent):void {
trace(evt.text);
}
private function httpstatus(evt:HTTPStatusEvent):void {
trace(evt.status);
}
private function securityerror(evt:SecurityErrorEvent):void {
trace(evt.text);
}
private function complete(evt:Event):void {
loader.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
loader.removeEventListener(Event.COMPLETE, complete);
_data = evt.target.data;
dispatchEvent(new Event(PHPConnector.COMPLETE));
}
public function get data():* {
return _data;
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
class PlateMessage extends Sprite {
private var base:Sprite;
private var plate:Shape;
private var arrow:Shape;
public var txt:TextField;
private static var fontType:String = "_ゴシック";
private var px:uint;
private var py:uint;
private var _width:uint = 100;
private var _height:uint = 60;
private static var xOffset:uint = 15;
private static var yOffset:uint = 10;
private var offset:int = 0;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private static var tColor:uint = 0x000000;
private var shade:DropShadowFilter;
private var _visible:Boolean = false;
public function PlateMessage() {
if (stage) {
initialize(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, initialize, false, 0, true);
}
}
public function init(option:Object):void {
if (option.width != undefined) _width = option.width;
if (option.height != undefined) _height = option.height;
if (option.offset != undefined) offset = option.offset;
draw();
}
private function draw():void {
shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 1.5, 2, false, false);
base = new Sprite();
plate = new Shape();
arrow = new Shape();
txt = new TextField();
addChild(base);
base.addChild(plate);
base.addChild(arrow);
base.addChild(txt);
createBox(plate, -_width*0.5, 0, _width, _height, 4);
createArrow(arrow, 6, 16);
base.y = yOffset;
plate.x = offset;
txt.x = -_width*0.5 + 5 + offset;
txt.y = 4;
txt.width = _width - 10;
txt.height = _height - 8;
enable = true;
txt.maxChars = 25;
txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
txt.textColor = tColor;
filters = [shade];
}
private function initialize(evt:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, initialize);
}
public function show(t:String):void {
txt.text = t;
}
public function get text():String {
return txt.text;
}
public function set enable(param:Boolean):void {
if (param) {
txt.type = TextFieldType.INPUT;
stage.focus = txt;
} else {
txt.type = TextFieldType.DYNAMIC;
stage.focus = null;
}
txt.selectable = param;
}
private function createBox(target:Shape, x:int, y:int, w:uint, h:uint, c:uint):void {
target.graphics.beginFill(bColor);
target.graphics.drawRoundRect(x, y, w, h, c*2);
target.graphics.endFill();
}
private function createArrow(target:Shape, w:uint, h:uint):void {
target.graphics.beginFill(bColor);
target.graphics.moveTo(0, 0);
target.graphics.lineTo(-w*0.5, 0);
target.graphics.lineTo(0, -h);
target.graphics.lineTo(w*0.5, 0);
target.graphics.lineTo(0, 0);
target.graphics.endFill();
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
class PlateReply extends Sprite {
private var base:Sprite;
private var plate:Shape;
private var arrow:Shape;
private var txt:TextField;
private static var fontType:String = "_ゴシック";
private var px:uint;
private var py:uint;
private var _width:uint = 100;
private var _height:uint = 60;
private static var xOffset:uint = 15;
private static var yOffset:uint = 10;
private var offset:int = 0;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private static var tColor:uint = 0x000000;
private var shade:DropShadowFilter;
private var _visible:Boolean = false;
public function PlateReply() {
if (stage) {
initialize(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, initialize, false, 0, true);
}
}
public function init(option:Object):void {
if (option.width != undefined) _width = option.width;
if (option.height != undefined) _height = option.height;
if (option.offset != undefined) offset = option.offset;
draw();
}
private function draw():void {
shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 1.5, 2, false, false);
base = new Sprite();
plate = new Shape();
arrow = new Shape();
txt = new TextField();
addChild(base);
base.addChild(plate);
base.addChild(arrow);
base.addChild(txt);
createBox(plate, -_width*0.5, -_height, _width, _height, 4);
createArrow(arrow, 6, 16);
base.y = - yOffset;
plate.x = offset;
txt.x = -_width*0.5 + 5 + offset;
txt.y = -_height + 4;
txt.width = _width - 10;
txt.height = _height - 8;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
txt.multiline = true;
txt.wordWrap = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
txt.textColor = tColor;
filters = [shade];
}
private function initialize(evt:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, initialize);
}
public function show(t:String):void {
//txt.text = t;
txt.htmlText = t;
}
private function createBox(target:Shape, x:int, y:int, w:uint, h:uint, c:uint):void {
target.graphics.beginFill(bColor);
target.graphics.drawRoundRect(x, y, w, h, c*2);
target.graphics.endFill();
}
private function createArrow(target:Shape, w:uint, h:uint):void {
target.graphics.beginFill(bColor);
target.graphics.moveTo(0, 0);
target.graphics.lineTo(-w*0.5, 0);
target.graphics.lineTo(0, h);
target.graphics.lineTo(w*0.5, 0);
target.graphics.lineTo(0, 0);
target.graphics.endFill();
}
}