Actionscript 3.0 如何从 addChild 定义对象?
Posted
技术标签:
【中文标题】Actionscript 3.0 如何从 addChild 定义对象?【英文标题】:Actionscript 3.0 How do i define objects from 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))"未定义属性蘑菇的访问。 为了完成,您应该通过编辑将错误消息添加到问题本身 【参考方案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
【讨论】:
以上是关于Actionscript 3.0 如何从 addChild 定义对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flash 中使用 Actionscript 3.0 制作动态视频播放器