Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by Joncom

In my game Scary Chicken, some people have reported that they instantly win after eating the first egg (it counts as more than one egg).

I suspect this is because the egg is not being killed fast enough, and the fox thinks he collided with it more than one time.

Here is my fox entity method for dealing with egg collisions:

// Handle collisions with other entities.
check: function(other) {

    if( other.name === 'chicken' ) {

        // Collided with the chicken.

        if( !this.god ){

            // Draw game over message to screen.
            ig.game.spawnEntity(EntityGameOver, 0, 0, { status: 'lose' } );

            // Chicken makes no more noises.
            ig.game.chicken.soundTimer.set(99999);

            this.kill();

        }

    } else {

        // Collided with an egg.

        this.eaten++;

        // Game over?
        if( this.eaten === ig.game.eggTotal ) {

            ig.game.spawnEntity(EntityGameOver, 0, 0, { status: 'win' });

            ig.game.chicken.kill();

            ig.game.clock.timer.pause();

            // High score?
            if( ! ig.game.highscore ) ig.game.highscore = ig.game.clock.timeString;

            else {

                // A high score already exists, overwrite?
                if( ig.game.highscore > ig.game.clock.timeString ) {

                    // New highscore!
                    ig.game.highscore = ig.game.clock.timeString;

                }

            }

        }

        this.eatSound.play();

        // Chicken becomes more aggressive.
        ig.game.chicken.setBehaviour( ig.game.chicken.behaviour + 1 );

        // Set music to match chicken aggressiveness.
        ig.music.play('track' + ( ig.game.chicken.behaviour + 1 ) );

        // Kill the egg entity.
        other.kill();

    }

}

ImpactJS does not use threads, correct? So that means there is zero chance that an egg entity could trigger fox.check() more than once?

8 years ago by Joncom

// Fox Entity
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.B,

// Egg Entity
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.NONE,

// Chicken Entity
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.NONE,

(No other TYPE.B entities exist. Just in case this is helpful.)

8 years ago by Joncom

Bug fixed by moving the line other.kill(); up top, like so:

    } else {

        // Collided with an egg.

        // Kill egg IMMEDIATELY!
        other.kill();

        this.eaten++;

        ...

    }

However... I still have absolutely NO IDEA why this fixed the bug. Last I heard JavaScript doesn't support threads and ImpactJS is not thread-like, so why would click() ever be called twice for the same egg entity? Hmm...

8 years ago by dominic

Quote from Joncom
ImpactJS does not use threads, correct?

Correct. Actually, there are no threads in JavaScript. The language is completely single threaded (ignoring WebWorkers).

I'm not sure what's happening in your game. Your code certainly does look correct and the check() method should only be called once. Maybe you're doing something "funky" in ig.game.chicken.setBehaviour() that somehow causes the game's update() method to execute twice!?


Btw. there's a .pause() method for ig.Timer that you could use to silence the chicken :)

8 years ago by Joncom

ig.game.chicken.setBehaviour() is actually quite simple.

        setBehaviour: function( tier ) {

            if( tier === 1 ) this.speed = this.maxVel.x = 100;

            else if( tier === 2 ) this.speed = this.maxVel.x = 150;

            else if( tier === 3 ) this.speed = this.maxVel.x = 250;

            else if( tier === 4 ) this.speed = this.maxVel.x = 250;

            this.behaviour = tier;

        }

Thanks for pointing out the silent-chicken technique; It is better.
Page 1 of 1
« first « previous next › last »