dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Help with hitting enemy

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    12

    Help with hitting enemy

    My Hero and Enemy are in the same movieclip.

    The Hero adds a punch movieclip like this:
    parent.addChild(_punch);
    _punch.x = this.x;
    _punch.y = this.y;

    How do I detect the punch hitTest with the enemy?

    Please help!
    Desperate and replies are extremely apreciated!
    Last edited by ishiaru; 06-08-2015 at 12:28 PM.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    HI,

    You could use a simple hitTestObjec.
    PHP Code:
    stage.addEventListener(Event.ENTER_FRAMEdoHitTest);
    function 
    doHitTest(e:Event):void
    {
        if (
    _punch.hitTestObject(otherThing))
        {
            
    trace("hit");
        }


  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    12
    The Map Movieclip that contains both the Hero and the Enemy is where the _punch is being added.
    That is my exact code.
    The Enemy has the instance name of "Sheep"
    How then, do I access the punch? Do I do this inside the punches Class File?
    My main question is that I do not know how to access the punch.

    Thank you so much!

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Can't do anything without your fla or lots more code and graphics

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    12
    Here is everything that's required for analysis. Hope you can help !!

    PHP Code:
    package  {
        
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
    import flash.events.KeyboardEvent;
        
    import flash.ui.Keyboard;
        
        
        public class 
    Hero extends MovieClip {
            
            private var 
    _vx 5;
            private var 
    _vy 5;
            
            private var 
    _movingDown:Boolean false;
            private var 
    _movingUp:Boolean false;
            private var 
    _movingLeft:Boolean false;
            private var 
    _movingRight:Boolean false;
            
            private var 
    _fireQ:Boolean false;
            
            private var 
    _sideToSide:SidetoSide;
            
            public function 
    Hero() {
                
    addEventListener(Event.ADDED_TO_STAGEonAddedToStage)
            }
            private function 
    onAddedToStage(event:Event):void
            
    {
                
    addEventListener(Event.REMOVED_FROM_STAGEonRemovedFromStage);
                
                
    stage.addEventListener(Event.ENTER_FRAMEenterFrameHandler);
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNonKeyPressed);
                
    stage.addEventListener(KeyboardEvent.KEY_UPonKeyReleased);
                
                
    _sideToSide = new SidetoSide;
            }
            private function 
    onRemovedFromStage(event:Event):void
            
    {
                
    removeEventListener(Event.ENTER_FRAMEenterFrameHandler);
                
    removeEventListener(KeyboardEvent.KEY_DOWNonKeyPressed);
                
    removeEventListener(KeyboardEvent.KEY_UPonKeyReleased);
            }
            private function 
    onKeyPressed(event:KeyboardEvent):void
            
    {
                if(
    event.keyCode == Keyboard.W)
                {
                    
    _movingUp true;
                }
                if(
    event.keyCode == Keyboard.S)
                {
                    
    _movingDown true;
                }
                if(
    event.keyCode == Keyboard.A)
                {
                    
    _movingLeft true;
                }
                if(
    event.keyCode == Keyboard.D)
                {
                    
    _movingRight true;
                }
                if(
    event.keyCode == Keyboard.Q)
                {
                    
    _fireQ true;
                }
            }
            private function 
    onKeyReleased(event:KeyboardEvent):void
            
    {
                if(
    event.keyCode == Keyboard.W)
                {
                    
    _movingUp false;
                }
                if(
    event.keyCode == Keyboard.S)
                {
                    
    _movingDown false;
                }
                if(
    event.keyCode == Keyboard.A)
                {
                    
    _movingLeft false;
                }
                if(
    event.keyCode == Keyboard.D)
                {
                    
    _movingRight false;
                }
                if(
    event.keyCode == Keyboard.Q)
                {
                    
    _fireQ false;
                }
            }
            private function 
    enterFrameHandler(event:Event):void
            
    {
                
    //Hero moves left
                
    if(_movingLeft && !_movingRight)
                {
                    
    -= _vx;
                    
    rotation 270;
                }
                
    //Hero moves right
                
    if(!_movingLeft && _movingRight)
                {
                    
    += _vx;
                    
    rotation 90;
                }
                
    //Hero moves Up
                
    if(_movingUp && !_movingDown)
                {
                    
    -= _vy;
                    
    rotation 0;
                }
                
    //Hero moves Down
                
    if(!_movingUp && _movingDown)
                {
                    
    += _vy;
                    
    rotation 180;
                }
                if(
    _fireQ)
                {
                    
    parent.addChild(_sideToSide);
                    
    _sideToSide.this.x;
                    
    _sideToSide.this.y;
                    
    _sideToSide.rotation this.rotation;
                }
            }
        }

    PHP Code:
    package  {
        
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
        
        public class 
    SidetoSide extends MovieClip {
            
            
            public function 
    SidetoSide() {
                
                
    addEventListener(Event.ADDED_TO_STAGEonAddedToStage);
                
    addEventListener(Event.REMOVED_FROM_STAGEonRemovedFromStage);
                
            }
            private function 
    onAddedToStage(event:Event):void
            
    {
                
    addEventListener(Event.ENTER_FRAMEenterFrameHandler);
            }
            private function 
    onRemovedFromStage(event:Event):void
            
    {
                
    removeEventListener(Event.ENTER_FRAMEenterFrameHandler);
            }
            private function 
    enterFrameHandler(event:Event):void
            
    {
                if(
    currentFrame == 11)
                {
                    
    parent.removeChild(this);
                }
            }
        }

    If there's anything else you need let me know I will add it in
    Attached Files Attached Files

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    PHP Code:
     private function enterFrameHandler(event:Event):void
            
    {
                if(
    currentFrame == 11)
                {
                    
    parent.removeChild(this);
                }
                
                if(
    this.hitTestObject(parent.parent.area.Sheep))
                {
                    
    trace("Hit");
                }
            } 
    I gave the clip with the sheep and box inside the name of "area"

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    12
    I am extremely grateful. Bye and enjoy your evening!

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