code on 2008-12-18
package {
import flash.display.Sprite
public class Main extends Sprite {
public function Main() {
var w:Sprite = new Sprite()
w.graphics.beginFill(0xA7E6FC)
w.graphics.drawRect(200, 0, 200, 500)
w.graphics.endFill()
addChild(w)
var sfc:Sprite = new Surface()
sfc.x = 200
sfc.y = 0
addChild(sfc)
}
}
}
import flash.events.*
import flash.geom.Point
import flash.display.Sprite
import fl.transitions.*
import fl.transitions.easing.*
class Surface extends Sprite{
var p1,p2,c1,c2,center
var line:Sprite
const RELEASE_THRESHOLD = 150
const HEIGHT = 350
function Surface() {
p1 = new Point(0,-HEIGHT)
c1 = new Point(0,-50)
center = new Point(0,0)
c2 = new Point(0,50)
p2 = new Point(0, HEIGHT)
line = new Sprite()
addChild( line )
var handler = new Sprite()
handler.graphics.beginFill( 0xff0000, 0 )
handler.graphics.drawRect( p1.x-10, p1.y, 20, HEIGHT*2 )
handler.graphics.endFill()
addChild( handler )
handler.addEventListener( MouseEvent.MOUSE_OVER, catchLine )
addEventListener(Event.ENTER_FRAME, enterFrame)
}
function enterFrame(e:Event):void {
if ( onCatch ) {
center.y = this.mouseY
center.x = this.mouseX
}
if ( Math.abs(center.x) > RELEASE_THRESHOLD ) {
onCatch = false
releaseLine()
}
var color:uint = ( center.x <= 1 )? 0xA7E6FC:0xffffff
line.graphics.clear()
line.graphics.lineStyle(0, color)
line.graphics.beginFill( color )
line.graphics.moveTo( p1.x, p1.y )
line.graphics.curveTo( c1.x, c1.y, center.x, center.y )
line.graphics.curveTo( c2.x, c2.y, p2.x, p2.y )
line.graphics.lineTo( p1.x, p1.y )
line.graphics.endFill()
}
var onCatch,onTween = false
var catchx,catchy = 0
var twnx,twny
function catchLine(e:Event):void {
if ( onTween ) return
catchy = mouseY
catchx = 0
center.y = ( c1.y + c2.y ) / 2
var dy = catchy - center.y
p1.y += dy ; c1.y += dy; p2.y += dy; c2.y += dy; center.y += dy
onCatch = true
}
function releaseLine():void {
onTween = true
twnx = new Tween( center, "x", Elastic.easeOut, center.x, catchx, 20 )
twny = new Tween( center, "y", Elastic.easeOut, center.y, catchy, 20 )
twny.addEventListener( TweenEvent.MOTION_FINISH, afterReleaseLine )
}
function afterReleaseLine(e:Event):void {
onTween = false
twny.removeEventListener( TweenEvent.MOTION_FINISH, afterReleaseLine )
twny = null; twnx = null
}
}