【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
转载:9RIA游戏开发者社区(天地会)
作者:沐枫(原天地会大神)
【基础小知识】系列教程(总目录)——沐枫大神出品
介绍:本小节主要是关于数学中三角函数公式的一些应用,在日常做游戏中你会发现它非常重要。比如在学到碰撞检测时,我们会发现内置的碰撞检测(hit testing)方法存在着先天不足,然后会看到使用勾股定理公式完成基于距离(distance-based)碰撞检测方法。它还非常适合用于计算重力或弹力等,因为这些力的大小与两个物体之间的距离成正比。
反正切实现箭头跟随鼠标旋转
[Actionscript3] 纯文本查看 复制代码 import flash.display.Sprite;
import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
function onEnterFrameHandler(e:Event):void
{
var dx:Number = mouseX - mc.x;
var dy:Number = mouseY - mc.y;
var radians:Number = Math.atan2(dy,dx);//求出箭头起点与鼠标直接连线与水平线的夹角弧度。
mc.rotation = radians * 180 / Math.PI;
}
平滑的上下运动
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
var angle:Number=0;
stage.addEventListener(Event.ENTER_FRAME,onENTER_FRAMEHandler);
function onENTER_FRAMEHandler(e:Event):void
{
mc.y=stage.stageHeight/2+Math.sin(angle)*100;
angle+=0.1;
}
线性垂直运动和绘制波形
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
var angle:Number=0;
var sp:Sprite=new Sprite();
sp.graphics.lineStyle(1,0x000000,0.5);
sp.graphics.moveTo(mc.x,200);
stage.addEventListener(Event.ENTER_FRAME,onENTER_FRAMEHandler);
function onENTER_FRAMEHandler(e:Event):void
{
if(mc.x>550-33)//舞台默认宽550;
{
mc.x=16;
angle=0;
}
else
{
mc.x+=2;
}
mc.y=stage.stageHeight/2-Math.sin(angle)*100;
angle+=0.05;
sp.graphics.lineTo(mc.x,mc.y);
stage.addChild(sp);
}
心跳运动
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
var angle:Number=0;
stage.addEventListener(Event.ENTER_FRAME,onENTER_FRAMEHandler);
function onENTER_FRAMEHandler(e:Event):void
{
mc.scaleX=1+Math.sin(angle)*0.5;
mc.scaleY=1+Math.sin(angle)*0.5;
angle+=0.1;
}
双角波形
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
import flash.display.Sprite;
var angle:Number=0;
var angle2:Number=0;
var sp:Sprite=new Sprite();
sp.graphics.lineStyle(1,0x000000,0.5);
sp.graphics.moveTo(mc.x,200);
stage.addEventListener(Event.ENTER_FRAME,onENTER_FRAMEHandler);
function onENTER_FRAMEHandler(e:Event):void
{
sp.graphics.lineTo(mc.x,mc.y);
mc.x=275+Math.sin(angle)*50;
mc.y=200+Math.sin(angle2)*50;
angle+=0.05;
angle2+=0.11;
stage.addChild(sp);
}
圆形运动
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
var angle:Number=0;
var sp:Sprite=new Sprite();
sp.graphics.lineStyle(1,0x000000,0.5);
sp.graphics.moveTo(mc.x,200);
stage.addEventListener(Event.ENTER_FRAME,onENTER_FRAMEHandler);
function onENTER_FRAMEHandler(e:Event):void
{
mc.x=275+Math.cos(angle)*100;
mc.y=200+Math.sin(angle)*100;
//mc.y=200+Math.sin(angle)*150;//画椭圆
angle+=0.05;
sp.graphics.lineTo(mc.x,mc.y);
stage.addChild(sp);
}
两点间距离
[Actionscript3] 纯文本查看 复制代码 import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
var sprite1:Sprite;
var textField:TextField;
sprite1 = new Sprite();
addChild(sprite1);
sprite1.graphics.beginFill(0x000000);
sprite1.graphics.drawRect(-2, -2, 4, 4);
sprite1.graphics.endFill();
sprite1.x = stage.stageWidth / 2;
sprite1.y = stage.stageHeight / 2;
textField = new TextField();
addChild(textField);
var textF:TextFormat = new TextFormat();
textF.color = 0xff0000;
textF.size = 35;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
function onMouseMoveHandler(event:MouseEvent):void
{
graphics.clear();
graphics.lineStyle(1, 0, 1);
graphics.moveTo(sprite1.x, sprite1.y);
graphics.lineTo(mouseX, mouseY);
var dx:Number = sprite1.x - mouseX;
var dy:Number = sprite1.y - mouseY;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
textField.text = dist.toString();
textField.setTextFormat(textF);
}
下载:
源文件下载.rar
(47.09 KB, 下载次数: 6)
|