flash as3 怎样让元件一开始向一个随机的方向,然后在这个方向上按照同样的速度移动 60
4个回答
展开全部
在影片剪辑的帧上加如下代码即可:
var speed:Number=5;//移动速度
this.rotation=360*Math.random();//随机移动方向
this.addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
var obj:Object=e.target;
obj.x+=Math.sin(obj.rotation)*speed;
obj.y+=Math.cos(obj.rotation)*speed;
}
var speed:Number=5;//移动速度
this.rotation=360*Math.random();//随机移动方向
this.addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
var obj:Object=e.target;
obj.x+=Math.sin(obj.rotation)*speed;
obj.y+=Math.cos(obj.rotation)*speed;
}
追问
那碰到边界反弹,还是一样的速度,怎么做?
追答
看看这个效果吧。。。
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
创建一个元件放在舞台中,起实例名为mc
speedx = -(random(10))+5;
speedy = -(random(10))+5;
onEnterFrame = function () {
speed = -(random(10))+5;
trace(speed);
mc._x += speedx;
mc._y += speedy;
};
speedx = -(random(10))+5;
speedy = -(random(10))+5;
onEnterFrame = function () {
speed = -(random(10))+5;
trace(speed);
mc._x += speedx;
mc._y += speedy;
};
更多追问追答
追问
speed是干什么用的?
追答
没用,刚才用来测试的,4,5行都可以删除掉不要
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//命名一个影片剪辑实例名为xiaoqiu
var speed=10;//初速度
var zbj = 0;
var ybj =stage.stageWidth;
var sbj = 0;
var xbj =stage.stageHeight
var vx=(Math.random()-Math.random())*speed//小球x轴速度方向
var vy = (Math.random()-Math.random())*speed//小球y轴速度方向
addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
xiaoqiu.x += vx;
xiaoqiu.y += vy;
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj || xiaoqiu.x + xiaoqiu.width / 2 >= ybj)
{
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj)
{
xiaoqiu.x = 0 + xiaoqiu.width / 2;
vx *= -1;
}
else
{
xiaoqiu.x = ybj - xiaoqiu.width / 2;
vx *= -1;
}
}//x轴边界判断
else if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj || xiaoqiu.y + xiaoqiu.height / 2 >= xbj)
{
if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj)
{
xiaoqiu.y = 0 + xiaoqiu.height / 2;
vy *= -1;
}
else
{
xiaoqiu.y = xbj - xiaoqiu.height / 2;
vy *= -1;
}
}//y轴边界判断
}
/**
*以下是小球运动中的物理现象
*
*
*
*
*
*
**/
//命名一个影片剪辑实例名为xiaoqiu
var speed=10;//初速度
var zbj = 0;
var ybj =stage.stageWidth;
var sbj = 0;
var xbj =stage.stageHeight
var vx=(Math.random()-Math.random())*speed//小球x轴速度方向
var vy = (Math.random()-Math.random())*speed//小球y轴速度方向
addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
vy+=0.1
xiaoqiu.x += vx;
xiaoqiu.y += vy;
trace(vx)
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj || xiaoqiu.x + xiaoqiu.width / 2 >= ybj)
{
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj)
{
xiaoqiu.x = 0 + xiaoqiu.width / 2;
vx *= -1
}
else
{
xiaoqiu.x = ybj - xiaoqiu.width / 2;
vx *= -1;
}
}//x轴边界判断
else if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj || xiaoqiu.y + xiaoqiu.height / 2 >= xbj)
{
if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj)
{
xiaoqiu.y = 0 + xiaoqiu.height / 2;
vy *= -1;
}
else
{
xiaoqiu.y = xbj - xiaoqiu.height / 2;
vy *= -0.54;
vx*=0.78
if(vx*vx-0.000000000001<=0)
{ vy=0;
vx=0
removeEventListener(Event.ENTER_FRAME,MoveEvent);
}
}
}//y轴边界判断
}
//不懂的可以提问在线解答
var speed=10;//初速度
var zbj = 0;
var ybj =stage.stageWidth;
var sbj = 0;
var xbj =stage.stageHeight
var vx=(Math.random()-Math.random())*speed//小球x轴速度方向
var vy = (Math.random()-Math.random())*speed//小球y轴速度方向
addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
xiaoqiu.x += vx;
xiaoqiu.y += vy;
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj || xiaoqiu.x + xiaoqiu.width / 2 >= ybj)
{
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj)
{
xiaoqiu.x = 0 + xiaoqiu.width / 2;
vx *= -1;
}
else
{
xiaoqiu.x = ybj - xiaoqiu.width / 2;
vx *= -1;
}
}//x轴边界判断
else if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj || xiaoqiu.y + xiaoqiu.height / 2 >= xbj)
{
if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj)
{
xiaoqiu.y = 0 + xiaoqiu.height / 2;
vy *= -1;
}
else
{
xiaoqiu.y = xbj - xiaoqiu.height / 2;
vy *= -1;
}
}//y轴边界判断
}
/**
*以下是小球运动中的物理现象
*
*
*
*
*
*
**/
//命名一个影片剪辑实例名为xiaoqiu
var speed=10;//初速度
var zbj = 0;
var ybj =stage.stageWidth;
var sbj = 0;
var xbj =stage.stageHeight
var vx=(Math.random()-Math.random())*speed//小球x轴速度方向
var vy = (Math.random()-Math.random())*speed//小球y轴速度方向
addEventListener(Event.ENTER_FRAME,MoveEvent);
function MoveEvent(e:Event):void
{
vy+=0.1
xiaoqiu.x += vx;
xiaoqiu.y += vy;
trace(vx)
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj || xiaoqiu.x + xiaoqiu.width / 2 >= ybj)
{
if (xiaoqiu.x - xiaoqiu.width / 2 <= zbj)
{
xiaoqiu.x = 0 + xiaoqiu.width / 2;
vx *= -1
}
else
{
xiaoqiu.x = ybj - xiaoqiu.width / 2;
vx *= -1;
}
}//x轴边界判断
else if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj || xiaoqiu.y + xiaoqiu.height / 2 >= xbj)
{
if (xiaoqiu.y - xiaoqiu.height / 2 <= sbj)
{
xiaoqiu.y = 0 + xiaoqiu.height / 2;
vy *= -1;
}
else
{
xiaoqiu.y = xbj - xiaoqiu.height / 2;
vy *= -0.54;
vx*=0.78
if(vx*vx-0.000000000001<=0)
{ vy=0;
vx=0
removeEventListener(Event.ENTER_FRAME,MoveEvent);
}
}
}//y轴边界判断
}
//不懂的可以提问在线解答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要写AS代码的!我忘记怎么写了!上网搜一下应该有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询