/******************************************************************************/
/** @file FlashExample9_14.as
@brief FlashExample9_14 class
@note Copyright (C) 2010 ThinkinGall. All rights reserved.
@author xuyeding
@date 2010/11/29
@version 1.00
*******************************************************************************/
package {
import flash.display.Bitmap;
import flash.display.GradientType;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.ui.Keyboard;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.net.URLRequest;
/**
* FlashExample9_14 class example
* @class FlashExample9_14
*/
public class FlashExample9_14 extends MovieClip {
private var fieldx:int = 0;
private var fieldy:int = 0;
private var targetx:int = 0;
private var targety:int = 0;
private var friction:Number = 0.5;
private var gravity:Number = 0.5;
private var bounce:Number = -0.5;
private var spring:Number = 0.05;
private var radian:Number = 0;
private var vr:Number = 0;
private var sin:Number = 0;
private var cos:Number = 0;
/** instance */
private var line:Line = new Line();
private var box:Box = new Box();
private var arrow:Arrow = new Arrow();
private var ball:Ball = new Ball();
private var ballOne:Ball = new Ball();
private var ballTwo:Ball = new Ball();
private var ballOneFlag:Boolean = false;
private var ballTwoFlag:Boolean = false;
private var springLength:Number = 100;
/* Array Ball */
[ArrayElementType("Ball")]
private var arrBall:Array = [];
[ArrayElementType("Box")]
private var arrBox:Array = [];
public function FlashExample9_14() {
/* -------ninth chapter-start------*/
//hitTestObjectBox1();
//bubbles();
/* -------ninth chapter-end------*/
/* -------tenth chapter-start------*/
//simpleRotate();
//complicateRotate();
anglebounce();
/* -------tenth chapter-end------*/
}
/*--------TEST--START----------*/
public function anglebounce():void {
line = new Line(30, 500);
line.x = 50;
line.y = 150;
addChild(line);
ball.x = 100;
ball.y = 100;
gravity = 3;
bounce = -1;
addChild(ball);
addEventListener(Event.ENTER_FRAME, anglebounceHandler, false, 0, true);
}
/**
* anglebounceHandler ENTER_FRAME
*/
public function anglebounceHandler(event:Event):void {
line.rotation = Math.atan2(mouseY - line.y, line.length) * 180 / Math.PI;
radian = line.rotation * Math.PI / 180;
cos = Math.cos(radian);
sin = Math.sin(radian);
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
/* wall's hit test */
moveBall(ball);
/* hit test method 1 */
//if (!ball.hitTestObject(line)) {
//return;
//}
/* hit test method 2 */
var bounds:Rectangle = line.getBounds(this);
if (ball.x < bounds.left || ball.x > bounds.right) {
return;
}
var x1:Number = ball.x - line.x;
var y1:Number = ball.y - line.y;
//rotation coordinator
var y2:Number = cos * y1 - sin * x1;
var vy1:Number = cos * ball.vy - sin * ball.vx;
/* line's hit test */
if (y2 > -ball.height / 2 && y2 < vy1) {
var x2:Number = cos * x1 + sin * y1;
//rotation valocity
var vx1:Number = cos * ball.vx + sin * ball.vy;
y2 = -ball.height / 2;
vy1 *= bounce;
// reback rotation coordinator
x1 = cos * x2 - sin * y2;
y1 = cos * y2 + sin * x2;
// reback rotation coordinator
ball.vx = cos * vx1 - sin * vy1;
ball.vy = cos * vy1 + sin * vx1;
// ball's position
ball.x = line.x + x1;
ball.y = line.y + y1;
}
}
/*--------TEST--START----------*/
public function complicateRotate():void {
ball.vr = 1;
ball.x = Math.random() * stage.stageWidth ;
ball.y = Math.random() * stage.stageHeight ;
addChild(ball);
addEventListener(Event.ENTER_FRAME, complicateRotateHandler, false, 0, true);
}
/**
* complicateRotateHandler ENTER_FRAME
*/
public function complicateRotateHandler(event:Event):void {
var tmpx1:Number = ball.x - stage.stageWidth / 2;
var tmpy1:Number = ball.y - stage.stageHeight / 2;
var tmpx2:Number = Math.cos(ball.vr) * tmpx1 - Math.sin(ball.vr) * tmpy1;
var tmpy2:Number = Math.cos(ball.vr) * tmpy1 + Math.sin(ball.vr) * tmpx1;
ball.x = stage.stageWidth / 2 + tmpx2;
ball.y = stage.stageHeight / 2 + tmpy2;
onDrawLine(ball.x, ball.y, stage.stageWidth / 2, stage.stageHeight / 2);
}
/*--------TEST--START----------*/
/**
* simpleRotate
*/
public function simpleRotate():void {
ball.vr = 0.2;
addChild(ball);
addEventListener(Event.ENTER_FRAME, simpleRotateHandler, false, 0, true);
}
/**
* simpleRotate ENTER_FRAME
*/
public function simpleRotateHandler(event:Event):void {
ball.x = stage.stageWidth / 2 + Math.cos(radian) * (100 + ball.radius);
ball.y = stage.stageHeight / 2 + Math.sin(radian) * (100 + ball.radius);
radian += ball.vr;
onDrawLine(ball.x, ball.y, stage.stageWidth / 2, stage.stageHeight / 2);
}
/*--------TEST--START----------*/
/**
* bubbles
*/
public function bubbles():void {
ball = new Ball(50, 0xCCCCCC);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
bounce = -0.9;
spring = 0.8;
addChild(ball);
randomCreateBalls(10);
addEventListener(Event.ENTER_FRAME, bubblesHandler, false, 0, true);
}
/**
* bubblesHandler ENTER_FRAME
*/
public function bubblesHandler(event:Event):void {
singleBubbleHit(ball);
//multiBubbleHit();
}
/**
* multiBubbleHit
*/
public function multiBubbleHit():void {
for (var i:int = 0; i < arrBall.length - 1; i++) {
ballOne = arrBall[i];
for (var j:int = 0; j < arrBall.length; j++) {
ballTwo = arrBall[j];
ballTwo.x += ballTwo.vx;
ballTwo.y += ballTwo.vy;
moveBall(ballTwo);
var dist:Number = getAFromB(ballTwo.x, ballOne.x, ballTwo.y, ballOne.y);
var minDist:Number = ballOne.radius + ballTwo.radius;
// hitTest
if (dist < minDist) {
radian = Math.atan2(ballTwo.y - ballOne.y, ballTwo.x - ballOne.x);
targetx = ballOne.x + Math.cos(radian) * minDist;
targety = ballOne.y + Math.sin(radian) * minDist;
ballTwo.ax = (targetx - ballTwo.x) * spring;
ballTwo.ay = (targety - ballTwo.y) * spring;
ballTwo.vx += ballTwo.ax;
ballTwo.vy += ballTwo.ay;
ballOne.vx -= ballTwo.ax;
ballOne.vy -= ballTwo.ay;
}
}
}
}
/**
* singleBubbleHit
*/
public function singleBubbleHit(_ball:Ball):void {
for (var i:int = 0; i < arrBall.length; i++) {
var tmpBall:Ball = arrBall[i];
tmpBall.x += tmpBall.vx;
tmpBall.y += tmpBall.vy;
moveBall(tmpBall);
var dist:Number = getAFromB(tmpBall.x, ball.x, tmpBall.y, ball.y);
var minDist:Number = _ball.radius + tmpBall.radius;
// hitTest
if (dist < minDist) {
radian = Math.atan2(tmpBall.y - _ball.y, tmpBall.x - _ball.x);
targetx = _ball.x + Math.cos(radian) * minDist;
targety = _ball.y + Math.sin(radian) * minDist;
tmpBall.vx += (targetx - tmpBall.x) * spring;
tmpBall.vy += (targety - tmpBall.y) * spring;
}
}
}
/**
* bubblesHandler
*/
public function moveBall(_ball:Ball):void {
if (_ball.x + _ball.radius >= stage.stageWidth) {
_ball.x = stage.stageWidth - _ball.radius;
_ball.vx *= bounce;
}else if (_ball.x - _ball.radius <= 0) {
_ball.x = _ball.radius;
_ball.vx *= bounce;
}
if (_ball.y + _ball.radius >= stage.stageHeight) {
_ball.y = stage.stageHeight - _ball.radius;
_ball.vy *= bounce;
}else if (_ball.y - _ball.radius <= 0) {
_ball.y = _ball.radius;
_ball.vy *= bounce;
}
}
/*--------------------------------------------*/
/**
* hitTestObjectBox
*/
public function hitTestObjectBox1():void {
createBox();
addEventListener(Event.ENTER_FRAME, hitTestObjectBoxHandler, false, 0, true);
}
/**
* doubleSpring ENTER_FRAME
*/
public function hitTestObjectBoxHandler(event:Event):void {
box.vy += gravity;
box.y += box.vy;
if (box.y + box.height / 2 > stage.stageHeight) {
box.y = stage.stageHeight - box.height / 2;
createBox();
}
for (var i:int = 0; i < arrBox.length; i++) {
if (box != arrBox[i] && box.hitTestPoint(mouseX, mouseY, true)) {
box.y = 100;
createBox();
}
//if (box != arrBox[i] && box.hitTestObject(arrBox[i])) {
//box.y = arrBox[i].y - arrBox[i].height / 2 - box.height / 2;
//createBox();
//}
}
}
public function createBox():void {
box = new Box(Math.random() * 40 + 10, Math.random() * 40 + 10, Math.random() * 0x00FF00);
box.x = Math.random() * stage.stageWidth;
addChild(box);
arrBox.push(box);
}
/*-------common-------*/
public function getAFromB(x1:Number, x2:Number, y1:Number,y2:Number):Number {
return Math.sqrt((x1 - x2) *(x1 - x2) + (y1 - y2) *(y1 - y2));
}
/**
* draw line
*/
public function onDrawLine(x1:Number, y1:Number, x2:Number,y2:Number):void {
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(x1, y1);
graphics.lineTo(x2, y2);
}
/**
* create ball
* @param num
*/
public function randomCreateBalls(num:int = 10, random:Number = 40, offSet:Number = 5):void {
for (var i:int = 0; i < num; i++) {
var _ball:Ball = new Ball(Math.random() * random + offSet, Math.random() * 0xFFFFFF);
_ball.x = Math.random() * stage.stageWidth;
_ball.y = Math.random() * stage.stageHeight;
_ball.vx = Math.random() * 6 - 3;
_ball.vy = Math.random() * 6 - 3;
addChild(_ball);
arrBall.push(_ball);
}
}
/*-------common-------*/
}
}
Flash.ActionScript.3.0动画教程_code3
最新推荐文章于 2020-11-30 00:45:23 发布