A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Hittest problem

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Posts
    7

    Hittest problem

    am having a problem with my hittest, the test which is suppose to check to see if the shoot that comes from my spaceship mc, hits the enemy which is generated in the exact same way which the shoots in the fireShoots function are is not being detected at all i have tried using a loop in order to keep track of the multiple shoots that are being created but even then it still doesnt work am at my wits end please help me.
    thanks in advance dbomb101

    function fireShoots() {
    var shoot = "shoot";
    var depth = _root.getNextHighestDepth();
    var newname = "shoot"+_root.ShootNum++;
    _root.attachMovie(shoot, newname, depth);
    _root[newname]._y = _root.avatar2._y-23;
    _root[newname]._x = _root.avatar2._x+1;
    _root[newname].onEnterFrame = function() {
    var bullet_speed = 7;
    this._y -= bullet_speed;
    if (this._y<0) {
    this.removeMovieClip();
    }
    };
    for (var i = 0; i<enemyNum; i++) {
    var myEnemy = _root["enemy"+i] //enemy0, enemy1, enemy2
    if (this.hitTest(myEnemy)) {
    this.removeMovieClip();
    _root.score++;
    //trace("enemy"+i);
    }
    }
    }

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    code:
    for (var i = 0; i<enemyNum; i++) {
    if (this.hitTest(_root["enemy"+i])) {
    this.removeMovieClip();
    _root.score++;
    //trace("enemy"+i);
    }
    }


    Try this.
    .

  3. #3
    Junior Member
    Join Date
    Apr 2007
    Posts
    7
    thanks for the help, but that doesnt work either, i will put up my entire code.

    [QUOTE]
    _root.attachMovie("avatar", "avatar2", 1, {_x:240, _y:350});
    _root, EnemyNum=0;
    _root, h=500;
    _root, t=0;
    _root, ShootNum=0;
    _root, score=0
    function moveAvatar(speed) {
    if (Key.isDown(Key.LEFT)) {
    _root.avatar2._x -= speed;
    } else if (Key.isDown(Key.RIGHT)) {
    _root.avatar2._x += speed;
    }
    if (Key.isDown(Key.SPACE)) {
    fireShoots();
    }
    if (Key.isDown(Key.CONTROL)) {
    Shield();
    }
    }
    function Enemys() {
    t++;
    if (t%30==0)
    var enemy= "enemy"
    var enemyNew= "enemy"+EnemyNum++;
    var depth= _root.getNextHighestDepth()
    _root.attachMovie(enemy,enemyNew,depth);
    _root[enemyNew]._y = 5;
    _root[enemyNew]._x = 10+random(h-25);
    _root[enemyNew].onEnterFrame = function() {
    var enemy_speed = 7;
    this._y += enemy_speed;
    if (this._y>350) {
    this.removeMovieClip();
    }
    }
    }
    function Shield(enterFrame) {
    _root.attachMovie("shield", "shield", 2);
    _root["shield"]._y = _root.avatar2._y-11;
    _root["shield"]._x = _root.avatar2._x-6;
    }
    function fireShoots() {
    var shoot = "shoot";
    var depth = _root.getNextHighestDepth();
    var newname = "shoot"+_root.ShootNum++;
    _root.attachMovie(shoot, newname, depth);
    _root[newname]._y = _root.avatar2._y-23;
    _root[newname]._x = _root.avatar2._x+1;
    _root[newname].onEnterFrame = function() {
    var bullet_speed = 7;
    this._y -= bullet_speed;
    if (this._y<0) {
    this.removeMovieClip();
    }
    }
    for (var i = 0; i<enemyNum; i++) {
    if (this.hitTest(_root["enemy"+i])) {
    this.removeMovieClip();
    _root.score++;
    //trace("enemy"+i);
    }
    }
    }
    _root.onEnterFrame = function() {
    moveAvatar(20);
    Enemys();
    };
    [QUOTE]
    Last edited by dbomb101; 04-17-2007 at 08:59 PM.

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Try
    code:
    for(var i:Number = 0; i<_root.EnemyNum; i++){
    for(var j:Number = 0; j<_root.ShootNum; j++){
    if(_root["shoot"+i].hitTest(_root["enemy"+i])){
    trace("hit");
    }
    }
    }


    If this doesn't work then could you upload your fla?
    .

  5. #5
    Junior Member
    Join Date
    Apr 2007
    Posts
    7
    sorry the code is still not working, i uploaded my fla file so u can look at it, thanks for the help and i hop am not pestering u too much
    Attached Files Attached Files

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    I haven't gotten a chance to fix the problem yet but redid the attaching so when firing it doesn't slow the computer down like it did before. I'll check it a bit later.
    Last edited by swak; 10-08-2007 at 07:41 AM.
    .

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Ok, I found the issue. This is the working code:
    code:
    _root[newname].onEnterFrame = function() {
    var bullet_speed = 7;
    this._y -= bullet_speed;
    if (this._y<0) {
    this.removeMovieClip();
    }
    for (var i:Number = 0; i<_root.EnemyNum; i++) {
    if (this.hitTest(_root["enemy"+i])) {
    _root["enemy"+i].removeMovieClip();
    this.removeMovieClip();
    }
    }
    };


    Anyways, the attachment should work fine. Tell me what you think.
    Last edited by swak; 10-08-2007 at 07:41 AM.
    .

  8. #8
    Junior Member
    Join Date
    Apr 2007
    Posts
    7
    it works perfectly, thanks so much ur a life saver

  9. #9
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    What do you think of the other changes I made? Does your older version seem at all slower to you?
    .

  10. #10
    Junior Member
    Join Date
    Apr 2007
    Posts
    7
    yeah actually the older one was slower although this one slows down when to many enemies are hit by the bullet but i wont be using anymore than 4 to 5 at a time
    anyway thanx for the help

  11. #11
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Yeah. It's understandable that it would slow down but this has less of an issue with it. Good luck with your game.
    .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center