在使用奇怪的问题的addChild和则hitTest与AS3奇怪、问题、addChild、hitTest

2023-09-08 15:05:12 作者:少女盗梦贼

我有几个问题,增加了孩子动作脚本3.我目前正在建设一个太空入侵者游戏,我写,增加了小行星的阶段函数时。

I am having a couple of problems when adding a child in action script 3. I am currently building a Space Invaders game and I am writing the function that adds the asteroids to the stage.

我的第一个问题是,所有的previous小行星被每一个我尝试添加一个新的小行星时添加。

My first problem is that the all previous asteroids are being added each time I try to add a new asteroid.

我的第二个问题是,当我添加hitTestOject功能。它抛出了一个错误,当太空船撞击小行星对象时,它不会做任何事情。

My second issue is when I add the hitTestOject function. It throws up an error and it doesn't do anything when the space ship hits the asteroid object.

下面是我收到的hitTestObject错误:

Here is the error I receive with the hitTestObject:

类型错误:错误#1034:类型强制失败:不能将ast_0   到flash.display.DisplayObject。在   spaceranger_fla :: MainTimeline / addAstroid()在   flash.utils ::定时器/ _timerDispatch()在flash.utils ::定时器/周期()

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()

和这里是我的code。我用一个定时器所以每个小行星被添加每5000毫秒:

And here is my code. I use a timer so each asteroid is added every 5000ms:

// 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);
    trace(i);
    if(ship.hitTestObject(ast.name)){
        gotoAndStop("2");
    }
i = i+1;
}

astTimer.start();

一些意见,建议和答案将大大AP preciated:)

Some advice, recommendations and answers will be greatly appreciated :)

更新

我整理了循环错误。老小行星不会再出现了! :D

I sorted the looping error. Old asteroids no longer appear again! :D

非常感谢,

彼得·斯图尔特

推荐答案

按照您的第一个问题,它不会出现增量 - 它总是 0

Per your first problem, it does not appear i increments - it's always 0.

在您指定的名称,增量

When you assign name, increment i:

ast.name = "ast_" + (i++).toString();

基本上,他说 I = I + 1;

接下来,点击测试针对实例本身,而不是一个身份:

Next up, hit test against the instance itself, not an identity:

ship.hitTestObject(ast)

不知道如何您的游戏作品,但它似乎是你真正想要的是两个处理程序:

Not sure how your game play works, but it would seem what you really want are two handlers:

在一个偶尔添加一个新的小行星 在一个测试碰撞

目前你的 addAsteroid()函数添加一个新的小行星,并立即进行测试,如果它在创建船舶碰撞。该小行星将永远不会被碰撞再次测试。如果这是类似于经典的小行星游戏,你可能需要在每个小行星推到一个数组,并为 ENTER_FRAME 添加事件侦听器来测试每个小行星反对的船舶碰撞。

Currently your addAsteroid() function adds a new asteroid and immediately tests if it collides with the ship upon creation. That asteroid will never be tested for collision again. If this is similar to a classic asteroids game, you may want to push each asteroid to an array, and add an event listener for ENTER_FRAME to test each asteroid for collision against the ship.

 
精彩推荐