【问题标题】:Actionscript 3.0 How do i define objects from addChild?Actionscript 3.0 如何从 addChild 定义对象?
【发布时间】:2015-12-27 01:38:28
【问题描述】:

所以基本上我想让一个男孩吃掉一些使用 addChild 生成的蘑菇。我为它做了一个热门测试。但我有一个未定义属性的错误 1120。我怎么能解决这个问题?任何帮助将不胜感激。

这是我的代码。

    var timer:Timer = new Timer(1000,10);
timer.addEventListener(TimerEvent.TIMER, addTarget);
timer.start();

var score:int = 0;

function addTarget(e:TimerEvent)
{
    var posX = Math.random()*860;
    var posY = Math.random()*500;


    var mushroom:Mushroom = new Mushroom();
    addChild(mushroom);


    mushroom.x = posX;
    mushroom.y = posY;
    boy_mc.addEventListener(Event.ENTER_FRAME, scoring);
}


function scoring(e:Event)
{
    trace("test");
    if (boy_mc.hitTestObject(mushroom))

    {
    score = score + (MovieClip(e.currentTarget).point);

    score_txt.text = String(score);
    }
}

----------------这是我的类文件----------

package  
{
    import flash.display.MovieClip;

    public class Mushroom extends MovieClip
    {
        private var size:Number;


        public var point:int;


        public var mushroom:int;

        public function Mushroom() 
        {


            // constructor code
            size = (Math.random()*100)+20;
            this.width = size;
            this.height = size;

            point = Math.random()*10;
        }

    }

}

【问题讨论】:

  • error :is on this " if (boy_mc.hitTestObject(mushroom))"未定义属性蘑菇的访问。
  • 为了完成,您应该通过编辑将错误消息添加到问题本身

标签: actionscript-3 flash actionscript adobe


【解决方案1】:

您需要蘑菇参考,例如 Array()Vector.<Mushroom>()。然后,访问for 中的引用。

var mushrooms:Vector.<Mushrooms> = new Vector.<Mushrooms>();

var timer:Timer = new Timer(1000,10);
timer.addEventListener(TimerEvent.TIMER, addTarget);
timer.start();

var score:int = 0;

function addTarget(e:TimerEvent) {

    var posX = Math.random()*860;
    var posY = Math.random()*500;

    var mushroom:Mushroom = new Mushroom();
    mushrooms.push(mushroom); //add the new mushroom in vector
    addChild(mushroom);

    mushroom.x = posX;
    mushroom.y = posY;
    boy_mc.addEventListener(Event.ENTER_FRAME, scoring);

}


function scoring(e:Event) {

    var totalMushrooms:int = mushrooms.length;

    for(var i:int = 0; i < totalMushrooms; i++) {
        if (boy_mc.hitTestObject(mushrooms[i])) { //mushroom reference
            score = score + (MovieClip(e.currentTarget).point);
            score_txt.text = String(score);
            removeChild(mushrooms[i]); //remove
            mushrooms.splice(i, 1); //remove mushroom from vector
        }   
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2011-06-07
    • 1970-01-01
    • 2014-10-13
    • 2013-08-03
    相关资源
    最近更新 更多