/**
* Copyright rxr01676 ( http://wonderfl.net/user/rxr01676 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/plKR
*/
// forked from yuugurenote's バネの動きの練習
// クラス化 - isRunning - toText
// バネの根元を表現するクラス - rootSpringSprite
// マウスボタンを押している間、バネの束縛を受けない - xPres - xReles - isPulled
// ( ボタンが効かない時がある )
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.Bitmap;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.filters.BlurFilter;
import flash.geom.Point;
[SWF(width=465,height=465,backgroundColor=0x000000,frameRate=60)]
public class AS111113_01 extends Sprite {
public var sw:Number=stage.stageWidth;
public var sh:Number=stage.stageHeight;
public var myColor:ColorTransform = new ColorTransform();
public var viewBmd:BitmapData=new BitmapData(sw,sh,false,0x000000);
public var viewBmp:Bitmap=new Bitmap(viewBmd);
public var condTF:TextField = new TextField();
public var _mySprite:mySprite;
public var _springRootSprite:SpringRootSprite;
public function AS111113_01() {
addChild(viewBmp);
var tmpBmd:BitmapData;
myColor.redMultiplier=0.9;
myColor.greenMultiplier=0.9;
myColor.blueMultiplier=0.9;
_springRootSprite = new SpringRootSprite(this);
addChild(_springRootSprite);
_mySprite = new mySprite(this);
addChild(_mySprite);
_springRootSprite.addmySprite(_mySprite);
condTF.textColor = 0xFFFFFF;
condTF.autoSize = TextFieldAutoSize.LEFT;
addChild(condTF);
addEventListener(Event.ENTER_FRAME, xEnter);
addEventListener(Event.ENTER_FRAME, xDraw);
addEventListener(MouseEvent.MOUSE_DOWN, xPres);
addEventListener(MouseEvent.MOUSE_UP, xReles);
}
public function xDraw(e:Event):void {
viewBmd.draw(stage,null,myColor);
viewBmd.applyFilter(viewBmd, viewBmd.rect, new Point(), new BlurFilter(8, 8, 3));
}
private function xEnter(e:Event):void {
_springRootSprite.posX = this.mouseX;
_springRootSprite.posY = this.mouseY;
_springRootSprite.updateState();
_mySprite.updateState();
var s:String = _mySprite.toText();
s += _springRootSprite.toText();
condTF.text=s;
}
private function xPres(e:Event):void {
_springRootSprite.isPulling = false;
}
private function xReles(e:Event):void {
_springRootSprite.isPulling = true;
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
class SpringRootSprite extends Sprite {
public var isPulling:Boolean=true;
public var posX:Number=0;
public var posY:Number=0;
public var parentSprite:Sprite;
public var childSprite:mySprite;
public function SpringRootSprite(ps:Sprite) {
this.graphics.beginFill(0x00FF7F,1);
this.graphics.drawCircle(0,0,5);
this.graphics.endFill();
this.parentSprite=ps;
this.x = this.parentSprite.stage.stageWidth / 2 - this.width / 2;
this.y = this.parentSprite.stage.stageWidth / 2 - this.width / 2;
this.posX = this.x;
this.posY = this.y;
}
public function updateState():void {
this.x=this.posX;
this.y=this.posY;
childSprite.oriX=this.posX;
childSprite.oriY=this.posY;
childSprite.isPulled = this.isPulling;
}
public function toText():String {
var s:String = '';
if (this.isPulling) { s += 'Pulling\n'; } else { s += '\n'; }
return s
}
public function addmySprite(ms:mySprite):void {
childSprite = ms;
}
}
class mySprite extends Sprite {
private var _preX:Number=0;
private var _preY:Number=0;
public var parentSprite:Sprite;
public var oriX:Number=0;
public var oriY:Number=0;
public var posX:Number=0;
public var posY:Number=0;
public var speedX:Number=0;
public var speedY:Number=0;
public var minRunningSpeed:Number=0.01;
public var isRunning:Boolean=false;
public var isPulled:Boolean=true;
public var spring:Number=0.1;
public var friction:Number=0.89;
private function _updateRunningState():void {
if (this.isRunning) {
if (Math.abs(this.speedX)<this.minRunningSpeed && Math.abs(this.speedY)<this.minRunningSpeed) {
this.isRunning=false;
this._preX=Math.round(this.x);
this._preY=Math.round(this.y);
}
}
else {
if (this._preX != Math.round(this.x) || this._preY != Math.round(this.y)) {
this.isRunning=true;
}
}
}
private function _updateSpeed():void {
var myIntX:Number=this.oriX-this.posX;
var myIntY:Number=this.oriY-this.posY;
this.speedX+=myIntX*this.spring;
this.speedY+=myIntY*this.spring;
this.speedX*=this.friction;
this.speedY*=this.friction;
}
public function toText():String {
var s:String = "";
s+=sprintf('x=%+010.3f \ty=%+010.3f \tox=%+010.3f \toy=%+010.3f\n', x, y, oriX, oriY);
s+=sprintf('vx=%+010.8f\tvy=%+010.8f\n', speedX, speedY);
if (isRunning) { s+='Running\n'; } else { s+='\n'; }
return s;
}
public function mySprite(ps:Sprite) {
this.graphics.beginFill(0x00FF7F,1);
this.graphics.drawCircle(0,0,5);
this.graphics.endFill();
this.parentSprite=ps;
this.x = this.parentSprite.stage.stageWidth / 2 - this.width / 2;
this.y = this.parentSprite.stage.stageWidth / 2 - this.width / 2;
this.posX = this.x;
this.posY = this.y;
this._preX = this.x;
this._preY = this.y;
}
public function updateState():void {
if(this.isPulled){ this._updateSpeed(); }
this.posX+=this.speedX;
this.posY+=this.speedY;
this.x=this.posX;
this.y=this.posY;
this._updateRunningState();
}
}
/**
* Copyright (c) 2008. Adobe Systems Incorporated.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Adobe Systems Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* sprintf(3) implementation in ActionScript 3.0.
*
* Author: Manish Jethani (manish.jethani@gmail.com)
* Date: April 3, 2006
* Version: 0.1
*
* Copyright (c) 2006 Manish Jethani
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/* sprintf(3) implementation in ActionScript 3.0.
*
* http://www.die.net/doc/linux/man/man3/sprintf.3.html
*
* The following flags are supported: '#', '0', '-', '+'
*
* Field widths are fully supported. '*' is not supported.
*
* Precision is supported except one difference from the standard: for an
* explicit precision of 0 and a result string of "0", the output is "0"
* instead of an empty string.
*
* Length modifiers are not supported.
*
* The following conversion specifiers are supported: 'd', 'i', 'o', 'u', 'x',
* 'X', 'f', 'F', 'c', 's', '%'
*
* Report bugs to manish.jethani@gmail.com
*/
function sprintf(format:String, ... args):String
{
var result:String = "";
var length:int = format.length;
var next:*;
var str:String;
for (var i:int = 0; i < length; i++)
{
var c:String = format.charAt(i);
if (c == "%")
{
var pastFieldWidth:Boolean = false;
var pastFlags:Boolean = false;
var flagAlternateForm:Boolean = false;
var flagZeroPad:Boolean = false;
var flagLeftJustify:Boolean = false;
var flagSpace:Boolean = false;
var flagSign:Boolean = false;
var fieldWidth:String = "";
var precision:String = "";
c = format.charAt(++i);
while (c != "d"
&& c != "i"
&& c != "o"
&& c != "u"
&& c != "x"
&& c != "X"
&& c != "f"
&& c != "F"
&& c != "c"
&& c != "s"
&& c != "%")
{
if (!pastFlags)
{
if (!flagAlternateForm && c == "#")
flagAlternateForm = true;
else if (!flagZeroPad && c == "0")
flagZeroPad = true;
else if (!flagLeftJustify && c == "-")
flagLeftJustify = true;
else if (!flagSpace && c == " ")
flagSpace = true;
else if (!flagSign && c == "+")
flagSign = true;
else
pastFlags = true;
}
if (!pastFieldWidth && c == ".")
{
pastFlags = true;
pastFieldWidth = true;
c = format.charAt(++i);
continue;
}
if (pastFlags)
{
if (!pastFieldWidth)
fieldWidth += c;
else
precision += c;
}
c = format.charAt(++i);
}
switch (c)
{
case "d":
case "i":
next = args.shift();
str = String(Math.abs(int(next)));
if (precision != "")
str = leftPad(str, int(precision), "0");
if (int(next) < 0)
str = "-" + str;
else if (flagSign && int(next) >= 0)
str = "+" + str;
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "o":
next = args.shift();
str = uint(next).toString(8);
if (flagAlternateForm && str != "0")
str = "0" + str;
if (precision != "")
str = leftPad(str, int(precision), "0");
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "u":
next = args.shift();
str = uint(next).toString(10);
if (precision != "")
str = leftPad(str, int(precision), "0");
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "X":
var capitalise:Boolean = true;
case "x":
next = args.shift();
str = uint(next).toString(16);
if (precision != "")
str = leftPad(str, int(precision), "0");
var prepend:Boolean = flagAlternateForm && uint(next) != 0;
if (fieldWidth != "" && !flagLeftJustify
&& flagZeroPad && precision == "")
str = leftPad(str, prepend
? int(fieldWidth) - 2 : int(fieldWidth), "0");
if (prepend)
str = "0x" + str;
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
if (capitalise)
str = str.toUpperCase();
result += str;
break;
case "f":
case "F":
next = args.shift();
str = Math.abs(Number(next)).toFixed(
precision != "" ? int(precision) : 6);
if (int(next) < 0)
str = "-" + str;
else if (flagSign && int(next) >= 0)
str = "+" + str;
if (flagAlternateForm && str.indexOf(".") == -1)
str += ".";
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "c":
next = args.shift();
str = String.fromCharCode(int(next));
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "s":
next = args.shift();
str = String(next);
if (precision != "")
str = str.substring(0, int(precision));
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "%":
result += "%";
}
}
else
{
result += c;
}
}
return result;
}
// Private functions
function leftPad(source:String, targetLength:int, padChar:String = " "):String
{
if (source.length < targetLength)
{
var padding:String = "";
while (padding.length + source.length < targetLength)
padding += padChar;
return padding + source;
}
return source;
}
function rightPad(source:String, targetLength:int, padChar:String = " "):String
{
while (source.length < targetLength)
source += padChar;
return source;
}