Actionscript 3 添加一个可由阵列中的每个精灵单独使用的函数

Actionscript 3 添加一个可由阵列中的每个精灵单独使用的函数,actionscript-3,actionscript,Actionscript 3,Actionscript,我需要通过点击来移动球,但它们需要单独移动,我还需要使用存储精灵的数组。我已经搜索了几个小时,但没有找到答案,这可能是很明显的,但我有点累了 import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * ... * @author Sam */ public class Main extends Sprite { public var array:Arr

我需要通过点击来移动球,但它们需要单独移动,我还需要使用存储精灵的数组。我已经搜索了几个小时,但没有找到答案,这可能是很明显的,但我有点累了

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

/**
 * ...
 * @author Sam
 */
public class Main extends Sprite 
{
    public var array:Array = new Array();
    public var bal:Sprite = new Sprite();

    public function Main():void 
    {
    for (var i:int = 0; i < 501; i++)
        {
            bal.graphics.beginFill(0x00FFFF);
            bal.graphics.drawCircle(600 * Math.random(), 800*Math.random(), 10);
            bal.graphics.endFill();

            array.push(bal);
            array[i].addEventListener(MouseEvent.CLICK, gaweg);
            addChild(bal);
        }
    }

    public function gaweg(e:MouseEvent):void
    {
        x += 3;
    }
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.MouseEvent;
/**
* ...
*@作者山姆
*/
公共类Main扩展了Sprite
{
公共变量数组:数组=新数组();
公共变量bal:Sprite=新Sprite();
公共函数Main():void
{
对于(变量i:int=0;i<501;i++)
{
bal.graphics.beginll(0x00FFFF);
bal.graphics.drawCircle(600*Math.random(),800*Math.random(),10);
bal.graphics.endFill();
阵列推送(bal);
数组[i].addEventListener(MouseEvent.CLICK,gaweg);
addChild(bal);
}
}
公共函数gaweg(e:MouseEvent):无效
{
x+=3;
}

是我目前使用的代码,我显然是一个初学者,“gaweg”是用来移动球的函数,我称精灵为“bal”。

这个问题应该使用OOP解决,即使用
gaweg
函数制作
ball
对象。球还应该负责绘制自身

public class Ball extends Sprite
{
    public function Ball()
    {
        graphics.beginFill(0x00FFFF);
        graphics.drawCircle(600 * Math.random(), 800 * Math.random(), 10);
        graphics.endFill();

        addEventListener(MouseEvent.CLICK, _gaweg);
    }

    private function _gaweg(e:MouseEvent):void
    {
        x += 3;
    }
}
通过这种方式,
Ball
是一个独立的对象,它可以绘制自身并具有自己的单击处理程序。从这里,您可以将当前代码简化为:

public class Main extends Sprite
{
    public var balls:Array = [];

    public function Main()
    {
        for(var i:int = 0; i < 501; i++)
        {
            var ball:Ball = new Ball();
            balls.push(ball);
            addChild(ball);
        }
    }
}
public类主扩展Sprite
{
公共变量:数组=[];
公共功能Main()
{
对于(变量i:int=0;i<501;i++)
{
变量ball:ball=新的ball();
推(球);
addChild(ball);
}
}
}

很抱歉我的回答迟了,但非常感谢,它成功了!不过我自己应该知道的。祝你有愉快的一天。