I added animation to the timeline of my movie clip, now its no longer colliding with objects as it did before

StackOverflow https://stackoverflow.com/questions/20967368

Question

I'm working on a platform game in AS3 for Flash. There is of course a player character and enemies for him to interact with. I first made a simple placeholder graphic for the enemy while I worked on the code. I got the enemy to move back and forth between two bumpers with code. He also collides with the player, sending them back to the start screen. Now that I got the code working, I wanted to add some animations to the enemies so they walk instead of just skate across the ground. I added the animations to the timeline of the enemy movie clip. Now when I test the game, the animations play fine and the enemy begins to move, but he passes through the first bumper and doesn't collide with the player at all. If I force the movie clip to stop and not play the collision starts to work again. What would be causing this?

This is the code within the main timeline of the .fla file.

addEnemiesToLevel1();
addBumpersToLevel1();

function addEnemiesToLevel1():void
{
    addEnemy(700, -54);
    addEnemy(1341, -54);
    addEnemy(2187, -54);
}

function addBumpersToLevel1():void
{
    addBumper(900, -80);
    addBumper(644, -80);
    addBumper(1135, -90);
    addBumper(1380, -90);
    addBumper(2053, -90);
    addBumper(2226, -90);
}


    function addEnemy(xLocation:int, yLocation:int):void
{
    var enemy:Enemy = new Enemy(xLocation, yLocation);
    back.addChild(enemy);
    enemy.addEventListener(Event.REMOVED, enemyRemoved);
    enemyList.push(enemy);
}


function addBumper(xLocation:int, yLocation:int):void
{
    var bumper:Bumper = new Bumper(xLocation, yLocation);
    back.addChild(bumper);
    bumper.visible = false;
    bumperList.push(bumper);
}

//corralling the bad guys with bumpers
    if (enemyList.length > 0){ //enemies left in the enemyList?
        for (var k:int = 0; k < enemyList.length; k++){ // for each enemy in the enemyList
            if (bumperList.length > 0){
                for (var h:int = 0; h < bumperList.length; h++){ // for each bumper in the List
                    if ( enemyList[k].hitTestObject(bumperList[h]) ){
                        enemyList[k].changeDirection();
                        }
                    }
                }
            }
        }

//player and enemy collisions
    if (enemyList.length > 0){ //enemies left?
        for (var m:int = 0; m < enemyList.length; m++){ // for each enemy in the enemyList
            if ( enemyList[m].hitTestObject(player) ){
                trace("player collided with enemy");
                gotoAndStop(4);                     
                enemyList[m].removeSelf();
            }
        }
    }
}

This is the enemy class file.

package {
import flash.display.MovieClip;
import flash.events.Event;public class Enemy extends MovieClip {

    private var xSpeedConst:int = 2;
    private var flip:int = 1;

    public function Enemy(xLocation:int, yLocation:int) {
        // constructor code
        x = xLocation;
        y = yLocation;
        addEventListener(Event.ENTER_FRAME, loop);
    }

    public function loop(e:Event):void {
        if ((flip%2) == 1){
            x += xSpeedConst;
        } else if((flip%2) == 0){
            x += (-xSpeedConst);
        }
    }

    public function removeSelf():void {
        trace("remove enemy");
        removeEventListener(Event.ENTER_FRAME, loop);
        this.parent.removeChild(this);
    }

    public function changeDirection():void{
        trace("x ="+x);
        flip++;
        this.scaleX *= -1;
    }
}
}
Was it helpful?

Solution

I had a problem that was very similar to this: 2D Platform Game using hitTestPoint:glitches My problem was that my character's hitTestPoint wouldn't work after I animated the character. However, it wasn't because of the animation, it was because of my turning code for the character. I was using scaleX to flip the character. However, this flipped the points nested within the character as well (which were used to handle my collisions). I notice that you also flipped the character's scale:

public function changeDirection():void{
        trace("x ="+x);
        flip++;
        this.scaleX *= -1;
    }

Perhaps you are having the same problem as I did. If so, try removing this line of code for now:

this.scaleX *= -1;

...and see what happens...We can figure out what to do next from there

Drake Swartzy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top