简体   繁体   中英

How (if possible) can I check a collision with a MovieClip inside a MovieClip

This is my first time on this site and what I'm sure will be the first of many questions. My only real experience in this are classes I barley understood, books and on-line tutorials. Long story short I finally have a basic understanding of ActionScript but when I try to branch out, if the problem isn't from a tutorial I'm not sure where to turn.

So down to the gritty. I'm trying to write a code that will check for a collision of a MovieClip inside of a MovieClip on an instance on the stage. Here's the code.

idle_btn.addEventListener(MouseEvent.CLICK, idleAnim);
walk_btn.addEventListener(MouseEvent.CLICK, walkAnim);
attack_btn.addEventListener(MouseEvent.CLICK, attackAnim);
stage.addEventListener(Event.ENTER_FRAME, checkHit);


function idleAnim(event:MouseEvent):void{
    hero_mc.gotoAndPlay("idle");
    hitObject_mc.gotoAndStop("ready");
}

function walkAnim(event:MouseEvent):void{
    hero_mc.gotoAndPlay("walking");
    hitObject_mc.gotoAndStop("ready");
}

function attackAnim(event:MouseEvent):void{
    hero_mc.gotoAndPlay("attack");
}

function checkHit(event:Event):void{
    if(hero_mc.slash_mc.hitTestObject(hitObject_mc)){
        hitObject_mc.gotoAndPlay("hit");
    }
}

-The three buttons make the timeline in the hero_mc jump to the three actions I have animated.

-hitObject_mc.gotoAndStop("ready"); is to set the hitObject back to it's ready position after it's cut.

-Hero_mc & hitObject_mc are both named instances on the stage.

In the checkHit() function I originally had the .testHitObject first condition to be hero_mc only, and it worked. The hero's sword hit the box and it broke. I have in the hero MovieClip a MovieClip called slash_mc. It's the light flash from the sword that I want to be what determines a hit.

The idea of that being if I have the hero set to detect the collision than any part of him touches an enemy it will kill it, instead I only want the weapon to do that.

As the code is above, when I test the movie I get this error:

TypeError: Error #1010: A term is undefined and has no properties. at collisionPlusTest_fla::MainTimeline/checkHit()

it appears in the output window repetitively, I assume that has to do with the ENTER_FRAME listener.

To sum it up nice and easy, how do I get the slash_mc MovieClip inside the hero_mc MovieClip to detect collisions?

Any answers, tips or directions to look into will be a huge help. I appropriate your time and help.

This a part of the code i use for gaming, you might have to change it slightly to your own need: function testCollisions():void { for (var i:int = enemies.length -1;i >= 0;i--) { tempEnemy = enemies[i]; if (tempEnemy.hitTestObject(player)) { //this is the part to put the code for bouncing the ball back } } } function testCollisions():void { for (var i:int = enemies.length -1;i >= 0;i--) { tempEnemy = enemies[i]; if (tempEnemy.hitTestObject(player)) { //this is the part to put the code for bouncing the ball back } } }

Here i am testing collision of player on tempEnemy. i have all of my tempEnemy objects in the enemies array. this might not be the most efficient but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM