hitTestObject与动态添加的电影 [英] hitTestObject with dynamically add movies

查看:211
本文介绍了hitTestObject与动态添加的电影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code,增加了一个影片剪辑到舞台上。现在的问题是,它被添加后,我想那个对象实例上添加hitTestObject不过,我不断收到此错误:

 类型错误:错误#1034:类型强制失败:无法将ast_0转换为flash.display.DisplayObject。在
 

     

spaceranger_fla :: MainTimeline / addAstroid()在   flash.utils ::定时器/ _timerDispatch()在flash.utils ::定时器/周期()

下面是我的code:

  //添加astoid
VAR astTimer:定时器=新的定时器(5000);
astTimer.addEventListener(TimerEvent.TIMER,addAstroid);
变种我:数= 0;
功能addAstroid(E:TimerEvent):无效{
    VAR AST =新的星形线();
    ast.name =AST _+我;
    ast.y =的Math.random()* stage.stageHeight;
    ast.x = 565;
    的addChild(AST);
    如果(ship.hitTestObject(ast.name)){
        gotoAndStop(2);
    }
    I = I + 1;
}
 

据我所知,ast.name是一个字符串,而不是显示对象,所以我怎么把它转换为显示对象?

谢谢

佩德罗

解决方案

从的code从根本上误解,究竟如何它的工作原理你的问题派生。请不要见怪的评论,所有的程序员开始了这种方式。你问如何将字符串转换成一个显示对象,因此可以参考的对象。真正的问题应该是,我怎么觉得引用显示对象的另一种方法?有一种方法可以使用​​,这将抓住对象被它的名字,和对象返还给您。 [家长] .getChildByName()将允许您通过[对象]。名称是否必须进行程序这种方式。

不过,你还有一个问题,这个功能。如果你把上面我的建议,你可能会编辑code,你还是不会看到你正在寻找的结果,这将导致你的坏的结论,我错了。这是不是这样的,让我解释一下。要添加一个对象到舞台上,并做则hitTest()都在同一时刻。一旦小行星被添加,它基本上是检查完点击测试。即使你有你所有的code正确的,它不会回报你你正在寻找的结果。你需要addAstroid一个独立的功能外()来处理你的命中测试。

结论:鉴于c您所提供的$ C $,你问,我只想建议编写简单的概念,然后再转移到一个完全成熟的游戏(甚至是一个看似简单的游戏,如小行星)的问题。我还建议学习足以让错误的感觉。系统的声音是错误本身,如果你不理解的错误,你不会明白你的系统告诉你。这些仅仅是建议,不是命令。

这个环节应该有助于解释显示对象和您正在使用的方法。 <一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestObject%28%29"相对=nofollow>的ActionScript 3 - DisplayObject.hitTestObject()

I have code that adds a movie clip to the stage. The problem is, after it is added I want to add a hitTestObject on that object instance however I keep getting this error:

TypeError: Error #1034: Type Coercion failed: cannot convert "ast_0" to flash.display.DisplayObject.  at

spaceranger_fla::MainTimeline/addAstroid() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

Here is my code:

// Add astoid
var astTimer:Timer = new Timer(5000);
astTimer.addEventListener(TimerEvent.TIMER, addAstroid);
var i:Number = 0;
function addAstroid (e:TimerEvent):void{
    var ast = new astroid();
    ast.name = "ast_"+i;
    ast.y = Math.random()*stage.stageHeight;
    ast.x = 565;
    addChild(ast);
    if(ship.hitTestObject(ast.name)){
        gotoAndStop("2");
    }
    i = i+1;
}

I understand that ast.name is a string and not a display object, so how do I convert it to a display object?

Thanks,

Pedro

解决方案

Your question derives from a fundamental misunderstanding of the code and how exactly it works. Please don't take offense to that comment, all programmers start out this way. Your asking how you can convert a string into a display object so you can reference the object. The real question should be, "how do I find another method of referencing a display object?" There is a method you can use which will grab the object by it's name, and return the object to you. [parent].getChildByName() will allow you to pass the [object].name if you must conduct your program this way.

However, you have another problem with this function. If you take my advice above, you will probably edit your code and you still will not see the results you are looking for, which would lead you to the bad conclusion that I'm wrong. This is not the case, let me explain. You are adding an object to the stage, and doing the hitTest() all in the same moment. Once the asteroid is added, it's essentially finished checking the hit test. Even if you had all your code correct, it would not return you the results you are looking for. You will need a separate function OUTSIDE of addAstroid() to handle your hit test.

In conclusion: Given the code you have provided, and the question you asked, I would simply suggest programming simpler concepts before you move on to a full fledged game (even a seemingly simple game like Asteroids). I would also suggest learning enough to make sense of errors. The voice of your system is the error itself, if you don't understand the errors, you won't understand what your system is telling you. These are merely suggestions, not commands.

This link should help explain display objects and the method you are using. ActionScript 3 - DisplayObject.hitTestObject()

这篇关于hitTestObject与动态添加的电影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆