Results 1 to 14 of 14

Thread: [AS3] hitTestObject wont work.

  1. #1

    [AS3] hitTestObject wont work.

    Hello there.
    In my enemy.as file i have a code, if a bullet hits the enemy, enemy disappears. But for some reason it wont recognise my bullet.
    Code:
    public function bulletCollision(event:Event):void
    {
        if(this.hitTestObject(_r.ebullet))
        {
            removeEventListener(Event.ENTER_FRAME, eFrame);  
            removeEventListener(Event.ENTER_FRAME, bulletCollision);
            parent.removeChild(this);
        }
    }

    If i replace "ebullet" with "hero"(which is my player character) it will work, when hero touches enemy, enemy disappears, so im not sure why it wont recognise my bullet cos its the same as hero, i mean has a class name "ebullet" and library name "ebullet".
    I also have a bullet.as file where is written with hitTestObject when bullet touches the enemy it will make a splash animation.
    If you need to see more of my code to detect the problem i can link for sure.
    I would be in your debt if you help me.

  2. #2
    First of all check if the code even works by adding a trace.
    It probably isn't even working otherwise at least the listeners should have been removed.
    Anything written above this is probably rubbish.

  3. #3
    Well this bit of code seem to work, because if i replace "if(this.hitTestObject(_r.ebullet))" with "if(this.hitTestObject(_r.circle))" it will work (circle is the player character). So i dont know from where it comes.
    Shall i post the entire code?

  4. #4
    What I was saying is, write the code like this,

    PHP Code:
    if(this.hitTestObject(_r.ebullet))
        {
            
    trace("It works but does not work")
            
    removeEventListener(Event.ENTER_FRAMEeFrame);  
            
    removeEventListener(Event.ENTER_FRAMEbulletCollision);
            
    parent.removeChild(this);
        } 
    If the trace works, then you should probably post the whole code, else there might be something wrong with the name _r.ebullet.
    Anything written above this is probably rubbish.

  5. #5
    Hmm the trace doesnt work with the ebullet, but works with the circle.

  6. #6
    This usually happens with things like bullets because they're too small and travel too fast. What happens is in one frame the bullet is before the enemy, and in the next frame its past it; as far as Flash is concerned, it never technically hit the enemy.
    What I always do is just make a custom function for a hit-test. I won't bother writing it out, but basically have the function just check an inequality like so
    bullet.x <= enemy.x <= bullet.x + bullet.speed.x || bullet.x >= enemy.x >= bullet.x + bullet.speed.x
    (obviously checking both the x and the y axis)

    Sorry if this is a bit messy; if it doesn't make sense let me know and I'll write out a bit of AS3 for you

  7. #7
    really thanks for the reply, ill try it out and let you know how its going.

  8. #8
    Hmm i might know whats the problem, if i add a bullet to the stage from library the bullet automatically moves depending on hero location, so if i add an instance name to the bullet that i added to stage, "ebullet" that respective bullet will listen to my code, it will remove the enemy, but when i shoot with "Z" keys the bullet that come out from the gun wont remove the enemy i think because they dont have the instance name "ebullet".
    The _r.ebullet is the root of the bullet, if i dont give instance name ebullet to my bullet on the stage it will give me error like this: "A term is undefined and has no properties." on the output.

  9. #9
    So how can i make it so that the new bullets that come from the gun when im in game, have the instance name "ebullet"?

  10. #10
    You can't have bullets have the same instance name, you need to use for loops.

    Here is some code I had made a few weeks ago to make the player shoot bolts.

    PHP Code:
    function fire_bolt(e:MouseEvent):void {
        var 
    bolt_no:MovieClip = new heroweapon();
        
    bolt_no.name "bolt" bolt_no;
        
    gameplay.addChild(bolt_no);
        
    bolt_no.hero_weapon.x;
        
    bolt_no.hero_weapon.y;
        
    bolt_no.rotation angle;

    Here's how I manipulated them in the onEnterFrame

    PHP Code:
    if (total_bolt_no>0) {
        var 
    i=1;
        var 
    boltblur:BlurFilter=new BlurFilter(boltspeed/5,boltspeed/5,2);

        for (
    i=1i<=bolt_noi++) {
            var 
    bolt_name="bolt"+i;
            var 
    bolt_n:MovieClip=gameplay.getChildByName(bolt_name) as heroweapon;

            
    bolt_n.x+=Math.cos(bolt_n.angle*(Math.PI/180))*boltspeed;
            
    bolt_n.y+=Math.sin(bolt_n.angle*(Math.PI/180))*boltspeed;
        }

    Hope that helps
    Anything written above this is probably rubbish.

  11. #11
    Havent learned about "for"s yet, im not sure how to apply that in my case. But ill try to write something.

  12. #12
    So i tried to do something with a "for" but doesnt work, so if you can tell me what the code lacks, i would appreciate.
    Code:
            public function bulletCollision(event:Event):void
            {
                var bulletArray = new Array(_r.ebullet);
                for(var i=0; i<bulletArray.length; i++)
                {
                    if(this.hitTestObject(bulletArray[i])==true)
                    {
                        removeEventListener(Event.ENTER_FRAME, eFrame);
                        parent.removeChild(this);
                    }
                }
            }

    I dont get any errors but it just wont remove the enemy from stage.

  13. #13
    Again, try tracing when something doesn't work. Trace at different levels, ie , like in the if statement to see if it works, in the for loop to see if that is implemented, even in the function to see that it is even being called or not.

    And keep in mind that if there are no errors, the code is right in most cases, its the objects or variables that falter.

    And also, check again if the _r.ebullet actually exists.
    Anything written above this is probably rubbish.

  14. #14
    Hello again.
    Ive finnaly made it, i had to insert the ebullet thing into an array on the main timeline to make it work.
    And then at the function in my enemy class i call that array with root: "_r.bulletArray". It works for now but not sure if it ll cause problem in the future (like this is a newby code afterall).
    But anyway heres the code:

    On the main timeline on the shoot function:
    Code:
    var bulletArray:Array=new Array(); //<----this i added in the begining at the var's.
    
    
        if(zKeyIsDown && !canShoot && cantShoot)
        {
            var ebullet:MovieClip = new Bullet();
            if(circle.scaleX == 4/10)
            {
                ebullet.x=circle.x+75;
                ebullet.y=circle.y-134;
            }
            if(circle.scaleX == -4/10)
            {
                ebullet.x=circle.x-75;
                ebullet.y=circle.y-126;
            }
            ebullet.rotation=circle.rotation123.rotation;
            bulletArray.push(ebullet); //<------ i added this
            addChild(ebullet);
            canShoot=true;
            cantShoot=false;
        }
    And on the enemy.as file:

    Code:
            public function bulletCollision(event:Event):void
            {
                for(var i=0; i<_r.bulletArray.length; i++)
                {
                    if(this.hitTestObject(_r.bulletArray[i])==true)
                    {
                        removeEventListener(Event.ENTER_FRAME, eFrame);
                    }
                }
            }
    Thanks all for the help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012