ひよこちゃん(2.5D)ジャンプする!
ひよこちゃん(2.5D)ジャンプする!
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6Fy4
*/
////////////////////////////////////////////////////////////////////////////////
// ひよこちゃん(2.5D)ジャンプする!
////////////////////////////////////////////////////////////////////////////////
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;
[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/piyo25d.swf";
private var Piyo:Class;
private var piyo:MovieClip;
private var _height:Number = 0;
private static var initspeed:uint = 50;
private var speed:uint = initspeed;
private var bounce:Number = 0.8;
private var acceleration:Number = 0;
private static var gravity:uint = 4;
private static var limit:uint = 18;
private var se:SoundEffect;
private var soundPath:String = "http://www.project-nya.jp/images/flash/bound.mp3";
public function Main() {
//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));
se = new SoundEffect();
se.load(soundPath, "bound");
}
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 = 232;
piyo.y = 400;
piyo.rotate(0);
piyo.buttonMode = true;
piyo.mouseChildren = false;
piyo.addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
private function click(evt:MouseEvent):void {
piyo.mouseEnabled = false;
speed = initspeed;
addEventListener(Event.ENTER_FRAME, jump, false, 0, true);
}
private function jump(evt:Event):void {
acceleration += gravity;
_height += speed - acceleration;
if (_height <= 0) {
se.play("bound", 0.2);
_height = 0;
acceleration = 0;
speed = speed*bounce;
}
piyo.base.y = - _height;
piyo.shade.alpha = 1 - _height/200;
piyo.rotate(_height*1.28);
if (speed < limit) {
//altitude = 0;
_height = 0;
acceleration = 0;
removeEventListener(Event.ENTER_FRAME, jump);
piyo.mouseEnabled = true;
}
}
/////////////////////////////////////////////
//背景
/////////////////////////////////////////////
private function draw():void {
drawSky();
drawGround();
drawSun();
}
private function drawSky():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 350, 0.5*Math.PI, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, [0x3F68AB, 0x77B2EE], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 0, 465, 350);
graphics.endFill();
}
private function drawGround():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 115, 0.5*Math.PI, 0, 350);
graphics.beginGradientFill(GradientType.LINEAR, [0x99CC33, 0x7EB133], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 350, 465, 115);
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));
}
}
}
//////////////////////////////////////////////////
// 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.events.ProgressEvent;
import flash.net.URLRequest;
class SoundEffect extends EventDispatcher {
private static var soundList:Object;
private var sound:Sound;
private var channel:SoundChannel;
private static var initialized:Boolean = false;
private var volume:Number;
private var looping:Boolean = false;
public function SoundEffect() {
if (!initialized) initialize();
}
private static function initialize():void {
initialized = true;
soundList = new Object();
}
public function init(Snd:Class, id:String):void {
var snd:Sound = new Snd();
soundList[id] = snd;
}
public function load(file:String, id:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(file));
snd.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
snd.addEventListener(Event.COMPLETE, loaded, false, 0, true);
soundList[id] = snd;
}
public function play(id:String, vol:Number, loop:Boolean = false):void {
if (channel != null) channel.stop();
sound = soundList[id];
volume = vol;
looping = loop;
channel = sound.play();
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
if (looping) {
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
}
}
public function stop():void {
if (channel != null) {
channel.stop();
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
}
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function loaded(evt:Event):void {
dispatchEvent(evt);
}
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 = volume;
channel.soundTransform = transform;
}
}
}