0

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.

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

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.

Here is the error I receive with the hitTestObject:

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

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

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

UPDATE

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

Many Thanks,

Peter Stuart

Peter Stuart
  • 2,362
  • 7
  • 42
  • 73

2 Answers2

1

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

Basically, saying 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:

  • one to occasionally add a new asteroid
  • one that tests for collisions

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.

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • Per your edit just now, I see you added `i=i+1;` to your example. – Jason Sturges May 05 '13 at 00:33
  • Hi Jason, yeah, I had removed the increment code when debugging, I forgot to add it in, however that hasn't made a difference. Each asteroid object is a movie clip and the movie clip was looping. I fixed that by adding "stop()" at the end of the motion tween. It is now getting my hitTestObject to work? – Peter Stuart May 05 '13 at 11:25
  • Yeah, your answer makes sense to me! I will create a timer that checks for hits every 500 milliseconds! Thanks :) – Peter Stuart May 05 '13 at 15:07
1

ship.hitTestObject(ast.name) is not going to work because ast.name is a String, not a DisplayObject.

Try this :

if(ship.hitTestObject(ast)){
        gotoAndStop("2");
    }
prototypical
  • 6,731
  • 3
  • 24
  • 34
  • No luck, ship.hitTestObject(ast) returns false :( – Peter Stuart May 05 '13 at 11:23
  • The question is about a coercion error, this solves that problem. If it returns false and you don't know why, that' a different question. Open a new question. – prototypical May 05 '13 at 14:55
  • I have: http://stackoverflow.com/questions/16384424/hittestobject-with-dynamically-add-movies – Peter Stuart May 05 '13 at 14:56
  • So accept this answer, it solves the coercion error. If you are not accepting answers that solve problems, how likely do you think it is that someone will help you with your next issue ? -- and by the way , that new question has the same issue that this answer solves. – prototypical May 05 '13 at 14:59
  • No, but it doesn't solve the overall picture. Don't tell me what answers to accept, it's at my own discretion. It did solve part of the problem, 1+ for that, but there are still problems within the script.... I now still get false with the hitTest but I think it is because it is running the hitTest at the point of it being added, and not later on. – Peter Stuart May 05 '13 at 15:06
  • You don't have to accept answers, but as I said, why would someone help you with your next problem if you won't? This is a question and answer site, not a debug my program in a single question site. If you have multiple issues in one question, you are being too broad with your questions. – prototypical May 05 '13 at 15:20