flash as3.0 用代码做一个小球在400*300的界面四处碰撞的动画,怎么做? 就和以前的一个泡泡屏保类似的
2个回答
展开全部
这个脚本实现了一个小球的上述效果
import flash.display.Shape;import flash.events.Event;
var ball:Shape=new Shape();with (ball.graphics)
{
beginFill(0xFF2200,1);
drawCircle(0,0,10);
endFill();
}
addChild(ball);
//以上绘制了一个半径为10的红色小球
var speed:Number = 8;
var dx:int = 1;
var dy:int = 1;
var r:Number = 30;
var s:Number = Math.PI / 180;
var stageW:Number = stage.stageWidth,stageH = stage.stageHeight;
addEventListener(Event.ENTER_FRAME,onEnter);
function onEnter(evt:Event):void
{
ball.x+=dx*speed*Math.cos(r*s);
ball.y+=dy*speed*Math.sin(r*s);
if (ball.x >= stageW - ball.width / 2)
{
ball.x = stageW - ball.width / 2;
dx *= -1;
}
if (ball.x <= ball.width / 2)
{
ball.x = ball.width / 2;
dx *= -1;
}
if (ball.y >= stageH - ball.height / 2)
{
ball.y = stageH - ball.height / 2;
dy *= -1;
}
if (ball.y <= ball.height / 2)
{
ball.y = ball.height / 2;
dy *= -1;
}
}
import flash.display.Shape;import flash.events.Event;
var ball:Shape=new Shape();with (ball.graphics)
{
beginFill(0xFF2200,1);
drawCircle(0,0,10);
endFill();
}
addChild(ball);
//以上绘制了一个半径为10的红色小球
var speed:Number = 8;
var dx:int = 1;
var dy:int = 1;
var r:Number = 30;
var s:Number = Math.PI / 180;
var stageW:Number = stage.stageWidth,stageH = stage.stageHeight;
addEventListener(Event.ENTER_FRAME,onEnter);
function onEnter(evt:Event):void
{
ball.x+=dx*speed*Math.cos(r*s);
ball.y+=dy*speed*Math.sin(r*s);
if (ball.x >= stageW - ball.width / 2)
{
ball.x = stageW - ball.width / 2;
dx *= -1;
}
if (ball.x <= ball.width / 2)
{
ball.x = ball.width / 2;
dx *= -1;
}
if (ball.y >= stageH - ball.height / 2)
{
ball.y = stageH - ball.height / 2;
dy *= -1;
}
if (ball.y <= ball.height / 2)
{
ball.y = ball.height / 2;
dy *= -1;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询