as3 as3.0问题 让多个物体运动怎么写代码?
我写的代码如下,但只能让一个物体运动,我目的是所有物体都一样运动,希望高手改一下,谢谢!改完希望测试一下,测试成功即采纳!constn=10;constg=.1;vara...
我写的代码如下,但只能让一个物体运动,我目的是所有物体都一样运动,希望高手改一下,谢谢!
改完希望测试一下,测试成功即采纳!
const n=10;
const g=.1;
var arr:Array=[];
var vx:Array=[];
var vy:Array=[];
for (var i:int = 0; i<n; i++) {
arr[i]=new Sprite();
var na=arr[i];
addChild(na);
na.graphics.beginFill(0xffffff*Math.random());
na.graphics.drawCircle(0,0,Math.random()*50+10);
na.x=Math.random()*550,na.y=Math.random()*200;
//trace(Number(na.name.substr(8)));
vy[i]=0;
vx[i]=0;
}
var fx:Number=.95;
var fy:Number=.9;
stage.addEventListener(Event.ENTER_FRAME,fdo);
function fdo(e:Event):void {
//for (i = 0; i<n; i++) {
na=arr[1];
na.addEventListener(MouseEvent.ROLL_OVER,roll);
function roll(event:MouseEvent):void {
var chan=1;
vy[1]=-3;
vx[1] = (na.x-mouseX)/3;
}
vy[1]+=g;
na.y+=vy[1];
//trace(vy[i]);
na.x+=vx[1];
if (na.y>400-na.width/2) {
na.y=400-na.width/2;
vy[1]=- vy[1]*fy;
vx[1]=vx[1]*fx;
}
if (na.x>550-na.width/2) {
na.x=550-na.width/2;
vx[1]=- vx[1];
}
if (na.x<na.width/2) {
na.x=na.width/2;
vx[1]=- vx[1];
}
} 展开
改完希望测试一下,测试成功即采纳!
const n=10;
const g=.1;
var arr:Array=[];
var vx:Array=[];
var vy:Array=[];
for (var i:int = 0; i<n; i++) {
arr[i]=new Sprite();
var na=arr[i];
addChild(na);
na.graphics.beginFill(0xffffff*Math.random());
na.graphics.drawCircle(0,0,Math.random()*50+10);
na.x=Math.random()*550,na.y=Math.random()*200;
//trace(Number(na.name.substr(8)));
vy[i]=0;
vx[i]=0;
}
var fx:Number=.95;
var fy:Number=.9;
stage.addEventListener(Event.ENTER_FRAME,fdo);
function fdo(e:Event):void {
//for (i = 0; i<n; i++) {
na=arr[1];
na.addEventListener(MouseEvent.ROLL_OVER,roll);
function roll(event:MouseEvent):void {
var chan=1;
vy[1]=-3;
vx[1] = (na.x-mouseX)/3;
}
vy[1]+=g;
na.y+=vy[1];
//trace(vy[i]);
na.x+=vx[1];
if (na.y>400-na.width/2) {
na.y=400-na.width/2;
vy[1]=- vy[1]*fy;
vx[1]=vx[1]*fx;
}
if (na.x>550-na.width/2) {
na.x=550-na.width/2;
vx[1]=- vx[1];
}
if (na.x<na.width/2) {
na.x=na.width/2;
vx[1]=- vx[1];
}
} 展开
3个回答
展开全部
const n = 10;
const g = .1;
var arr:Array = [];
var vx:Array = [];
var vy:Array = [];
for (var i:int = 0; i<n; i++) {
arr[i]=new Sprite();
var na = arr[i];
addChild(na);
na.graphics.beginFill(0xffffff*Math.random());
na.graphics.drawCircle(0,0,Math.random()*50+10);
na.x = Math.random() * 550;
na.y = Math.random() * 200;
na.name=String(i);
//trace(Number(na.name.substr(8)));
vy[i] = 0;
vx[i] = 0;
na.addEventListener(MouseEvent.ROLL_OVER,roll);
}
var fx:Number = .95;
var fy:Number = .9;
stage.addEventListener(Event.ENTER_FRAME,fdo);
function fdo(e:Event):void {
for (i = 0; i<n; i++) {
na = arr[i];
vy[i] += g;
na.y += vy[i];
na.x += vx[i];
if (na.y > 400 - na.width / 2) {
na.y = 400 - na.width / 2;
vy[i] = - vy[1] * fy;
vx[i] = vx[i] * fx;
}
if (na.x > 550 - na.width / 2) {
na.x = 550 - na.width / 2;
vx[i] = - vx[i];
}
if (na.x < na.width / 2) {
na.x = na.width / 2;
vx[i] = - vx[i];
}
}
}
function roll(event:MouseEvent):void {
var ba=event.currentTarget;
var i=int(ba.name);
vy[i] = -3;
vx[i] = (na.x-mouseX)/3;
}
//注意逻辑
展开全部
把运动的方法封装到函数中 之后在轮询事件中调用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
const n = 10;
const g = .1;
var arr:Array = [];
var vx:Array = [];
var vy:Array = [];
for (var i:int = 0; i<n; i++)
{
arr[i]=new Sprite();
addChild( arr[i]);
arr[i].graphics.beginFill(0xffffff*Math.random());
arr[i].graphics.drawCircle(0,0,Math.random()*50+10);
arr[i].x = Math.random() * 550, arr[i].y = Math.random() * 200;
//trace(Number(na.name.substr(8)));
vy[i] = 0;
vx[i] = 0;
}
var fx:Number = .95;
var fy:Number = .9;
stage.addEventListener(Event.ENTER_FRAME,fdo);
function fdo(e:Event):void
{
for (i = 0; i<n; i++) {
var na = arr[i];
na.addEventListener(MouseEvent.ROLL_OVER,roll);
function roll(event:MouseEvent):void
{
var chan = 1;
vy[i] = -3;
vx[i] = (na.x-mouseX)/3;
}
vy[i] += g;
na.y += vy[i];
//trace(vy[i]);
na.x += vx[i];
if (na.y > 400 - na.width / 2)
{
na.y = 400 - na.width / 2;
vy[i] = - vy[i] * fy;
vx[i] = vx[i] * fx;
}
if (na.x > 550 - na.width / 2)
{
na.x = 550 - na.width / 2;
vx[i] = - vx[i];
}
if (na.x < na.width / 2)
{
na.x = na.width / 2;
vx[i] = - vx[i];
}
}
}
const g = .1;
var arr:Array = [];
var vx:Array = [];
var vy:Array = [];
for (var i:int = 0; i<n; i++)
{
arr[i]=new Sprite();
addChild( arr[i]);
arr[i].graphics.beginFill(0xffffff*Math.random());
arr[i].graphics.drawCircle(0,0,Math.random()*50+10);
arr[i].x = Math.random() * 550, arr[i].y = Math.random() * 200;
//trace(Number(na.name.substr(8)));
vy[i] = 0;
vx[i] = 0;
}
var fx:Number = .95;
var fy:Number = .9;
stage.addEventListener(Event.ENTER_FRAME,fdo);
function fdo(e:Event):void
{
for (i = 0; i<n; i++) {
var na = arr[i];
na.addEventListener(MouseEvent.ROLL_OVER,roll);
function roll(event:MouseEvent):void
{
var chan = 1;
vy[i] = -3;
vx[i] = (na.x-mouseX)/3;
}
vy[i] += g;
na.y += vy[i];
//trace(vy[i]);
na.x += vx[i];
if (na.y > 400 - na.width / 2)
{
na.y = 400 - na.width / 2;
vy[i] = - vy[i] * fy;
vx[i] = vx[i] * fx;
}
if (na.x > 550 - na.width / 2)
{
na.x = 550 - na.width / 2;
vx[i] = - vx[i];
}
if (na.x < na.width / 2)
{
na.x = na.width / 2;
vx[i] = - vx[i];
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询