1、导航按钮,使用 navigateToURL 函数。
btn.addEventListener(MouseEvent.CLICK,onCLICK);
function onCLICK(evt:MouseEvent){
navigateToURL(new URLRequest("http://dzxz.blog.5d.cn"),"_self") ;
}
2、拖拽,通过 startDrag , stopDrag 来实现。
cir1.addEventListener(MouseEvent.MOUSE_DOWN, onMD);
function onMD(evt) {
cir1.startDrag();
//cir1.addEventListener(MouseEvent.MOUSE_MOVE, onMV);
cir1.addEventListener(MouseEvent.MOUSE_UP, onMU);
}
//function onMV(evt) {
//do something here
//}
function onMU(evt) {
cir1.stopDrag();
//cir1.removeEventListener(MouseEvent.MOUSE_MOVE, onMV);
cir1.removeEventListener(MouseEvent.MOUSE_UP, onMU);
}
3、碰撞检测,通过 hitTestObject 实现,(尽管都知道碰撞检测的精度是不够用的,但有总比没有好,AS3里还留着)。
if ( cir1.hitTestObject(cir2) ) { // 两个圆之间的碰撞检测
//do something here
}
4、屏幕中运动的代码段落,碰到屏幕边界后就折返。
cir2.addEventListener(Event.ENTER_FRAME, onEF );
cir2.speed = 10;
cir2.direct = Math.random()*2*Math.PI;
cir2.vx = cir2.speed * Math.cos(cir2.direct);
cir2.vy = cir2.speed * Math.sin(cir2.direct);
cir2.radius = cir2.width/2;
function onEF(evt:Event) {
cir2.x += cir2.vx;
cir2.y += cir2.vy;
if ( cir2.x - cir2.radius < 0 ){
cir2.x = cir2.radius;
cir2.vx *= -1;
return;
}
if( cir2.x + cir2.radius > stage.stageWidth) {
cir2.x = stage.stageWidth - cir2.radius;
cir2.vx *= -1;
return;
}
if ( cir2.y - cir2.radius< 0 ){
cir2.y = cir2.radius;
cir2.vy *= -1;
return;
}
if( cir2.y + cir2.radius> stage.stageHeight) {
cir2.y = stage.stageHeight - cir2.radius;
cir2.vy *= -1;
return;
}
}
5、假如 小球2 的运动过程中,每一帧都与小球1 进行碰撞检测,将这部分内容用函数来实现,并且简化处理一下(反弹的角度并不符合物理实验效果),这样的代码合成到一起,实现的效果是:小球1 可以被拖动,小球2 会自动的运动,并且在发生碰撞时会改变方向,碰撞可能发生在墙壁、地板、天花板,还有和第二个小球之间。
function chgPsnBall() {
if ( cir1.hitTestObject(cir2) ) {
cir2.direct = Math.atan2( cir2.y -cir1.y , cir2.x-cir1.x);
cir2.x += ( cir1.width/2 )* Math.cos( cir2.direct );
cir2.y += ( cir1.width/2 ) * Math.sin( cir2.direct );
cir2.vx = cir2.speed * Math.cos(cir2.direct);
cir2.vy = cir2.speed * Math.sin(cir2.direct);
}
}
完成的效果如下,有点像是打砖块游戏的一个初级模型:
场景中建两个小球件元件,实例名为 cir1 和 cir2 ,完整的代码如下:
cir1.addEventListener(MouseEvent.MOUSE_DOWN, onMD);
function onMD(evt) {
cir1.startDrag();
cir1.addEventListener(MouseEvent.MOUSE_UP, onMU);
}
function onMU(evt) {
cir1.stopDrag();
cir1.removeEventListener(MouseEvent.MOUSE_UP, onMU);
}
function chgPsnBall() {
if ( cir1.hitTestObject(cir2) ) {
cir2.direct = Math.atan2( cir2.y -cir1.y , cir2.x-cir1.x);
cir2.x += ( cir1.width/2 )* Math.cos( cir2.direct );
cir2.y += ( cir1.width/2 ) * Math.sin( cir2.direct );
cir2.vx = cir2.speed * Math.cos(cir2.direct);
cir2.vy = cir2.speed * Math.sin(cir2.direct);
}
}
cir2.addEventListener(Event.ENTER_FRAME, onEF );
cir2.speed = 10;
cir2.direct = Math.random()*2*Math.PI;
cir2.vx = cir2.speed * Math.cos(cir2.direct);
cir2.vy = cir2.speed * Math.sin(cir2.direct);
cir2.radius = cir2.width/2;
function onEF(evt:Event) {
chgPsnBall();
cir2.x += cir2.vx;
cir2.y += cir2.vy;
if ( cir2.x - cir2.radius < 0 ) {
cir2.x = cir2.radius;
cir2.vx *= -1;
return;
}
if (cir2.x + cir2.radius > stage.stageWidth) {
cir2.x = stage.stageWidth - cir2.radius;
cir2.vx *= -1;
return;
}
if ( cir2.y - cir2.radius< 0 ) {
cir2.y = cir2.radius;
cir2.vy *= -1;
return;
}
if ( cir2.y + cir2.radius> stage.stageHeight) {
cir2.y = stage.stageHeight - cir2.radius;
cir2.vy *= -1;
return;
}
}
回复Comments
作者:
{commentrecontent}