Action Script 3. Animation loop forever after goto

2019-09-18 23:00发布

问题:

I'm creating simple flash game. I have problem, animation loop forever after I user gotoAndStop() and I have lag when during animation if character have collision with ground or any stage (if character flying on the air don't have any lags)

Here is collision list:

var myCollisionList:CollisionList = new CollisionList(Hero);
myCollisionList.addItem(ground);
myCollisionList.addItem(ground3);
myCollisionList.addItem(ground5);
myCollisionList.addItem(ground4);

And here is my part of code where jumping with animation.

if(Hero.y_speed>0 && myCollisionList.checkCollisions().length > 0 )
{
    Hero.y_speed=0;
    Hero.x_speed=0;

    if(space)
    {
        if (ground.hitTestPoint(Hero.x + 28, Hero.y+20, true))
        {
            Hero.gotoAndStop("attack");
            stop();
            Hero.y_speed = -20;
        }
    }
}

UPDATE: Screenshot of the map:

UPDATE 2:

Here is part of code for move character to left (to right side the same) side, I know Its terrible, but I don't know how to make It better.

pakopos - name of CollisionList

fonas - background

    var pakopos:CollisionList = new CollisionList(Hero);
    pakopos.addItem(ground);
    pakopos.addItem(ground3);
    pakopos.addItem(ground5);
    pakopos.addItem(ground4);

if(left){

    Hero.x_speed = -walkspeed;

    setDirection(1);

    if(pakopos.checkCollisions().length > 0) {

    if (ground5.hitTestPoint(Hero.x - 26, Hero.y-120, true)) {                  
        trace("Touching left side - ground5");
        ground5.x += 0;
        ground4.x += 0;
        ground3.x += 0;
        fonas.x += 0;
        Enemy.x += 0;
        }
    else if (Enemy.hitTestPoint(Hero.x - 26, Hero.y-120, true)) {
        trace("Touching Enemy");
        ground5.x += 0;
        ground4.x += 0;
        ground3.x += 0;
        fonas.x += 0;
        Enemy.x += 0;
        }
    else if (ground3.hitTestPoint(Hero.x - 26, Hero.y-120, true)) {
        trace("Touching left side - ground3");
        ground5.x += 0;
        ground4.x += 0;
        ground3.x += 0;
        fonas.x += 0;
        Enemy.x += 0;
        }
    else if (ground4.hitTestPoint(Hero.x - 26, Hero.y-120, true)) {
        trace("Touching left side - ground4");
        ground5.x += 0;
        ground4.x += 0;
        ground3.x += 0;
        fonas.x += 0;
        Enemy.x += 0;
    }else
    {
        Hero.x_speed = 0; 
        ground5.x += 4;
        ground4.x += 4;
        ground3.x += 4;
        fonas.x += 4;
        Enemy.x += 4;

    }}
    else {
        ground5.x += 4;
        ground4.x += 4;
        ground3.x += 4;
        fonas.x += 4;
        Enemy.x += 4;

    }}

回答1:

Your code is very cryptic so it's hard to say what's going on without actually debugging it but my guess is that you're checking collisions multiple times every time your posted code runs. You should check the documentation of the checkCollisions() function that you use since it seems to return an array of results. It likely gives you all collisions that it found among the objects in the collision list so you don't have to call hitTestPoint() directly afterwards.

Edit:

I'm not familiar with CDK that you use in your code but it returns an array of collision results where each result has the colliding objects, their angle and if they're overlapping. For what you're trying to do it seems like an overkill but as I said, I'm not familiar with it - it may be a lot faster then hitTestPoint().

I'd recommend using only one hit test method - either use CDK or use hitTestPoint() but not both. Both will give you pixel perfect detection results. If you use CDK, read the documentation on how checkCollisions() works. You'd have to do something like this:

...
var oResults:Array = pakopos.checkCollisions();
var nCount:int = oResults.length;

if (nCount > 0)
{
    for ( var i:int = 0; i <nCount; i++ )
    {
        var oHit:Object = oResults[i];

        // TODO check `oHit.object1` and `oHit.object2` to see which objects
        // collided and do something based on that
        // You may have multiple results since your hero may collide with
        // 'ground' and 'ground4' at the same time.
    }
}

Your collision group (pakopos) setup for CDK seems to be off - you won't have to determine if there's a collision between all the objects in your group - I assume you don't care if ground collides with ground4 but you added both to your collision list.

I think (not sure) that checkCollisions() tries to check all objects in the group against all other objects. What you need is check one object (Hero) against a list of objects (the various ground objects). That's a massive difference between the number of checks. This and your extra calls to hitTestPoint() can easily account for your lag.